Models‎ > ‎

Lorentz Attractor

This program iteratively draws the Lorentz Attractor projections in two vertical and one horizontal plane. The three dimensions are plotted as they are traversed in the lower right corner. It was written by me using Borland C++ for a physics tutorial, and was inspired by James Gleick's book CHAOS.



#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

#define ox1 160
#define oy1 120
#define ox2 160
#define oy2 360
#define ox3 480
#define oy3 120

double y1offset;
double y2offset;
double y3offset;

void do_lorentz(double x, double y, double z, double r);
void boundputpixel(int x, int y, int lx, int ly, int mx, int my, int color);

double scaler = 5.0;

int main(int argc, char *argv[])
{
int driver, mode;
register int i;
double x,y,z,r;

if(argc>7)
{
scaler=atof(argv[1]);
if(scaler==0.0) scaler = 5.0;
x=atof(argv[2]);
y=atof(argv[3]);
z=atof(argv[4]);
y1offset=atof(argv[5]);
y2offset=atof(argv[6]);
y3offset=atof(argv[7]);
r=atof(argv[8]);
if(!r)r=24.74;
driver=VGA;
mode=VGAHI;
initgraph(&driver,&mode,"");

setcolor(1);
line(ox1,0,ox1,480);
line(0,oy1,640,oy1);
line(0,oy2,640,oy2);
line(ox3,0,ox3,240);
line(320,240,320,480);
line(0,0,640,0);
line(0,240,640,240);
line(0,480,640,480);
line(0,0,0,480);
line(640,0,640,480);
line(320,0,320,480);
do_lorentz(x,y,z,r);

restorecrtmode();
}
else
{
printf("The Lorentz attractor. Program copyright 1994 Adrian Moore.\n");
printf("Supply a magnification scale, start x, start y, start z, x offset, y offset, z offset, [and parameter r].\n(all double precision).\n");
}
return 0;
}

void do_lorentz(double x, double y, double z, double r)
{
double a=10,b=2.6666,dt=0.001,dx,dy,dz;
int tx;
register int count=0, colour=1;

while(!kbhit())
{
/* Draw the attractor */
count++;
if(count>318)
{
colour++;
if(colour>15) colour=1;
count=0;
}

boundputpixel(ox1+(int)((z+y3offset)*scaler),oy1-(int)((y+y2offset)*scaler),0,0,320,240,colour);
boundputpixel(ox2+(int)((z+y3offset)*scaler),oy2-(int)((x+y1offset)*scaler),0,240,320,480,colour);
boundputpixel(ox3+(int)((x+y1offset)*scaler),oy3-(int)((y+y2offset)*scaler),320,0,640,240,colour);

/* Draw the horizontal plot */

tx=321+count;
setcolor(EGA_BLACK);
line(tx,241,tx,480);
boundputpixel(tx,oy2-(int)((x+y1offset)*scaler),320,241,640,480,EGA_WHITE);
boundputpixel(tx,oy2-(int)((y+y2offset)*scaler),320,241,640,480,EGA_WHITE);
boundputpixel(tx,oy2-(int)((z+y3offset)*scaler),320,241,640,480,EGA_WHITE);

dx=(a*(y-x))*dt;
dy=(x*(r-z)-y)*dt;
dz=(-b*z+x*y)*dt;

/* Draw the gradient */

boundputpixel(ox1+(int)((dz+y3offset)*scaler),oy1-(int)((dy+y2offset)*scaler),0,0,320,240,EGA_WHITE);
boundputpixel(ox2+(int)((dz+y3offset)*scaler),oy2-(int)((dx+y1offset)*scaler),0,240,320,480,EGA_WHITE);
boundputpixel(ox3+(int)((dx+y1offset)*scaler),oy3-(int)((dy+y2offset)*scaler),320,0,640,240,EGA_WHITE);

x=x+dx;
y=y+dy;
z=z+dz;
}
}

void boundputpixel(int x,int y,int lx,int ly,int mx,int my,int color)
{
if(x>=lx&&x<=mx&&y>=ly&&y<=my)putpixel(x,y,color);
}
Č
ċ
LORENTZ.zip
(30k)
Adrian Brightmoore,
May 24, 2011, 11:14 PM
Comments