2008-01-09

Lorenz attractor

The Lorenz attractor is a set of differential equations which are popular in the field of Chaos. Following job visualize Lorenz attractor:
static void JobLorenzAttractor(Args _args)
{
        #define.iterations(1000000)

        #define.sigma(5)
        #define.r(15)
        #define.b(1)

        #define.imageSizeX(300)
        #define.imageSizeY(300)

        Form            form;
        FormRun         formRun;
        FormDesign      formDesign;

        Args            args;
        FormBuildDesign formBuildDesign;

        FormBuildWindowControl formBuildWindowControl;
        FormWindowControl pane;

        Image image = new Image();

        void DrawAttractor()
        {
                real x, y, z;
                int color;

                real t;

                real x1, y1, z1;
                real dt;

                int _x, _y;
                ;

                color = 0xFF00;

                x = 3.051522;
                y = 1.582542;
                z = 15.62388;

                dt = 0.0001;

                for(t=0; t<#iterations; t++)
                {
                        x1 = x + #sigma*(y-x)*dt;
                        y1 = y + (#r*x-y-z*x)*dt;
                        z1 = z + (x*y-#b*z)*dt;
        
                        x = x1; y = y1; z = z1;

                        _x = 16*(y - x*0.292893) + 150;
                        _y =-13*(z + x*0.292893) + 330;

                        if((_x>0)&&(_x<#imageSizeX)&&(_y>0)&&(_y<#imageSizeY))
                        {
                                image.setPixel(_x,_y, color);// + (t div 1000));
                        }
                }
        }
        ;

        form = new Form();
        formBuildDesign = form.addDesign('design');
        formBuildDesign.hideToolbar(true);

        formBuildDesign.columns(1);
        formBuildDesign.topMode(1); // Auto
        formBuildDesign.leftMode(1); // Auto
        formBuildDesign.widthMode(1); // Auto
        formBuildDesign.heightMode(1); // Auto
        formBuildDesign.width(1.4*#imageSizeX);

        formBuildWindowControl = formBuildDesign.addControl(FormControlType::IMAGE, 'pane');
        formBuildWindowControl.height(#imageSizeY);
        formBuildWindowControl.width(#imageSizeX);
        formBuildWindowControl.backgroundColor(0);

        args = new Args();
        args.object(form);
        formRun = classFactory.formRunClass(args);
        formRun.init();
        formRun.resetSize();
        formDesign = formRun.design();
        formRun.resetSize();
        formrun.formOnTop();
        formRun.run();


        formRun.design().caption('Lorenz attractor');
        pane = formRun.control(formBuildWindowControl.id());

        image.saveType(ImageSaveType::BMP_UNCOMP);
        image.createImage(#imageSizeX, #imageSizeY, 24);
        DrawAttractor();         pane.image(image);         formRun.design().caption(strFmt('Lorenz attractor - sigma: %1, r: %2, b: %3, iterations: %4',                 #sigma, #r, #b, #iterations));         formRun.wait(); }
Note: in the job the number of iterations is equal one million by default, so it takes several minutes to see the result. You may change parameters sigma, r, b and iterations and enjoy the behaviour of attractor.
Copyright © 2008 Ruslan Goncharov

1 comment:

Anonymous said...

thanks for this nice example, as I was looking for some easy lorenz attractor code for some while.