{============================================================== sierpinski.p Started by Jeff Ondich on 2/19/96 Last modified 10/23/96 This program draws a Sierpinski triangle, given some information provided by the user. ==============================================================} #include program sierpinski(input,output); import graphics; const windowWidth = 600; windowHeight = 600; windowBottom = 100; windowLeft = 100; var startingPoint : graphpoint; howDeep, howWide : integer; {============================================================== StartGraphics initializes the graphics library, and opens a white graphics window with a black border. ==============================================================} procedure StartGraphics; var boundary : polyarray; begin { open a window } initializegraphics; createwindow(windowLeft,windowBottom,windowLeft+windowWidth,windowBottom+windowHeight); { flood the window with a white background } setrgbcolor(1.0,1.0,1.0); flood; flushgraphics; setrgbcolor(0,0,0); boundary[1].x := 0; boundary[1].y := 1; boundary[2].x := windowWidth-1; boundary[2].y := 1; boundary[3].x := windowWidth-1; boundary[3].y := windowHeight; boundary[4].x := 0; boundary[4].y := windowHeight; drawpoly( 4, boundary ); flushgraphics end; {============================================================== DrawTriangle draws the 45-45-90 triangle whose legs have length legLength and extend left and down from the point upperRight. ==============================================================} procedure DrawTriangle( bottomRight : graphpoint; legLength : integer ); var triangle : polyarray; begin triangle[1] := bottomRight; triangle[2].x := bottomRight.x - legLength; triangle[2].y := bottomRight.y; triangle[3].x := bottomRight.x; triangle[3].y := bottomRight.y + legLength; drawpoly( 3, triangle ); flushgraphics end; {============================================================== DrawSierpinski draws a Sierpinski triangle, calling DrawTriangle to draw the center triangle, and using recursive calls to itself to draw the lower levels of triangles. The parameter "center" refers to the bottom right vertex of the large central triangle. ==============================================================} procedure DrawSierpinski( center : graphpoint; depth, legLength : integer ); var halfLeg : integer; newCenter : graphpoint; begin if depth > 0 then begin { The big triangle in the center } DrawTriangle( center, legLength ); halfLeg := legLength div 2; { The bottom small triangle } newCenter.x := center.x - halfLeg; newCenter.y := center.y - halfLeg; DrawSierpinski( newCenter, depth-1, halfLeg ); { The upper left small triangle } newCenter.x := center.x - halfLeg; newCenter.y := center.y + halfLeg; DrawSierpinski( newCenter, depth-1, halfLeg ); { The upper right small triangle } newCenter.x := center.x + halfLeg; newCenter.y := center.y + halfLeg; DrawSierpinski( newCenter, depth-1, halfLeg ); end end; {============================================================== Main program ==============================================================} begin write( 'Please enter the x and y coordinates of the starting point: ' ); readln( startingPoint.x, startingPoint.y ); write( 'How many levels of triangles do you want to see? ' ); readln( howDeep ); write( 'How wide should the first triangle be? ' ); readln( howWide ); StartGraphics; DrawSierpinski( startingPoint, howDeep, howWide ); readln end.