home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Plotting / QuickPlot / Source / PlotView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  5.3 KB  |  289 lines

  1.  
  2. /*    Program By Fred Richards, Copyright 1990.    */
  3. /*                            */
  4. /*    This software is provided free of charge    */
  5. /*    and without warranty.  The source code may    */
  6. /*    be distributed freely provided this copyright    */
  7. /*    notice remains in tact.                */
  8.  
  9.  
  10. #import "PlotView.h"
  11. #import "Plot.h"
  12.  
  13. #define SQRT3    1.73205
  14.  
  15. @implementation PlotView
  16.  
  17.  
  18. - setPlotParam:anObject
  19. {
  20.     plotParam = anObject;
  21.     return self;
  22. }
  23.  
  24. - initPlot:sender
  25. {
  26.     PSsetlinewidth(0.0);
  27.     [self setPointSize];
  28.     [self setDrawColor:NX_BLACK];
  29.     [self clear:sender];
  30.     PSrectclip([plotParam xmin:self]-(pointSize.x/100.0),
  31.            [plotParam ymin:self]-(pointSize.y/100.0),
  32.            [plotParam xmax:self]-[plotParam xmin:self]+(pointSize.x/50.0),
  33.            [plotParam ymax:self]-[plotParam ymin:self]+(pointSize.y/50.0));
  34.     return self;
  35. }
  36.  
  37. - clear:sender
  38. {
  39.     NXEraseRect(&bounds);
  40.     return self;
  41. }
  42.  
  43. // Draw lines between all the data points given
  44.  
  45. - drawLines:sender
  46. {
  47.     int        i,
  48.         count = [sender dSize];
  49.     NXPoint    *data = [sender data];
  50.  
  51.     /* Buffer this so that a single path can */
  52.     /* contain up to 512 points. */
  53.  
  54.     PSnewpath();
  55.     PSmoveto(data[0].x, data[0].y);
  56.     for (i = 1; i < count; i++)  {
  57.       if ((i % 512) == 0)  {
  58.     PSstroke();
  59.     PSnewpath();
  60.     PSmoveto(data[i-1].x, data[i-1].y);
  61.       }
  62.       PSlineto(data[i].x, data[i].y);
  63.     }
  64.     PSstroke();
  65.     return self;
  66. }
  67.  
  68.  
  69. - drawPoints:sender
  70. {
  71.     int        i,
  72.         count = [sender dSize];
  73.     NXPoint    *data = [sender data];
  74.  
  75.     for (i = 0; i < count; i++)
  76.       [self dotAt:data[i]];
  77.     return self;
  78. }
  79.  
  80. - drawCrosses:sender
  81. {
  82.     int        i,
  83.         count = [sender dSize];
  84.     NXPoint    *data = [sender data];
  85.  
  86.     for (i = 0; i < count; i++)
  87.       [self crossAt:data[i]];
  88.     return self;
  89. }
  90.  
  91. - drawXs:sender
  92. {
  93.     int        i,
  94.         count = [sender dSize];
  95.     NXPoint    *data = [sender data];
  96.  
  97.     for (i = 0; i < count; i++)
  98.       [self xAt:data[i]];
  99.     return self;
  100. }
  101.  
  102. - drawBoxes:sender
  103. {
  104.     int        i,
  105.         count = [sender dSize];
  106.     NXPoint    *data = [sender data];
  107.  
  108.     for (i = 0; i < count; i++)
  109.       [self boxAt:data[i]];
  110.     return self;
  111. }
  112.  
  113. - drawTriangles:sender
  114. {
  115.     int        i,
  116.         count = [sender dSize];
  117.     NXPoint    *data = [sender data];
  118.  
  119.     for (i = 0; i < count; i++)
  120.       [self triangleAt:data[i]];
  121.     return self;
  122. }
  123.  
  124.  
  125. - setDrawColor:(float )color;
  126. {
  127.     PSsetgray(color);
  128.     return self;
  129. }
  130.  
  131. - setPointSize
  132. {
  133.     pointSize.x = bounds.size.width / 50.0;
  134.     pointSize.y = bounds.size.height / 50.0;
  135.  
  136.     pointOffset.x = pointSize.x/2.0;
  137.     pointOffset.y = pointSize.y/2.0;
  138.  
  139.     return self;
  140. }
  141.  
  142. - dotAt:(NXPoint )place
  143. {
  144.     PSnewpath();
  145.     PSmoveto(place.x, place.y);
  146.     PSlineto(place.x, place.y);
  147.     PSstroke();
  148.     return self;
  149. }
  150.  
  151. - crossAt:(NXPoint )place
  152. {
  153.     PSnewpath();
  154.     PSmoveto(place.x-pointOffset.x, place.y);
  155.     PSrlineto(pointSize.x, 0.0);
  156.     PSmoveto(place.x, place.y-pointOffset.y);
  157.     PSrlineto(0.0, pointSize.y);
  158.     PSstroke();
  159.     return self;
  160. }
  161.  
  162. - xAt:(NXPoint )place
  163. {
  164.     PSnewpath();
  165.     PSmoveto(place.x-pointOffset.x, place.y-pointOffset.y);
  166.     PSrlineto(pointSize.x, pointSize.y);
  167.     PSrmoveto(-pointSize.x, 0.0);
  168.     PSrlineto(pointSize.x, -pointSize.y);
  169.     PSstroke();
  170.     return self;
  171. }
  172.  
  173. - boxAt:(NXPoint )place
  174. {
  175.     PSnewpath();
  176.     PSmoveto(place.x-pointOffset.x, place.y-pointOffset.y);
  177.     PSrlineto(pointSize.x, 0.0);
  178.     PSrlineto(0.0, pointSize.y);
  179.     PSrlineto(-pointSize.x, 0.0);
  180.     PSclosepath();
  181.     PSstroke();
  182.     return self;
  183. }
  184.  
  185. - triangleAt:(NXPoint )place
  186. {
  187.     PSnewpath();
  188.     PSmoveto(place.x-pointOffset.x, place.y-(pointOffset.y/SQRT3));
  189.     PSrlineto(pointSize.x, 0.0);
  190.     PSrlineto(-pointOffset.x, pointSize.y);
  191.     PSclosepath();
  192.     PSstroke();
  193.     return self;
  194. }
  195.  
  196. - border
  197. {
  198.     PSnewpath();
  199.     PSsetgray(NX_BLACK);
  200.     PSsetlinewidth(0.0);
  201.  
  202.     PSmoveto([plotParam xmin:self], [plotParam ymin:self]);
  203.     PSlineto([plotParam xmax:self], [plotParam ymin:self]);
  204.     PSlineto([plotParam xmax:self], [plotParam ymax:self]);
  205.     PSlineto([plotParam xmin:self], [plotParam ymax:self]);
  206.     PSclosepath();
  207.  
  208.     PSstroke();
  209.     
  210.     return self;
  211. }
  212.  
  213. - axes
  214. {
  215.     PSnewpath();
  216.     PSsetgray(NX_BLACK);
  217.     PSsetlinewidth(0.0);
  218.  
  219.     PSmoveto([plotParam xmin:self], 0.0);
  220.     PSlineto([plotParam xmax:self], 0.0);
  221.     PSmoveto(0.0, [plotParam ymin:self]);
  222.     PSlineto(0.0, [plotParam ymax:self]);
  223.  
  224.     PSstroke();
  225.  
  226.     return self;
  227. }
  228.  
  229. - drawSelf:(const NXRect *)rects :(int )rectCount
  230. {
  231.     if (![plotParam data])
  232.       return self;
  233.  
  234.     [self initPlot:self];
  235.  
  236.     if ([plotParam border:self])
  237.       [self border];
  238.     if ([plotParam axes:self])
  239.       [self axes];
  240.  
  241.     [[self window] flushWindow];
  242.  
  243.     if ([plotParam lines:self])
  244.       [self drawLines:plotParam];
  245.  
  246.     switch ([plotParam style:self])  {
  247.     case 0:
  248.       if (![plotParam lines:self])
  249.     [self drawPoints:plotParam];
  250.       break;
  251.     case 1:
  252.       [self drawCrosses:plotParam];
  253.       break;
  254.     case 2:
  255.       [self drawXs:plotParam];
  256.       break;
  257.     case 3:
  258.       [self drawBoxes:plotParam];
  259.       break;
  260.     case 4:
  261.       [self drawTriangles:plotParam];
  262.       break;      
  263.     }
  264.  
  265.     return self;
  266. }
  267.  
  268.  
  269. - savePSCode:(char *)aFile
  270. {
  271.     NXStream    *psStream;
  272.  
  273.     psStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
  274.     if (!psStream)  {
  275.       return self;
  276.     }
  277.     [self getBounds:&bounds];
  278.     [self copyPSCodeInside:&bounds to:psStream];
  279.        
  280.     NXFlush(psStream);
  281.     NXSaveToFile(psStream, aFile);
  282.     NXCloseMemory(psStream, NX_FREEBUFFER);
  283.  
  284.     return self;
  285. }
  286.  
  287.  
  288. @end
  289.