// This program demonstrates more about colors and loops and functions #include #include #include "graphics.h" using namespace std; const int WINDOW_SIZE = 400; //The size of the window that we use const int MAX_PIXEL_X = WINDOW_SIZE - 1; //Maximum x value in pixel coordinates const int MAX_PIXEL_Y = WINDOW_SIZE - 1; //Maximum y value in pixel coordinates const double MAX_CART_X = 5; //Maximum x value in cartitian coordinates const double MAX_CART_Y = 5; //Maximum y value in cartitian coordinates const int MIN_PIXEL_X = 0; //Minimum x value in pixel coordinates const int MIN_PIXEL_Y = 0; //Minimum y value in pixel coordinates const double MIN_CART_X = 0; //Minimum x value in cartitian coordinates const double MIN_CART_Y = 0; //Minimum y value in cartitian coordinates const int PIXEL_WIDTH = MAX_PIXEL_X - MIN_PIXEL_X + 1; //Pixel Width const double CART_WIDTH = MAX_CART_X - MIN_CART_X; //Cartitian Width const int PIXEL_HEIGHT = MAX_PIXEL_Y - MIN_PIXEL_Y + 1; //Pixel Height const double CART_HEIGHT = MAX_CART_Y - MIN_CART_Y; //Cartitian Height //Function prototypes double x_cart_conversion(int px); double y_cart_conversion(int py); int cart_color(double cx, double cy); ///////////////////////////////////////////////////////////////////////// int main() { int pixel_x, pixel_y; //Coordinate variables double cart_x, cart_y; //Cartitian coordinate variables int color; //variable to hold color initwindow(WINDOW_SIZE, WINDOW_SIZE, "Additive Colors"); for(pixel_x = 0; pixel_x < WINDOW_SIZE; ++pixel_x) { for(pixel_y = 0; pixel_y < WINDOW_SIZE; ++pixel_y) { //Change pixel_x to Cartitian coordinate cart_x = x_cart_conversion(pixel_x); //Change pixel_y to Cartitian coordinate cart_y = y_cart_conversion(pixel_y); //Choose color based on those coordinate color = cart_color(cart_x, cart_y); putpixel(pixel_x, pixel_y, color); } } getch(); return EXIT_SUCCESS; } ///////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////// double x_cart_conversion(int px) //Note: No semicolon here! { double ans; ans = CART_WIDTH/PIXEL_WIDTH * px + MIN_CART_X; return ans; } ///////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////// double y_cart_conversion(int py) //Note: No semicolon here! { double ans; ans = -CART_HEIGHT/PIXEL_HEIGHT * py + MAX_CART_X; return ans; } ///////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////// int cart_color(double cx, double cy) { if((cx > 2) && (cy < 3)) { return COLOR(255, 0, 0); //This is red } else { return COLOR(0, 0, 0); //This is black } } /////////////////////////////////////////////////////////////////////////