home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Educational / Dual / Source / PrimalView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  1.9 KB  |  74 lines

  1. // PrimalView.m
  2.  
  3. #import "PrimalView.h"
  4. #import "Controller.h"
  5.  
  6. @implementation PrimalView
  7.  
  8. - mouseDown: (NXEvent *)event
  9. {
  10.     NXPoint mouseLocation;
  11.     
  12.     mouseLocation = event->location;
  13.     [self convertPoint:&mouseLocation fromView:nil];
  14.     mouseLocation.x = xMin + mouseLocation.x / bounds.size.width * (xMax - xMin);
  15.     mouseLocation.y = yMin + mouseLocation.y / bounds.size.height * (yMax - yMin);
  16.     [controller addPrimal:mouseLocation];
  17.     return self;
  18. }
  19.  
  20. #define X(x) ((x - xMin) * sx)
  21. #define Y(y) ((y - yMin) * sy)
  22.  
  23. - drawSelf:(const NXRect *)rects :(int)rectCount
  24. {
  25.     PointLine *primalPoint, *primalLine;
  26.     int numberOfPrimalPoints, numberOfPrimalLines;
  27.     int i;
  28.     float sx = bounds.size.width / (xMax - xMin);
  29.     float sy = bounds.size.height / (yMax - yMin);
  30.     
  31.     [super drawSelf:rects :rectCount];
  32.  
  33.     // draw primal points
  34.     [controller getPrimalPoints:&numberOfPrimalPoints :&primalPoint];
  35.     for (i = 0; i < numberOfPrimalPoints; i++) {
  36.         NXSetColor(primalPoint[i].color);
  37.         if ([showLabels state]) {
  38.             PSmoveto(X(primalPoint[i].a), Y(primalPoint[i].b));
  39.             PSshow(primalPoint[i].label);
  40.         }
  41.         PSnewpath();
  42.         PSarc(X(primalPoint[i].a), Y(primalPoint[i].b), 2.0, 0.0, 360.0);
  43.         PSfill();
  44.     }
  45.  
  46.     // draw primal lines
  47.     [controller getPrimalLines:&numberOfPrimalLines :&primalLine];
  48.     for (i = 0; i < numberOfPrimalLines; i++) {
  49.         float x = xMin;
  50.         float y = x * primalLine[i].a + primalLine[i].b;
  51.         int dy = 0;        // used to adjust the position of the line label
  52.         if (y < yMin && primalLine[i].a) {
  53.             y = yMin;
  54.             x = (y - primalLine[i].b) / primalLine[i].a;
  55.         } else if (y > yMax && primalLine[i].a) {
  56.             y = yMax;
  57.             x = (y - primalLine[i].b) / primalLine[i].a;
  58.             dy = -12;
  59.         }
  60.         NXSetColor(primalLine[i].color);
  61.         if ([showLabels state]) {
  62.             PSmoveto(X(x), Y(y) + dy);
  63.             PSshow(primalLine[i].label);
  64.         }
  65.         PSmoveto(X(xMin), Y(xMin * primalLine[i].a + primalLine[i].b));
  66.         PSlineto(X(xMax), Y(xMax * primalLine[i].a + primalLine[i].b));
  67.         PSstroke();
  68.     }
  69.     
  70.     return self;
  71. }
  72.  
  73. @end
  74.