/******************************************************************************************** Solution.cpp Jim Millard for CO311 Uses the ezwin API ********************************************************************************************/ #include <iostream> #include <string> using namespace std; #include "ezwin.h" #include "rect.h" //required prototype void DrawCheckerboard(SimpleWindow& W, float blockHeight, float blockWidth); void BlockPosition(int row, int column, float blockHeight, float blockWidth, float& windowX, float& windowY); color SetColor(int row, int column, color, color); int ApiMain() { float BlockHeight, BlockWidth; cout << "Enter the vertical height of the blocks: "; cin >> BlockHeight; cout << "Enter the horizontal width of the blocks: "; cin >> BlockWidth; //create the primary window SimpleWindow W("Exercise 3.22, variable block size", 16., 16.); W.Open(); DrawCheckerboard(W, BlockHeight, BlockWidth); //wait for input prior to quitting char ch; cout << "Enter a character to continue"; cin >> ch; Terminate(); return 0; } // //this function draws a 4x4 blue/red checkerboard with 2cm squares. // void DrawCheckerboard(SimpleWindow& W, float height, float width) { //A stack of useful constants that can later be replaced by function parameters const int BlocksAcross=8, //static width of checkerboard BlocksDown =8; //static height of checkerboard const color BaseColor = Blue, //static color of first block AltColor = Red; //static color of alternate block color currentColor; //the color we're drawing now float absX, absY; //the position of the block in the window //Draw by rows for(int row = 0; row < BlocksDown; row++) { //Draw the rectangles across the row for (int column = 0; column < BlocksAcross; column++) { //set the color based on its position currentColor = SetColor(row, column, BaseColor, AltColor); //calculate the position of the block in the display window BlockPosition(row, column, height, width, absX, absY); //create the block with relevant parameters RectangleShape Rxy(W, absX, absY, currentColor, width, height); //dray the block Rxy.Draw(); } //inner for loop } //outer for loop return; } //DrawCheckerboard // //this function automates the discovery of the EXACT position where the block will be placed //based on the row/column "general" placement, and the block's height and width. //This routine assumes that all blocks will ultimately be of identical height and that //the first row/column is (0,0) and the origin (0,0) is in the upper-left corner. // void BlockPosition(int row, int column, float blockHeight, float blockWidth, float& windowX, float& windowY) { windowX = column*blockWidth + blockWidth/2; windowY = row*blockHeight + blockHeight/2; } // //this function sets the color of the square based on it's position in the checkerboard //it takes advantage of the coincicence between the sum of row & column for the block and //whether that sum is even or odd. // color SetColor(int row, int column, color color_1, color color_2) { switch ((row + column)%2) { case 1: return color_2; default: return color_1; } }