{============================================================== graphics4.p Started by Jeff Ondich on 2/19/96 Last modified 5/2/97 1. Try to predict what this program will draw before you run it. Then run it. 2. What does the extra stuff in StartGraphics do? 3. Take a closer look at the recursion in DrawPicture, and make sure you understand what's going on. 4. Now go to your terminal window and type /Accounts/courses/cs117/sierpinski Can you modify DrawPicture to do that? (You can cheat, if you wish, by looking at http://www.mathcs.carleton.edu/faculty/jondich/CS117/Labs/May2/sierpinski.p) ==============================================================} #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; {============================================================== DrawPicture draws a picture. Not too interesting, perhaps, but note the recursion. ==============================================================} procedure DrawPicture( center : graphpoint; depth, legLength : integer ); var halfLeg : integer; newCenter : graphpoint; begin if depth > 0 then begin DrawTriangle( center, legLength ); halfLeg := legLength div 2; newCenter.x := center.x - halfLeg; newCenter.y := center.y - halfLeg; DrawPicture( newCenter, depth-1, halfLeg ); end end; {============================================================== Main program ==============================================================} begin startingPoint.x := 300; startingPoint.y := 300; howDeep := 6; howWide := 200; StartGraphics; DrawPicture( startingPoint, howDeep, howWide ); readln end.