home *** CD-ROM | disk | FTP | other *** search
-
- program Sierpinski(input,output);
- {This program was taken from N. Wirth, "Algorithms + Data Structures =
- Programs, Prentice-Hall, 1976. Further information on Sierpinski curves
- may be found in "Creative Computing", July 1984.}
-
- {$U- Change the "-" to a "+" if you want Ctrl-Break to interrupt.}
-
- {The parameters below are set to draw Sierpinski curves up to level 6.
- When the "?" appears pressing "x" will exit the program. Pressing any
- other key will change the palette.}
-
- const n=6;h0=256;
- type AString = String[80];
- var i,h,x,y,x0,y0,xlast,ylast,plotcolor : integer;
-
- procedure SwitchToColor;
- begin
- memw[0000:$0410] := (mem[0000:$0410] and $00cf) or $0010;
- Textmode
- end;
-
- procedure CenterLine(ThisString : AString; xcoord, ycoord: integer);
- begin
- xcoord := xcoord + 20 - length(ThisString) div 2;
- gotoxy(xcoord,ycoord);
- write(ThisString);
- end;
-
- procedure Initialize;
- begin
- SwitchToColor;
- GraphColorMode;
- CenterLine('Sierpinski Curve',1,1);
- GraphWindow(30,10,319,199);
- GraphBackGround(1);
- Palette(0);
- end;
-
- procedure ChangePalette;
- var PaletteNumber : integer;
- Ch : char;
-
- begin
- PaletteNumber := 0;
- repeat
- read(Kbd, Ch);
- PaletteNumber := PaletteNumber+1;
- if PaletteNumber > 3 then PaletteNumber := 0;
- Palette(PaletteNumber)
- until(Ch ='x')
- end;
-
- procedure plotline;
- begin
- draw(xlast,ylast,x,y,plotcolor);
- xlast :=x;ylast:=y;
- end;
-
- procedure setplot;
- begin
- xlast := x; ylast := y;
- end;
-
- procedure A(i:integer); forward;
- procedure B(i:integer); forward;
- procedure C(i:integer); forward;
- procedure D(i:integer); forward;
-
- procedure A;
- begin if i > 0 then
- begin A(i-1);x:= x+h;y:=y-h;plotline;
- B(i-1);x:= x+2*h;plotline;
- D(i-1);x:=x+h;y:=y+h;plotline;
- A(i-1)
- end
- end;
-
- procedure B;
- begin if i > 0 then
- begin B(i-1);x:=x-h;y:=y-h;plotline;
- C(i-1);y:=y-2*h;plotline;
- A(i-1);x:=x+h;y:=y-h;plotline;
- B(i-1)
- end
- end;
-
- procedure C;
- begin if i > 0 then
- begin C(i-1);x:=x-h; y:=y+h;plotline;
- D(i-1);x:=x-2*h;plotline;
- B(i-1);x:=x-h;y:=y-h;plotline;
- C(i-1)
- end
- end;
-
- procedure D;
- begin if i > 0 then
- begin D(i-1);x:=x+h;y:=y+h;plotline;
- A(i-1);y:=y+2*h;plotline;
- c(i-1);x:=x-h;y:=y+h;plotline;
- D(i-1)
- end
- end;
-
- begin
- initialize;
- plotcolor := 1;
- i := 0; h:=h0 div 4; x0 := 2*h; y0 :=3*h;
- repeat i:=i+1;x0:=x0-h;
- h:=h div 2; y0:=y0+h;
- x:=x0;y:=y0;setplot;
- A(i);x:=x+h;y:=y-h;plotline;
- B(i);x:=x-h;y:=y-h;plotline;
- C(i);x:=x-h;y:=y+h;plotline;
- D(i);x:=x+h;y:=y+h;plotline;
- plotcolor := plotcolor + 1;if plotcolor > 3 then plotcolor := 1;
- gotoxy(39,25); write(i);
- until (i=n);
- gotoxy(39,24);
- write('?');
- ChangePalette;
- end.