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

  1. /**************************************************************************
  2.     Listing 13  -   ARC.CPP
  3.  
  4.     Written by Kevin D. Weeks, April 1990
  5.     Released to the Public Domain
  6. */
  7.  
  8. #include "arc.hpp"
  9.  
  10. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  11.     Constructor - creates an empty instance.
  12. */
  13. Arc::Arc(void)
  14. {
  15.     struct linesettingstype line_type;
  16.  
  17.     // set attributes to 0 or current BGI defaults
  18.     start_angle = 0;
  19.     end_angle = 0;
  20.     radius = 0;
  21.     getlinesettings(&line_type);
  22.     width = (line_widths)line_type.thickness;
  23.     color = (COLORS)getcolor();
  24.     visible = FALSE;
  25. }
  26.  
  27. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  28.     Constructor - since the "copy constructor", Arc(const Arc &source),
  29.     won't accept a pointer easily so we define a separate pointer
  30.     constructor for convenience' sake.
  31. */
  32. Arc::Arc(const Arc *source)
  33. {
  34.     start_angle = source->start_angle;
  35.     end_angle = source->end_angle;
  36.     radius = source->radius;
  37.     center = source->center;
  38.     width = source->width;
  39.     color = source->color;
  40.     visible = FALSE;
  41. }
  42.  
  43. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  44.     Constructor - primary constructor. Copies of all attributes are
  45.     accepted although init_color and init_width may be defaults.
  46. */
  47. Arc::Arc(const int starting_angle, const int ending_angle,
  48.          const int init_radius, const Point &init_center,
  49.          const COLORS init_color, const line_widths init_width)
  50. {
  51.     struct linesettingstype line_type;
  52.  
  53.     if (init_width == DEFAULT)
  54.     {
  55.         getlinesettings(&line_type);
  56.         width = (line_widths)line_type.thickness;
  57.     }
  58.     else
  59.         width = init_width;
  60.  
  61.     if (init_color == DEFAULT)
  62.         color = (COLORS)getcolor();
  63.     else
  64.         color = init_color;
  65.  
  66.     start_angle = starting_angle;
  67.     end_angle = ending_angle;
  68.     radius = init_radius;
  69.     center = init_center;
  70.     visible = FALSE;
  71. }
  72.  
  73. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  74.     color can be set whether the line is visible or not
  75. */
  76. void    Arc::set_color(const COLORS new_color)
  77. {
  78.     if (new_color == DEFAULT)
  79.         color = (COLORS)getcolor();
  80.     else
  81.         color = new_color;
  82.     if (visible)
  83.     {
  84.         erase();
  85.         draw();
  86.     }
  87. }
  88.  
  89. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  90.     width can be set whether the line is visible or not
  91. */
  92. void    Arc::set_width(line_widths new_width)
  93. {
  94.     struct linesettingstype line_type;
  95.  
  96.     if (new_width == DEFAULT)
  97.     {
  98.         getlinesettings(&line_type);
  99.         new_width = (line_widths)line_type.thickness;
  100.     }
  101.     if (visible)
  102.     {
  103.         erase();                            // erase using old width
  104.         width = new_width;                  // change style
  105.         draw();                             // draw using new width
  106.     }
  107.     else
  108.         width = new_width;
  109. }
  110.  
  111. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  112.     the draw() method first preserves the current BGI settings, then
  113.     resets the BGI defaults for this particular object, and finally
  114.     restores the original settings. had we written the graphics routines
  115.     from scratch the step of preserving and then restoring the settings
  116.     wouldn't be neccessary.
  117. */
  118. void    Arc::draw(void)
  119. {
  120.     struct linesettingstype hold_current;
  121.     int    default_color;
  122.  
  123.     // don't draw it twice
  124.     if (visible)
  125.         return;
  126.  
  127.     // preserve current settings
  128.     getlinesettings(&hold_current);
  129.     default_color = getcolor();
  130.  
  131.     // select color and line style for this object and draw it
  132.     setcolor(color);
  133.     setlinestyle(hold_current.linestyle,0,width);
  134.     arc(center.get_x(),center.get_y(),start_angle,end_angle,radius);
  135.  
  136.     // restore original settings
  137.     setlinestyle(hold_current.linestyle,hold_current.upattern,
  138.                  hold_current.thickness);
  139.     setcolor(default_color);
  140.     visible = TRUE;
  141. }
  142.  
  143. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  144.     the erase method draws over the existing arc using the background
  145.     color. as with the draw method, the current BGI settings must be
  146.     preserved and then restored.
  147. */
  148. void    Arc::erase(void)
  149. {
  150.     struct linesettingstype hold_current;
  151.     int    default_color;
  152.  
  153.     // don't erase it if it isn't visible
  154.     if (!visible)
  155.         return;
  156.  
  157.     // preserve current settings
  158.     getlinesettings(&hold_current);
  159.     default_color = getcolor();
  160.  
  161.     // set the foreground current color to the background color and set the
  162.     // BGI defaults for this object and draw over it
  163.     setcolor(getbkcolor());
  164.     setlinestyle(hold_current.linestyle,0,width);
  165.     arc(center.get_x(),center.get_y(),start_angle,end_angle,radius);
  166.  
  167.     // restore the defaults
  168.     setlinestyle(hold_current.linestyle,hold_current.upattern,
  169.                  hold_current.thickness);
  170.     setcolor(default_color);
  171.     visible = FALSE;
  172. }
  173.  
  174. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  175. void    Arc::move_relative(Point &distance)
  176. {
  177.     erase();
  178.     center += distance;
  179.     draw();
  180. }
  181.  
  182. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  183. void    Arc::move_absolute(Point &new_location, Point &reference)
  184. {
  185.     erase();
  186.     center += new_location - reference;
  187.     draw();
  188. }
  189.  
  190. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  191.     this is the private method used by the copy constructor and the =
  192.     operator. Note that the visible attribute is forced to FALSE.
  193. */
  194. Arc    &Arc::copy(const Arc &source)
  195. {
  196.     start_angle = source.start_angle;
  197.     end_angle = source.end_angle;
  198.     radius = source.radius;
  199.     center = source.center;
  200.     width = source.width;
  201.     color = source.color;
  202.     visible = FALSE;
  203.     return *this;
  204. }
  205.