home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / GRID.ZIP / grid.cpp
Encoding:
C/C++ Source or Header  |  1999-05-24  |  6.7 KB  |  220 lines

  1.  
  2.  
  3.  GRID SOURCE CODE
  4.  
  5.   
  6.  
  7.  
  8.    
  9.      //________________________________________________________________________//
  10.      //|             _____                            ____  ____              |//
  11.      //|            / ___/____  __  _______________  / __ \/ __ \             |//
  12.      //|            \__ \/ __ \/ / / / ___/ ___/ _ \/ / / / / / /             |//
  13.      //|           ___/ / /_/ / /_/ / /  / /__/  __/ /_/ / /_/ /              |//
  14.      //|          /____/\____/\__,_/_/   \___/\___/\____/\____/               |//
  15.      //|                                                                      |//
  16.      //|      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       |//
  17.      //|______________________________________________________________________|//
  18.      //                                                                        //
  19.      // Grid
  20.      // ----
  21.      // Author: Jeremy Petter
  22.      // Date  : March 10, 1997
  23.      // Email : source00@geocities.com
  24.      // Copywrite (C) The Source00, 1998
  25.  
  26.      #pragma hdrstop
  27.      #include 
  28.      #include 
  29.  
  30.      const int MAX_ROW = 40;
  31.      const int MAX_COL = 20;
  32.  
  33.      ////////////////////////////////////////////////////////////////////////////
  34.      // Class typeGrid
  35.      // ======
  36.      // Attributes-----
  37.      // protected:
  38.      //  grid     Contains points in the array.
  39.      //
  40.      // Methods--------
  41.      // public:
  42.      //  CONSTRUCTOR: Make the grid empty.
  43.      //  DESTRUCTOR:  Delete the grid array.
  44.      //  setOn        Turn a grid point on.
  45.      //  setoff       Turn a point off.
  46.      //  drawLine     Draw a line on the grid.
  47.      //  display      Display the grid.
  48.      //
  49.      class typeGrid {
  50.         protected:
  51.            char   grid [MAX_COL][MAX_ROW];   // typeGrid.
  52.  
  53.            public:
  54.               // Constructor, Destructor.
  55.               typeGrid()  { empty(); }
  56.               ~typeGrid() { delete grid; }
  57.  
  58.               // Class member functions and prototypes.
  59.               void setOn (int c, int r) { grid [c][r] = '*'; }
  60.               void setOff(int c, int r) { grid [c][r] = ' '; }
  61.  
  62.               void empty();
  63.               void drawLine(int, int, int, int);
  64.               void display();
  65.      }; // end class typeGrid
  66.  
  67.  
  68.      ////////////////////////////////////////////////////////////////////////////
  69.      // grid                                                                empty
  70.      // =====
  71.      // Set the grid to all blanks.
  72.      //
  73.      void typeGrid::empty() {
  74.  
  75.         for (int c = 0; c < MAX_COL; c++)
  76.            for (int r = 0; r < MAX_ROW; r++)
  77.            grid[c][r] = ' ';
  78.      } // end empty.
  79.  
  80.  
  81.      ////////////////////////////////////////////////////////////////////////////
  82.      // grid                                                              display
  83.      // =====
  84.      // Display the grid.
  85.      //
  86.      void typeGrid::display() {
  87.  
  88.         // Display a top of the grid.
  89.         cout << " ";
  90.         for (int i = 0; i <= MAX_COL; i++)
  91.            cout << "-";
  92.            cout << endl;
  93.  
  94.            // Display the contents of the grid.
  95.            for (int r = 0; r < MAX_ROW; r++) {
  96.               cout << "|";
  97.               for (int c = 0; c < MAX_COL; c++)
  98.                  cout << grid[c][r];
  99.               cout << endl;
  100.            } // end for
  101.  
  102.      } // end display
  103.  
  104.  
  105.      ////////////////////////////////////////////////////////////////////////////
  106.      // grid                                                             drawLine
  107.      // =====
  108.      // Display the grid.
  109.      //
  110.      void typeGrid::drawLine(int x1, int y1, int x2, int y2) {
  111.  
  112.         float slope = 0;  // Store slope.
  113.         float Dy = 0;     // Store change in y.
  114.         float Dx = 0;     // Store change in x.
  115.         int swap,         // Temp swap value.
  116.            t = 0;         // Temp value for y or x in equation.
  117.  
  118.         Dy = (y2-y1);     // Store change in y.
  119.         Dx = (x2-x1);     // Store change in x.
  120.  
  121.         if (Dx != 0)
  122.            slope = Dy/Dx; // Check division by zero error.
  123.         else
  124.            slope = 0;
  125.  
  126.         if ((x1 != x2)&&(y1 != y2))          // Diagonal line.
  127.            if ((slope <= 1)&&(slope >= -1))  // Decides which equation to use.
  128.  
  129.            // Slope is between 1 and -1.
  130.            for (int x = x1; x <= x2; x++) {
  131.               t = int((slope * (x - x1) + y1)+.5);
  132.               grid[x][t] = '*';
  133.            } // end for
  134.  
  135.            else {
  136.  
  137.            // Slope is outside of 1 and -1, y2 < y1.
  138.            if (y2 < y1)
  139.               for (int y = y2 ; y <= y1; y++) {
  140.                  t = int(((y-y1+(slope*x1))/slope)+.5);
  141.                  grid[t][y] = '*';
  142.               } // end for
  143.  
  144.            else
  145.              // Use this equation if y1 < y2.
  146.              for (int y = y1 ; y <= y2; y++) {
  147.                 t = int(((y-y1+(slope*x1))/slope)+.5);
  148.                 grid[t][y] = '*';
  149.              } // end for
  150.  
  151.            } // end else
  152.  
  153.            // Coordinates are a point.
  154.            else if ((x1==x2) && (y1==y2))
  155.               grid[x1][y1] = '*';
  156.  
  157.            else if (x1 == x2) {
  158.  
  159.               // Swaps y1 and y2 to prevent negative.
  160.               if (y1 > y2) {
  161.                  swap = y2;
  162.                  y2 = y1;
  163.                  y1 = swap;
  164.               } // end if
  165.  
  166.               // Plot vertical line.
  167.               for (int y = y1; y <= y2; y++)
  168.                  grid[x1][y] = '*';
  169.            } // end else
  170.  
  171.            else {
  172.  
  173.               // Horizontal line test.
  174.               if (x1 > x2) {
  175.                  swap = x1;
  176.                  x1 = x2;
  177.                  x2 = swap;
  178.               } // end if
  179.  
  180.               // Plots horizontal line.
  181.               for (int x = x1; x <= x2; x++) {
  182.                  grid[x][y1] = '*';
  183.               } // end for
  184.  
  185.            } // end else
  186.  
  187.      } // end drawLine
  188.  
  189.  
  190.      ////////////////////////////////////////////////////////////////////////////
  191.      // menu                                                                 main
  192.      // =====
  193.      // Main program to test grid.
  194.      //
  195.      void main(void) {
  196.  
  197.         int x1,y1,x2,y2;
  198.         typeGrid g;
  199.  
  200.         // Prompt and enter values for line.
  201.         cout << "Enter values for line:" << endl;
  202.         cout << "X1: "; cin >> x1;
  203.         cout << "Y1: "; cin >> y1;
  204.         cout << "X2: "; cin >> x2;
  205.         cout << "Y2: "; cin >> y2;
  206.  
  207.         // Draw the line on the graph.
  208.         g.drawLine(x1,y1,x2,y2);
  209.  
  210.         // Display the graph.
  211.         cout << endl << "Resulting Graph:" << endl;
  212.         g.display();
  213.  
  214.      } // end main
  215.  
  216.  
  217.  
  218. Copyright 1998,Jeremy Petter. All rights reserved worldwide. The Source shall not be liable in the event of incidental or consequential
  219. damages arising from the use of information supplied herein. 
  220.