/******************************************************************************************** Step7.cpp Jim Millard for CO311 Simple program that displays a 4x4 Blue/Red checkerboard Uses the ezwin API ********************************************************************************************/ #include <iostream> #include <string> using namespace std; #include "ezwin.h" #include "rect.h" void DrawCheckerboard(SimpleWindow& W); //required prototype void BlockPosition(int row, int column, float blockHeight, float blockWidth, float& windowX, float& windowY); color SetColor(int row, int column, color, color); int ApiMain() { //create the primary window SimpleWindow W("Exercise 3.22, Step 7 (enhanced)"); W.Open(); DrawCheckerboard(W); //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) { //A stack of useful constants that can later be replaced by function parameters const float height=2., //static height of all blocks width =2.; //static width of all blocks const int BlocksAcross=4, //static width of checkerboard BlocksDown =4; //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; } }