home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / BGICLA.ZIP / POINT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-24  |  8.0 KB  |  261 lines

  1. /**************************************************************************
  2.     Listing 4   -   POINT.CPP
  3.  
  4.     Written by Kevin D. Weeks, April 1990
  5.     Released to the Public Domain
  6. */
  7. #include <math.h>
  8. #include "point.hpp"
  9. #include "display.hpp"
  10.  
  11. extern Graphics_Display theDisplay;
  12.  
  13. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  14.     Constructor - creates an empty instance. all attributes are set to
  15.     zero.
  16. */
  17. Point::Point(void)
  18. {
  19.     x = y = 0;
  20.     virtual_x = virtual_y = 0.0;
  21. }
  22.  
  23. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  24.     Constructor - uses set_xy() method to initialize.
  25. */
  26. Point::Point(const int init_x, const int init_y)
  27. {
  28.     set_xy(init_x,init_y);
  29. }
  30.  
  31. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  32.     Constructor - uses set_xy() method to initialize.
  33. */
  34. Point::Point(const double init_x, const double init_y)
  35. {
  36.     set_xy(init_x,init_y);
  37. }
  38.  
  39. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  40.     change real X co-ordinate. if out of legal range, force to max.
  41. */
  42. void    Point::set_x(const int new_x)
  43. {
  44.     // check range
  45.     if (new_x > theDisplay.get_max_x())
  46.         x = theDisplay.get_max_x();
  47.     else
  48.         x = new_x;
  49.  
  50.     // set virtual co-ordinate
  51.     virtual_x = theDisplay.x_to_virtual(x);
  52. }
  53.  
  54. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  55.     change virtual X co-ordinate. if out of legal range, force to max.
  56. */
  57. void    Point::set_x(const double new_x)
  58. {
  59.     // check range
  60.     if (new_x > theDisplay.get_virtual_size())
  61.         virtual_x = theDisplay.get_virtual_size();
  62.     else
  63.         virtual_x = new_x;
  64.  
  65.     // set real co-ordinate
  66.     x = theDisplay.x_to_real(virtual_x);
  67. }
  68.  
  69. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  70.     change real Y co-ordinate. if out of legal range, force to max.
  71. */
  72. void    Point::set_y(const int new_y)
  73. {
  74.     // check range
  75.     if (new_y > theDisplay.get_max_y())
  76.         y = theDisplay.get_max_y();
  77.     else
  78.         y = new_y;
  79.  
  80.     // set virtual co-ordinate
  81.     virtual_y = theDisplay.y_to_virtual(y);
  82. }
  83.  
  84. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  85.     change virtual Y co-ordinate. if out of legal range, force to max.
  86. */
  87. void    Point::set_y(const double new_y)
  88. {
  89.     // check range
  90.     if (new_y > theDisplay.get_virtual_size())
  91.         virtual_y = theDisplay.get_virtual_size();
  92.     else
  93.         virtual_y = new_y;
  94.  
  95.     // set real co-ordinate
  96.     y = theDisplay.y_to_real(virtual_y);
  97. }
  98.  
  99. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  100.     change both real co-ordinates. if out of legal range, force them to max.
  101.     then set the virtual world co-ordinates.
  102. */
  103. void    Point::set_xy(const int new_x, const int new_y)
  104. {
  105.     // check range
  106.     if (new_x > theDisplay.get_max_x())
  107.         x = theDisplay.get_max_x();
  108.     else
  109.         x = new_x;
  110.     if (new_y > theDisplay.get_max_y())
  111.         y = theDisplay.get_max_y();
  112.     else
  113.         y = new_y;
  114.  
  115.     // update virtual co-ordinates
  116.     virtual_x = theDisplay.x_to_virtual(x);
  117.     virtual_y = theDisplay.y_to_virtual(y);
  118. }
  119.  
  120. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  121.     change both virtual co-ordinates. if out of legal range, force them to
  122.     max. then set the real co-ordinates.
  123. */
  124. void    Point::set_xy(const double new_x, const double new_y)
  125. {
  126.     // check range
  127.     if (new_x > theDisplay.get_virtual_size())
  128.         virtual_x = theDisplay.get_virtual_size();
  129.     else
  130.         virtual_x = new_x;
  131.     if (new_y > theDisplay.get_virtual_size())
  132.         virtual_y = theDisplay.get_virtual_size();
  133.     else
  134.         virtual_y = new_y;
  135.  
  136.     // update real co-ordinates
  137.     x = theDisplay.x_to_real(virtual_x);
  138.     y = theDisplay.y_to_real(virtual_y);
  139. }
  140.  
  141. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  142.     add one point to another. if the result is out of legal range,
  143.     force the result to max.
  144. */
  145. Point   &Point::operator+=(Point &addend)
  146. {
  147.     if ((virtual_x += addend.get_virtual_x()) > theDisplay.get_virtual_size())
  148.         virtual_x = theDisplay.get_virtual_size();
  149.     if ((virtual_y += addend.get_virtual_y()) > theDisplay.get_virtual_size())
  150.         virtual_y = theDisplay.get_virtual_size();
  151.     x = theDisplay.x_to_real(virtual_x);
  152.     y = theDisplay.y_to_real(virtual_y);
  153.     return *this;
  154. }
  155.  
  156. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  157.     subtract one point from another. if the result is out of legal range,
  158.     force the result to 0.
  159. */
  160. Point   &Point::operator-=(Point &subtrahend)
  161. {
  162.     if ((virtual_x -= subtrahend.get_virtual_x()) < 0.0)
  163.         virtual_x = 0.0;
  164.     if ((virtual_y -= subtrahend.get_virtual_y()) > 0.0)
  165.         virtual_y = 0.0;
  166.     x = theDisplay.x_to_real(virtual_x);
  167.     y = theDisplay.y_to_real(virtual_y);
  168.     return *this;
  169. }
  170.  
  171. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  172.     compare two points and is both co-ordinates are equal return TRUE.
  173. */
  174. Bool    Point::operator==(Point &comparator)
  175. {
  176.     if (x == comparator.get_x())
  177.         if (y == comparator.get_y())
  178.            return TRUE;
  179.     return FALSE;
  180. }
  181.  
  182. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  183.     compare two points and if this point is to the right of or below the
  184.     comparator then return TRUE.
  185. */
  186. Bool    Point::operator>(Point &comparator)
  187. {
  188.     if ((x >= comparator.get_x()) && (y > comparator.get_y()))
  189.         return TRUE;
  190.     else
  191.         if ((x > comparator.get_x()) && (y >= comparator.get_y()))
  192.             return TRUE;
  193.     return FALSE;
  194. }
  195.  
  196. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  197.     compare two points and if this point is to the left of or above the
  198.     comparator then return TRUE.
  199. */
  200. Bool    Point::operator<(Point &comparator)
  201. {
  202.     if ((x <= comparator.get_x()) && (y < comparator.get_y()))
  203.         return TRUE;
  204.     else
  205.         if ((x < comparator.get_x()) && (y <= comparator.get_y()))
  206.             return TRUE;
  207.     return FALSE;
  208. }
  209.  
  210. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  211.     compare two points and if this point is to the left of or above or equal
  212.     to the comparator then return TRUE.
  213. */
  214. Bool    Point::operator<=(Point &comparator)
  215. {
  216.     if ((x <= comparator.get_x()) && (y <= comparator.get_y()))
  217.         return TRUE;
  218.     return FALSE;
  219. }
  220.  
  221. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  222.     compare two points and if this point is to the right of or below or equal
  223.     to the comparator then return TRUE.
  224. */
  225. Bool    Point::operator>=(Point &comparator)
  226. {
  227.     if ((x >= comparator.get_x()) && (y >= comparator.get_y()))
  228.         return TRUE;
  229.     return FALSE;
  230. }
  231.  
  232. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  233.     this is a friend of the Point class. it adds two point s together and
  234.     returns the result as a third Point.
  235. */
  236. Point   &operator+(Point &augend, Point &addend)
  237. {
  238.     static Point   result;
  239.  
  240.     result.virtual_x = augend.virtual_x + addend.virtual_x;
  241.     result.virtual_y = augend.virtual_y + addend.virtual_y;
  242.     result.x = theDisplay.x_to_real(result.virtual_x);
  243.     result.y = theDisplay.y_to_real(result.virtual_y);
  244.     return result;
  245. }
  246.  
  247. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  248.     this is a friend of the Point class. it subtracts two points and
  249.     returns the result as a third Point.
  250. */
  251. Point   &operator-(Point &minuend, Point &subtrahend)
  252. {
  253.     static Point   result;
  254.  
  255.     result.virtual_x = minuend.virtual_x - subtrahend.virtual_x;
  256.     result.virtual_y = minuend.virtual_y - subtrahend.virtual_y;
  257.     result.x = theDisplay.x_to_real(result.virtual_x);
  258.     result.y = theDisplay.y_to_real(result.virtual_y);
  259.     return result;
  260. }
  261.