home *** CD-ROM | disk | FTP | other *** search
- Program SolidFractal;
- {$C-,V-,K-}
- {
- Dirk W. Howard
- CS 542
- Winter 87
-
- Assignment Number 1
- Due January 16, 1987
-
- }
- {$I GRAPH.P }
-
- type
- vertex = record
- x,y :real;
- end;
- poly = record
- v1,v2,v3 :vertex;
- end;
- storarray = array[1..1100] of poly;
- polystorage = ^storarray;
-
- var
- a,b,temp,tp :polystorage;
- ch :char;
- NumOfPolys :integer;
-
- Procedure GetStartPoly;
- var
- filein :text;
- filename :string[14];
-
- begin
- clrscr;
- write(' Is data to be read in from a file? (Y/N) ');
- read(kbd,ch);
- if (ch = 'Y') or (ch = 'y') then
- begin
- writeln;
- write('Enter filename : ');
- readln(filename);
- assign(filein,filename);
- reset(filein);
- readln(filein,a^[1].v1.x,a^[1].v1.y);
- readln(filein,a^[1].v2.x,a^[1].v2.y);
- readln(filein,a^[1].v3.x,a^[1].v3.y);
- NumOfPolys:=1;
- close(filein);
- end
- else begin
- a^[1].v1.x:=0.0;
- a^[1].v1.y:=0.1;
- a^[1].v2.x:=1.0;
- a^[1].v2.y:=0.1;
- a^[1].v3.x:=0.5;
- a^[1].v3.y:=1.0;
- NumOfPolys:=1;
- end;
- end; { GetStartPoly }
-
- Procedure Display;
- var
- x1,y1,x2,y2,
- x3,y3,i :integer;
-
- begin
- clearscreen;
- for i:= 1 to NumOfPolys do
- begin
- x1:=round(320*a^[i].v1.x);
- y1:=round(200*a^[i].v1.y);
- x2:=round(320*a^[i].v2.x);
- y2:=round(200*a^[i].v2.y);
- x3:=round(320*a^[i].v3.x);
- y3:=round(200*a^[i].v3.y);
- draw(x1,200-y1,x2,200-y2,3);
- draw(x2,200-y2,x3,200-y3,3);
- draw(x3,200-y3,x1,200-y1,3);
- end;
- end;
-
- function normal(mu,sigma: real): real;
-
- var z:real;
-
- begin
- z:=sqrt(-2.0*ln(random))*cos(6.28319*random);
- normal:=z*sigma+mu
- end;
-
- Procedure NewPoint(x1,y1,x2,y2 :real; var nx,ny :real);
- var
- dx,dy,
- distance :real;
-
- begin
- dx:=x2-x1;
- dy:=y2-y1;
- distance:=sqrt(dx*dx+dy*dy)*0.3;
- nx:=(x1+x2)/2.0+normal(0.0,0.3)*distance;
- ny:=(y1+y2)/2.0+normal(0.0,0.3)*distance;
- end;
-
-
- Procedure ColorFractal;
- var
- x1i,x2i,x3i,
- y1i,y2i,y3i,
- x,y,color,i :integer;
- level,x1,x2,x3,
- y1,y2,y3 :real;
-
- begin
- for i:= 1 to NumOfPolys do
- begin
- x1:=a^[i].v1.x;
- y1:=a^[i].v1.y;
- x2:=a^[i].v2.x;
- y2:=a^[i].v2.y;
- x3:=a^[i].v3.x;
- y3:=a^[i].v3.y;
- level:=(y1+y2+y3)/3.0;
- y:=round(200*level);
- x:=round(320*((x1+x2+x3)/3.0));
- level:=level+normal(0.0,0.25);
- if level >= 0.8 then color:=3
- else if level >= 0.45 then color:=2
- else color:=1;
- x1i:=round(320*a^[i].v1.x);
- y1i:=round(200*a^[i].v1.y);
- x2i:=round(320*a^[i].v2.x);
- y2i:=round(200*a^[i].v2.y);
- x3i:=round(320*a^[i].v3.x);
- y3i:=round(200*a^[i].v3.y);
- draw(x1i,200-y1i,x2i,200-y2i,color);
- draw(x2i,200-y2i,x3i,200-y3i,color);
- draw(x3i,200-y3i,x1i,200-y1i,color);
- FillShape(x,200-y,color,color);
- end;
- end;
-
-
- Procedure Fractalize;
- var
- t1,t2,t3,t4 :poly;
- k,i,j :integer;
- mkt1,mkt2,mkt3 :boolean;
-
- begin
- k:=1;
- for i:= 1 to NumOfPolys do
- begin
- gotoxy(32,3); write(k:4);
- t1.v1:=a^[i].v1;
- t1.v2:=a^[i].v2;
- t2.v1:=a^[i].v2;
- t2.v2:=a^[i].v3;
- t3.v1:=a^[i].v3;
- t3.v2:=a^[i].v1;
- mkt1:=true;
- mkt2:=true;
- mkt3:=true;
- for j:=1 to k-1 do
- begin
- if (((t1.v1.x = tp^[j].v1.x) and (t1.v2.x = tp^[j].v2.x)) and
- ((t1.v1.y = tp^[j].v1.y) and (t1.v2.y = tp^[j].v2.y))) or
- (((t1.v2.x = tp^[j].v1.x) and (t1.v1.x = tp^[j].v2.x)) and
- ((t1.v2.y = tp^[j].v1.y) and (t1.v1.y = tp^[j].v2.y))) then
- begin
- t1.v3:=tp^[j].v3;
- mkt1:=false;
- end;
- if (((t2.v1.x = tp^[j].v1.x) and (t2.v2.x = tp^[j].v2.x)) and
- ((t2.v1.y = tp^[j].v1.y) and (t2.v2.y = tp^[j].v2.y))) or
- (((t2.v2.x = tp^[j].v1.x) and (t2.v1.x = tp^[j].v2.x)) and
- ((t2.v2.y = tp^[j].v1.y) and (t2.v1.y = tp^[j].v2.y))) then
- begin
- t2.v3:=tp^[j].v3;
- mkt2:=false;
- end;
- if (((t3.v1.x = tp^[j].v1.x) and (t3.v2.x = tp^[j].v2.x)) and
- ((t3.v1.y = tp^[j].v1.y) and (t3.v2.y = tp^[j].v2.y))) or
- (((t3.v2.x = tp^[j].v1.x) and (t3.v1.x = tp^[j].v2.x)) and
- ((t3.v2.y = tp^[j].v1.y) and (t3.v1.y = tp^[j].v2.y))) then
- begin
- t3.v3:=tp^[j].v3;
- mkt3:=false;
- end;
- end;
- if mkt1 then NewPoint(t1.v1.x,t1.v1.y,t1.v2.x,t1.v2.y,t1.v3.x,t1.v3.y);
- if mkt2 then NewPoint(t2.v1.x,t2.v1.y,t2.v2.x,t2.v2.y,t2.v3.x,t2.v3.y);
- if mkt3 then NewPoint(t3.v1.x,t3.v1.y,t3.v2.x,t3.v2.y,t3.v3.x,t3.v3.y);
- t4.v1:=t1.v3;
- t4.v2:=t2.v3;
- t4.v3:=t3.v3;
- b^[k].v1:=t1.v1; b^[k].v2:=t1.v3; b^[k].v3:=t3.v3; tp^[k]:=t1; k:=k+1;
- b^[k].v1:=t2.v1; b^[k].v2:=t2.v3; b^[k].v3:=t1.v3; tp^[k]:=t2; k:=k+1;
- b^[k].v1:=t3.v1; b^[k].v2:=t3.v3; b^[k].v3:=t2.v3; tp^[k]:=t3; k:=k+1;
- b^[k].v1:=t1.v3; b^[k].v2:=t2.v3; b^[k].v3:=t3.v3; tp^[k]:=t4; k:=k+1;
- end;
- NumOfPolys:=k-1;
- temp:=a;
- a:=b;
- b:=temp;
- display;
- ColorFractal;
- if NumOfPolys < 1000 then
- begin
- gotoxy(26,1); write('Continue (Y/N)?');
- read(kbd,ch);
- if (ch = 'Y') or (ch = 'y') then
- begin
- gotoxy(26,1); write('Working ');
- fractalize;
- end;
- end
- else begin
- gotoxy(26,1); write('Press any key');
- gotoxy(26,2); write(' to exit.');
- read(kbd,ch);
- end;
- end;
-
-
- begin { Main program }
- new(a);
- new(b);
- new(tp);
- GetStartPoly;
- graphcolormode;
- palette(2);
- Display;
- gotoxy(26,1); write('Continue (Y/N)?');
- read(kbd,ch);
- if (ch = 'Y') or (ch = 'y') then
- begin
- gotoxy(26,1); write('Working ');
- fractalize;
- end;
- textmode;
- end.