/* Filename: DrawEmptyBox.cpp * * Author: Br. Isidore Minerd * * Date: July 17, 2009 * * Description: This small Dark GDK program shows an example of a function which * draws an empty box on the screen using the ink color as it is set. * Also, it shows how one function can call another to prevent code * duplication. */ #include "DarkGDK.h" // Function prototypes void drawEmptyBox(int ulx,int uly,int lrx,int lry); void drawEmptyBoxWH(int ulx,int uly,int width,int height); void drawEmptySquare(int ulx,int uly,int sideLength); void DarkGDK() { dbSetWindowTitle("Empty Box Examples"); // Draw a rectangle using the main drawing function drawEmptyBox(50,300,300,400); dbCenterText(175,350,"drawEmptyBox"); // Draw a rectangle using the first derivative function drawEmptyBoxWH(10,10,150,50); dbCenterText(85,35,"drawEmptyBoxWH"); // Draw a square using the derivative function drawEmptySquare(125,75,150); dbCenterText(200,150,"drawEmptySquare"); // Wait for input from the keyboard dbWaitKey(); return; } /* 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); } /* Given: ulx The upper-left corner's x coordinate uly The upper-left corner's y coordinate width The width of the rectangle height The height of the rectangle Task: Draw an empty rectangle, using the given upper-left corner and width/height dimensions Note that this calls another function to draw the rectangle Return: Nothing */ void drawEmptyBoxWH(int ulx,int uly,int width,int height) { drawEmptyBox(ulx,uly,ulx + width,uly + height); } /* Given: ulx The upper-left corner's x coordinate uly The upper-left corner's y coordinate sideLength The length of the sides for the square Task: Draws an empty square using the the coordinates and side length specified Note also that this calls another function to draw the square Return: Nothing */ void drawEmptySquare(int ulx,int uly,int sideLength) { // NB: This could also call drawEmptyBoxWH(ulx,uly,sideLength,sideLength) // However, that would require one more function call, thereby taking // more processing time drawEmptyBox(ulx,uly,ulx + sideLength,uly + sideLength); }