Click here to Skip to main content
6,635,160 members and growing! (16,102 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Beginner License: The Code Project Open License (CPOL)

Simple IFS Engine (Iterated Function System)

By Maxim_Barsuk

How to build and visualize IF-systems.
C#, Windows (WinXP), .NET (.NET 2.0, .NET 3.0, .NET 3.5), Visual Studio (VS2008), GDI, Dev
Posted:18 Nov 2008
Views:4,710
Bookmarked:7 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.61 Rating: 2.67 out of 5
1 vote, 25.0%
1
1 vote, 25.0%
2

3

4
2 votes, 50.0%
5

IFSEngine_bin

Introduction

Iterated function systems (IFS) are a method of constructing fractals; the resulting constructions are always self-similar (more information about IFS systems is available on Wikipedia).

Background

This application is my Science project in university. This work can be useful for fractal building (texture generation, terrain generation, etc.).

Using the code

The main class is IFSEngine:

public class IFSEngine: IFractalable
{

    private IFSExample ifs;
    private double scaleX = 100;
    private double scaleY = -100;

    public double ScaleY
    {
        get { return scaleY; }
        set { scaleY = value; }
    }
    private static Random random = new Random();

    public double ScaleX
    {
        get { return scaleX; }
        set { scaleX = value; }
    }

    public IFSExample IFS
    {
        get { return ifs; }
        set { ifs = value; }
    }
    private int iteration = 10000;

    public int IterationCount
    {
        get { return iteration; }
        set { iteration = value; }
    }

    public System.Drawing.Bitmap GetPicture(int width, int height)
    {
        int mx = width/2;
        int my = height/2;
        
        double x = 0.0;
        double y = 0.0;
        double t;
        Bitmap bitmap = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(bitmap);

        for (int k = 0; k < iteration; k++)
        {
            double p = random.NextDouble();
            t = x;
            x = ifs.GetX(x, y, p);
            y = ifs.GetY(t, y, p);
            Int64 xx = Convert.ToInt64(mx + scaleX * x);
            Int64 yy = Convert.ToInt64(my + scaleY * y);
            if ((xx > 0) & (xx < width) & (yy > 0) & (yy < height))
            {
                bitmap.SetPixel((int)xx, (int)yy, 
                   Color.FromArgb(Convert.ToInt16(p * 200 + 20), 
                   Convert.ToInt16(p * 200 + 20), 
                   Convert.ToInt16(p * 200 + 20)));
            }
        }
        return bitmap;
    }

IFractalable is simple interface for fractal classes:

public interface IFractalable
{
    Bitmap GetPicture(int x, int y);
}

The class IFSExample is for the IFS system. This class include IFS coefficients.

public class IFSExample
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public static IFSExample FromIFSFile(string filename)
    {
        string text = "";
        using (StreamReader sr = new StreamReader(filename))
        {
            text = sr.ReadToEnd();
        }
        return new IFSExample(text);
    }

    public List<double[]> list = new List<double[]>();

    private void ParseString(string desc)
    {
        string fs = desc.Split(new string[1] { "Row" }, 
                    StringSplitOptions.RemoveEmptyEntries)[0];
        string name = fs.Split(':')[1].Split('\n', '\r')[0]; ;
        this.name = name;
        for (int i = 1; i < desc.Split(new string[1] { "Row" }, 
             StringSplitOptions.RemoveEmptyEntries).Length; i++)
        {
            string s = desc.Split(new string[1] { "Row" }, 
                       StringSplitOptions.RemoveEmptyEntries)[i].Split('\n','\r')[0];
            double[] koefs = new double[7];
            for (int j = 0; j < 7; j++)
            {
                koefs[j] = Convert.ToDouble(d);
            }
            list.Add(koefs);
        }
    }

    public IFSExample(string desc)
    {
        ParseString(desc);
    }

    public double GetX(double x, double y, double p)
    {
        double pp = 0;
        double[] koef = list[0];
        for (int i = 0; i < list.Count; i++)
        {
            pp += list[i][6];
            koef = list[i];
            if (p <= pp)
            {
                break;
            }
        }
        return koef[0] * x + koef[1] * y + koef[4];
    }

    public double GetY(double x, double y, double p)
    {
        double pp = 0;
        double[] koef = list[0];
        for (int i = 0; i < list.Count; i++)
        {
            pp += list[i][6];
            koef = list[i];
            if (p <= pp)
            {
                break;
            }
        }
        return koef[2] * x + koef[3] * y + koef[5];
    }
}

An IFS file has a simple structure:

Name:<Name>
Row <c11>, <c12>,  <c13>, <c14>, <c15>, <c16>, <p1>
...
Row <cn1>, <cn2>,  <cn3>, <cn4>, <cn5>, <cn6>, <pn>

For example:

Name:Leaf
Row 0.14,  0.01,  0.00, 0.51, -0.08, -1.31, 0.06
Row 0.43,  0.52, -0.45, 0.50,  1.49, -0.75, 0.37
Row 0.45, -0.49,  0.47, 0.47, -1.62, -0.74, 0.36
Row 0.49,  0.00,  0.00, 0.51,  0.02,  1.62, 0.21

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Maxim_Barsuk


Member
Hello! My name is Maxim Subbotin. I'm from Russia.
Occupation: Software Developer
Company: EPAM
Location: Russian Federation Russian Federation

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Nov 2008
Editor: Smitha Vijayan
Copyright 2008 by Maxim_Barsuk
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project