home *** CD-ROM | disk | FTP | other *** search
-
-
- GRID SOURCE CODE
-
-
-
-
-
- //________________________________________________________________________//
- //| _____ ____ ____ |//
- //| / ___/____ __ _______________ / __ \/ __ \ |//
- //| \__ \/ __ \/ / / / ___/ ___/ _ \/ / / / / / / |//
- //| ___/ / /_/ / /_/ / / / /__/ __/ /_/ / /_/ / |//
- //| /____/\____/\__,_/_/ \___/\___/\____/\____/ |//
- //| |//
- //| T H E U L T I M A T E C O D E W A R R I O R S S I T E |//
- //|______________________________________________________________________|//
- // //
- // Grid
- // ----
- // Author: Jeremy Petter
- // Date : March 10, 1997
- // Email : source00@geocities.com
- // Copywrite (C) The Source00, 1998
-
- #pragma hdrstop
- #include
- #include
-
- const int MAX_ROW = 40;
- const int MAX_COL = 20;
-
- ////////////////////////////////////////////////////////////////////////////
- // Class typeGrid
- // ======
- // Attributes-----
- // protected:
- // grid Contains points in the array.
- //
- // Methods--------
- // public:
- // CONSTRUCTOR: Make the grid empty.
- // DESTRUCTOR: Delete the grid array.
- // setOn Turn a grid point on.
- // setoff Turn a point off.
- // drawLine Draw a line on the grid.
- // display Display the grid.
- //
- class typeGrid {
- protected:
- char grid [MAX_COL][MAX_ROW]; // typeGrid.
-
- public:
- // Constructor, Destructor.
- typeGrid() { empty(); }
- ~typeGrid() { delete grid; }
-
- // Class member functions and prototypes.
- void setOn (int c, int r) { grid [c][r] = '*'; }
- void setOff(int c, int r) { grid [c][r] = ' '; }
-
- void empty();
- void drawLine(int, int, int, int);
- void display();
- }; // end class typeGrid
-
-
- ////////////////////////////////////////////////////////////////////////////
- // grid empty
- // =====
- // Set the grid to all blanks.
- //
- void typeGrid::empty() {
-
- for (int c = 0; c < MAX_COL; c++)
- for (int r = 0; r < MAX_ROW; r++)
- grid[c][r] = ' ';
- } // end empty.
-
-
- ////////////////////////////////////////////////////////////////////////////
- // grid display
- // =====
- // Display the grid.
- //
- void typeGrid::display() {
-
- // Display a top of the grid.
- cout << " ";
- for (int i = 0; i <= MAX_COL; i++)
- cout << "-";
- cout << endl;
-
- // Display the contents of the grid.
- for (int r = 0; r < MAX_ROW; r++) {
- cout << "|";
- for (int c = 0; c < MAX_COL; c++)
- cout << grid[c][r];
- cout << endl;
- } // end for
-
- } // end display
-
-
- ////////////////////////////////////////////////////////////////////////////
- // grid drawLine
- // =====
- // Display the grid.
- //
- void typeGrid::drawLine(int x1, int y1, int x2, int y2) {
-
- float slope = 0; // Store slope.
- float Dy = 0; // Store change in y.
- float Dx = 0; // Store change in x.
- int swap, // Temp swap value.
- t = 0; // Temp value for y or x in equation.
-
- Dy = (y2-y1); // Store change in y.
- Dx = (x2-x1); // Store change in x.
-
- if (Dx != 0)
- slope = Dy/Dx; // Check division by zero error.
- else
- slope = 0;
-
- if ((x1 != x2)&&(y1 != y2)) // Diagonal line.
- if ((slope <= 1)&&(slope >= -1)) // Decides which equation to use.
-
- // Slope is between 1 and -1.
- for (int x = x1; x <= x2; x++) {
- t = int((slope * (x - x1) + y1)+.5);
- grid[x][t] = '*';
- } // end for
-
- else {
-
- // Slope is outside of 1 and -1, y2 < y1.
- if (y2 < y1)
- for (int y = y2 ; y <= y1; y++) {
- t = int(((y-y1+(slope*x1))/slope)+.5);
- grid[t][y] = '*';
- } // end for
-
- else
- // Use this equation if y1 < y2.
- for (int y = y1 ; y <= y2; y++) {
- t = int(((y-y1+(slope*x1))/slope)+.5);
- grid[t][y] = '*';
- } // end for
-
- } // end else
-
- // Coordinates are a point.
- else if ((x1==x2) && (y1==y2))
- grid[x1][y1] = '*';
-
- else if (x1 == x2) {
-
- // Swaps y1 and y2 to prevent negative.
- if (y1 > y2) {
- swap = y2;
- y2 = y1;
- y1 = swap;
- } // end if
-
- // Plot vertical line.
- for (int y = y1; y <= y2; y++)
- grid[x1][y] = '*';
- } // end else
-
- else {
-
- // Horizontal line test.
- if (x1 > x2) {
- swap = x1;
- x1 = x2;
- x2 = swap;
- } // end if
-
- // Plots horizontal line.
- for (int x = x1; x <= x2; x++) {
- grid[x][y1] = '*';
- } // end for
-
- } // end else
-
- } // end drawLine
-
-
- ////////////////////////////////////////////////////////////////////////////
- // menu main
- // =====
- // Main program to test grid.
- //
- void main(void) {
-
- int x1,y1,x2,y2;
- typeGrid g;
-
- // Prompt and enter values for line.
- cout << "Enter values for line:" << endl;
- cout << "X1: "; cin >> x1;
- cout << "Y1: "; cin >> y1;
- cout << "X2: "; cin >> x2;
- cout << "Y2: "; cin >> y2;
-
- // Draw the line on the graph.
- g.drawLine(x1,y1,x2,y2);
-
- // Display the graph.
- cout << endl << "Resulting Graph:" << endl;
- g.display();
-
- } // end main
-
-
-
- Copyright 1998,Jeremy Petter. All rights reserved worldwide. The Source shall not be liable in the event of incidental or consequential
- damages arising from the use of information supplied herein.
-