home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / Draw / Circle.m < prev    next >
Text File  |  1995-09-04  |  2KB  |  75 lines

  1. #import "draw.h"
  2.  
  3. /*
  4.  * This line is just a stub to get genstrings to generate
  5.  * a .strings file entry for the name of this type of Graphic.
  6.  * The name is used in the Undo New <Whatever> menu item.
  7.  *
  8.  * NSLocalString("Circle", NULL, "Name of the tool that draws ovals, i.e., the %@ of the New %@ operation.")
  9.  */
  10.  
  11. @implementation Circle : Graphic
  12.  
  13. - (Graphic *)colorAcceptorAt:(NSPoint)point
  14. /*
  15.  * An oval accepts a dropped color if the drop occurs
  16.  * within the bounds of the circle itself.
  17.  */
  18. {
  19.     if ([self hit:point]) {
  20.         return self;
  21.     } else {
  22.         return nil;
  23.     }
  24. }
  25.  
  26. - (float)naturalAspectRatio
  27. /*
  28.  * The natural aspect ratio of an oval is 1.0 (a circle).
  29.  */
  30. {
  31.     return 1.0;
  32. }
  33.  
  34. - draw
  35. {
  36.     if (bounds.size.width >= 1.0 && bounds.size.height >= 1.0) {
  37.         if ([self fill]) {
  38.             PSgsave();
  39.             [self setFillColor];
  40.             PSFilledOval(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
  41.             PSgrestore();
  42.         }
  43.         if (!gFlags.nooutline) {
  44.             [self setLineColor];
  45.             PSFramedOval(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
  46.         }
  47.     }
  48.     return self;
  49. }
  50.  
  51. - (BOOL)hit:(NSPoint)p
  52. /*
  53.  * Hit only if inside the interior of the oval.
  54.  */
  55. {
  56.     float x, y;
  57.     NSPoint center;
  58.     double angle, radius, diameter;
  59.  
  60.     if ([super hit:p]) {
  61.     center.x = bounds.origin.x + bounds.size.width / 2.0;
  62.     center.y = bounds.origin.y + bounds.size.height / 2.0;
  63.     diameter = MIN(bounds.size.width, bounds.size.height);
  64.     x = fabs(center.x - p.x) / (bounds.size.width / diameter);
  65.     y = fabs(center.y - p.y) / (bounds.size.height / diameter);
  66.     angle = atan2(y, x);
  67.     radius = diameter / 2.0;
  68.     return(x < radius * cos(angle) && y < radius * sin(angle));
  69.     } else {
  70.     return NO;
  71.     }
  72. }
  73.  
  74. @end
  75.