/* Filename: ShapeLoader.cpp * * Author: Br. Isidore Minerd * * Date: July 17, 2009 * * Description: This program expects a file named "drawing.txt" which follows the format * defined in the included document "fileformat.txt" * */ #include "DarkGDK.h" #include #include using namespace std; #define RECTANGLE_TYPE 1 #define CIRCLE_TYPE 2 // Function Prototypes void DrawBall(int CenterX, int CenterY, int Radius); void DrawEmptyBox(int ulx,int uly,int lrx,int lry); void DarkGDK ( void ) { fstream InFile; int type,filled,ulx,uly,lrx,lry,cx,cy,r; dbSetWindowTitle("A Simple Drawing Loader"); // Read in the file and draw appropriately InFile.open("drawing.txt", ios::in); if(InFile.fail()) { dbPrint("Could not open file drawing.txt!"); return; } while(! InFile.fail()) { InFile >> type; switch(type) { case RECTANGLE_TYPE: InFile >> filled >> ulx >> uly >> lrx >> lry; if(filled) { dbBox(ulx,uly,lrx,lry); } else { DrawEmptyBox(ulx,uly,lrx,lry); } break; case CIRCLE_TYPE: InFile >> filled >> cx >> cy >> r; if(filled) { DrawBall(cx,cy,r); } else { dbCircle(cx,cy,r); } break; } } dbWaitKey(); } /* Given: CenterX the x-coordinate of the center of the ball. * CenterY the y-coordinate of the center of the ball. * Radius the radius of the desired ball. * Task: To draw a filled-in ball with the given center and radius, at least as much of the ball as fits inside of * the bounding rectangle with upper left corner UpperLeftX, UpperLeftY and lower right corner LowerRightX, * LowerRightY. * Return: Nothing. */ void DrawBall(int CenterX, int CenterY, int Radius) { int RadiusSquared, XDiff, YDiff, UpperLeftX, UpperLeftY, LowerRightX, LowerRightY,rightX,rightY; // One of several ways to implement this UpperLeftX = CenterX - Radius; UpperLeftY = CenterY - Radius; RadiusSquared = Radius * Radius; for(int row = UpperLeftY; row <= CenterY; row++) { for(int col = UpperLeftX; col <= CenterX; col++) { XDiff = CenterX - col; YDiff = CenterY - row; if ((XDiff * XDiff + YDiff * YDiff) <= RadiusSquared) { for(int i=col,max = 2 * CenterX - col; i <= max; i++) { dbDot(i,row); dbDot(i,2*CenterY - row); } // We need not go any further to right, so we can break from this for loop break; } } } } /* Given: ulx The upper-left corner's x coordinate uly The upper-left corner's y coordinate lrx The lower-right corner's x coordinate lry The lower-right corner's y coordinate Task: Draw an empty rectangle, using the coordinates specified Note that this function does not check the validity of the coordinates' values (since we have not studied conditionals yet) Return: Nothing */ void DrawEmptyBox(int ulx,int uly,int lrx, int lry) { // Draw line from upper left to upper right (NB: urx = lrx and ury = uly) dbLine(ulx,uly,lrx,uly); // Draw line from upper right to lower right dbLine(lrx,uly,lrx,lry); // Draw line from lower right to lower left (NB: llx = ulx and lly = lry) dbLine(lrx,lry,ulx,lry); // Draw line from lower left to upper left dbLine(ulx,lry,ulx,uly); }