/////////////////////////////////////////////////////////////////// // // sierpinski.cpp // // Started in Pascal by Jeff Ondich on 2/19/96 // Last modified 5/20/98 // /////////////////////////////////////////////////////////////////// #include #include // Constants const int windowWidth = 600; const int windowHeight = 600; const int windowBottom = 100; const int windowLeft = 100; // Prototypes void StartGraphics( void ); void DrawTriangle( graphpoint bottomRight, int legLength ); void DrawPicture( graphpoint center, int depth, int legLength ); int main( void ) { int howDeep = 6; int howWide = 200; graphpoint startingPoint; startingPoint.x = 300; startingPoint.y = 300; StartGraphics(); DrawPicture( startingPoint, howDeep, howWide ); cin.ignore(); return( 0 ); } /////////////////////////////////////////////////////////////////// // DrawTriangle draws the 45-45-90 triangle whose legs // have length legLength and extend left and down from // the point upperRight. /////////////////////////////////////////////////////////////////// void DrawTriangle( graphpoint bottomRight, int legLength ) { graphpoint triangle[3]; triangle[0] = bottomRight; triangle[1].x = bottomRight.x - legLength; triangle[1].y = bottomRight.y; triangle[2].x = bottomRight.x; triangle[2].y = bottomRight.y + legLength; drawpoly( 3, triangle ); flushgraphics(); } /////////////////////////////////////////////////////////////////// // DrawPicture draws a picture. Note the recursion. /////////////////////////////////////////////////////////////////// void DrawPicture( graphpoint center, int depth, int legLength ) { int halfLeg; graphpoint newCenter; if( depth > 0 ) { DrawTriangle( center, legLength ); halfLeg = legLength / 2; newCenter.x = center.x - halfLeg; newCenter.y = center.y - halfLeg; DrawPicture( newCenter, depth-1, halfLeg ); newCenter.x = center.x - halfLeg; newCenter.y = center.y + halfLeg; DrawPicture( newCenter, depth-1, halfLeg ); newCenter.x = center.x + halfLeg; newCenter.y = center.y + halfLeg; DrawPicture( newCenter, depth-1, halfLeg ); } } /////////////////////////////////////////////////////////////////// // StartGraphics initializes the graphics package // and opens a black window for drawing. /////////////////////////////////////////////////////////////////// void StartGraphics( void ) { // Call initializegraphics only once during the program, // before you do any other graphics initializegraphics(); createwindow( windowLeft, windowBottom, windowLeft + windowWidth, windowBottom + windowHeight ); setrgbcolor( 1.0, 1.0, 1.0 ); flood(); flushgraphics(); graphpoint boundary[4]; setrgbcolor( 0, 0, 0 ); boundary[0].x = 0; boundary[0].y = 1; boundary[1].x = windowWidth-1; boundary[1].y = 1; boundary[2].x = windowWidth-1; boundary[2].y = windowHeight; boundary[3].x = 0; boundary[3].y = windowHeight; drawpoly( 4, boundary ); flushgraphics(); }