home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / IP_Graph3D / GraphView.m < prev    next >
Encoding:
Text File  |  1991-10-10  |  9.1 KB  |  325 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "GraphView.h"
  5. #import "GraphController.h"
  6. #import <math.h>
  7. #import <dpsclient/wraps.h>
  8. #import <appkit/appkit.h>
  9. #import "DrawPoly.h"
  10.  
  11. float sinTheta=0.0,cosTheta=0.0,sinPhi=0.0,cosPhi=0.0, perspective=0.0, vanish=0.0, halfSize=0.0;
  12. int xSlider, ySlider, size=0;
  13. float mouseScale=0.4;
  14. float xStart=0.0, yStart=0.0;
  15.  
  16.  
  17. void Rotate(px, py, pz)
  18.     float *px, *py, *pz;
  19. {
  20.     float xt=0.0, yt=0.0;
  21.  
  22.     xt = *pz * sinTheta + *px * cosTheta;
  23.     *pz = *pz * cosTheta - *px * sinTheta;
  24.     *px = xt;
  25.  
  26.     yt = *py * cosPhi - *pz * sinPhi;
  27.     *pz = *py * sinPhi + *pz * cosPhi;
  28.     *py = yt;
  29.  
  30.     xt = (vanish-*pz * perspective) / vanish;
  31.     *px = *px * xt;
  32.     *py = *py * xt;
  33. }
  34.  
  35. void DrawPolygon(curPoly)
  36.     PointThreeD curPoly[];
  37. {
  38.     Rotate(&curPoly[0].x, &curPoly[0].y, &curPoly[0].z);
  39.     Rotate(&curPoly[1].x, &curPoly[1].y, &curPoly[1].z);
  40.     Rotate(&curPoly[2].x, &curPoly[2].y, &curPoly[2].z);
  41.  
  42.     DrawPoly3(curPoly[0].x+xStart, curPoly[0].y+yStart,
  43.           curPoly[1].x+xStart, curPoly[1].y+yStart,
  44.           curPoly[2].x+xStart, curPoly[2].y+yStart);
  45. }
  46.  
  47. void FillPolygon(curPoly, gray)
  48.     PointThreeD curPoly[];
  49.     float gray;
  50. {
  51.     Rotate(&curPoly[0].x, &curPoly[0].y, &curPoly[0].z);
  52.     Rotate(&curPoly[1].x, &curPoly[1].y, &curPoly[1].z);
  53.     Rotate(&curPoly[2].x, &curPoly[2].y, &curPoly[2].z);
  54.  
  55.     FillPoly3(curPoly[0].x+xStart, curPoly[0].y+yStart,
  56.           curPoly[1].x+xStart, curPoly[1].y+yStart,
  57.           curPoly[2].x+xStart, curPoly[2].y+yStart, gray);
  58. }
  59.  
  60.  
  61. void DrawPolygon4(curPoly)
  62.     PointThreeD curPoly[];
  63. {
  64.     Rotate(&curPoly[0].x, &curPoly[0].y, &curPoly[0].z);
  65.     Rotate(&curPoly[1].x, &curPoly[1].y, &curPoly[1].z);
  66.     Rotate(&curPoly[2].x, &curPoly[2].y, &curPoly[2].z);
  67.     Rotate(&curPoly[3].x, &curPoly[3].y, &curPoly[3].z);
  68.  
  69.     DrawPoly4(curPoly[0].x+xStart, curPoly[0].y+yStart,
  70.           curPoly[1].x+xStart, curPoly[1].y+yStart,
  71.           curPoly[2].x+xStart, curPoly[2].y+yStart,
  72.           curPoly[3].x+xStart, curPoly[3].y+yStart);
  73. }
  74.  
  75.  
  76. /***************************************************************/
  77.  
  78. @implementation GraphView
  79.  
  80. - initFrame:(const NXRect *)r
  81. {
  82.     [super initFrame:r];
  83.     yScale = 0.1;
  84.     xSize=r->size.width;
  85.     ySize=r->size.height;
  86.     xStart = xSize / 2;
  87.     yStart = ySize / 2;
  88.     return self;
  89. }
  90.  
  91. - drawSelf:(const NXRect *)r: (int)c;
  92. {
  93.     PSsetgray(1.0);
  94.     NXRectFill(r);
  95.  
  96.     [self drawGraph:[graphControllerPtr readDetail]:
  97.                 [graphControllerPtr readTheta]:
  98.                 [graphControllerPtr readPhi]:
  99.                 [graphControllerPtr readGraphArray]];
  100.  
  101.     [self drawBoundingBox: [graphControllerPtr readTheta]: [graphControllerPtr readPhi]];
  102. /*    [self drawAxes: [graphControllerPtr readTheta]: [graphControllerPtr readPhi]];
  103. */
  104.     return self;
  105. }
  106.  
  107. - drawAxes: (int)theta: (int)phi
  108. {
  109.     PointThreeD curPoly[3];
  110.     float x1=0,y1=0,z1=0,x2=0,y2=0,z2=0;
  111.  
  112.     sinTheta = sin(theta/180.0*PI);
  113.     sinPhi = sin(-phi/180.0*PI);
  114.     cosTheta = cos(theta/180.0*PI);
  115.     cosPhi = cos(-phi/180.0*PI);
  116.  
  117.     PSsetgray(0);
  118.     PSsetlinewidth(2);
  119.     
  120.     x1 = -halfSize; y1 = -halfSize; z1 = -halfSize;
  121.     x2 =  halfSize; y2 = -halfSize; z2 = -halfSize;
  122.     Rotate(&x1, &y1, &z1);
  123.     Rotate(&x2, &y2, &z2);
  124.     PSmoveto(x1+xStart, y1+yStart);
  125.     PSlineto(x2+xStart, y2+yStart);
  126.     PSstroke();
  127.     PSmoveto(x2*.9+xStart, y2*.9+yStart);
  128.     PSshow("X");
  129.  
  130.     x1 = -halfSize; y1 = -halfSize; z1 = -halfSize;
  131.     x2 = -halfSize; y2 =  halfSize; z2 = -halfSize;
  132.     Rotate(&x1, &y1, &z1);
  133.     Rotate(&x2, &y2, &z2);
  134.     PSmoveto(x1+xStart, y1+yStart);
  135.     PSlineto(x2+xStart, y2+yStart);
  136.     PSstroke();
  137.     PSmoveto(x2*.9+xStart, y2*.9+yStart);
  138.     PSshow("Y");
  139.  
  140.     x1 = -halfSize; y1 = -halfSize; z1 = -halfSize;
  141.     x2 = -halfSize; y2 = -halfSize; z2 =  halfSize;
  142.     Rotate(&x1, &y1, &z1);
  143.     Rotate(&x2, &y2, &z2);
  144.     PSmoveto(x1+xStart, y1+yStart);
  145.     PSlineto(x2+xStart, y2+yStart);
  146.     PSstroke();
  147.     PSmoveto(x2*.9+xStart, y2*.9+yStart);
  148.     PSshow("Z");
  149.  
  150.     curPoly[0].x = halfSize;     curPoly[0].y =  -halfSize;     curPoly[0].z = -halfSize;
  151.     curPoly[1].x = halfSize*.9;  curPoly[1].y = -halfSize*.97;  curPoly[1].z = -halfSize;
  152.     curPoly[2].x = halfSize*.9;  curPoly[2].y = -halfSize*1.03; curPoly[2].z = -halfSize;
  153.     FillPolygon(curPoly,0);
  154.  
  155.     curPoly[0].x = -halfSize;   curPoly[0].y = halfSize;     curPoly[0].z = -halfSize;
  156.     curPoly[1].x = -halfSize;   curPoly[1].y = halfSize*.9;  curPoly[1].z = -halfSize*.97;
  157.     curPoly[2].x = -halfSize;   curPoly[2].y = halfSize*.9;  curPoly[2].z = -halfSize*1.03;
  158.     FillPolygon(curPoly,0);
  159.  
  160.     curPoly[0].x = -halfSize;      curPoly[0].y = -halfSize;  curPoly[0].z = halfSize;
  161.     curPoly[1].x = -halfSize*.97;  curPoly[1].y = -halfSize;  curPoly[1].z = halfSize*.9;
  162.     curPoly[2].x = -halfSize*1.03; curPoly[2].y = -halfSize;  curPoly[2].z = halfSize*.9;
  163.     FillPolygon(curPoly,0);
  164.  
  165.     
  166.     curPoly[0].x = halfSize;     curPoly[0].y =  -halfSize;     curPoly[0].z = -halfSize;
  167.     curPoly[1].x = halfSize*.9;  curPoly[1].y = -halfSize;  curPoly[1].z = -halfSize*.97;
  168.     curPoly[2].x = halfSize*.9;  curPoly[2].y = -halfSize; curPoly[2].z = -halfSize*1.03;
  169.     FillPolygon(curPoly,0);
  170.  
  171.     curPoly[0].x = -halfSize;   curPoly[0].y = halfSize;     curPoly[0].z = -halfSize;
  172.     curPoly[1].x = -halfSize*.97;   curPoly[1].y = halfSize*.9;  curPoly[1].z = -halfSize;
  173.     curPoly[2].x = -halfSize*1.03;   curPoly[2].y = halfSize*.9;  curPoly[2].z = -halfSize;
  174.     FillPolygon(curPoly,0);
  175.  
  176.     curPoly[0].x = -halfSize;      curPoly[0].y = -halfSize;  curPoly[0].z = halfSize;
  177.     curPoly[1].x = -halfSize;  curPoly[1].y = -halfSize*.97;  curPoly[1].z = halfSize*.9;
  178.     curPoly[2].x = -halfSize; curPoly[2].y = -halfSize*1.03;  curPoly[2].z = halfSize*.9;
  179.     FillPolygon(curPoly,0);
  180.  
  181.  
  182.     PSsetlinewidth(1);
  183.     return self;
  184. }
  185.  
  186.  
  187. - drawBoundingBox: (int)theta: (int)phi
  188. {
  189.     PointThreeD curPoly[4];
  190.  
  191.     sinTheta = sin(theta/180.0*PI);
  192.     sinPhi = sin(-phi/180.0*PI);
  193.     cosTheta = cos(theta/180.0*PI);
  194.     cosPhi = cos(-phi/180.0*PI);
  195.     
  196.     PSsetgray(0.333);
  197.  
  198.     curPoly[0].x = -halfSize; curPoly[0].y = -halfSize; curPoly[0].z = -halfSize;
  199.     curPoly[1].x =  halfSize; curPoly[1].y = -halfSize; curPoly[1].z = -halfSize;
  200.     curPoly[2].x =  halfSize; curPoly[2].y =  halfSize; curPoly[2].z = -halfSize;
  201.     curPoly[3].x = -halfSize; curPoly[3].y =  halfSize; curPoly[3].z = -halfSize;
  202.     DrawPolygon4(curPoly);
  203.  
  204.     curPoly[0].x = -halfSize; curPoly[0].y = -halfSize; curPoly[0].z =  halfSize;
  205.     curPoly[1].x =  halfSize; curPoly[1].y = -halfSize; curPoly[1].z =  halfSize;
  206.     curPoly[2].x =  halfSize; curPoly[2].y =  halfSize; curPoly[2].z =  halfSize;
  207.     curPoly[3].x = -halfSize; curPoly[3].y =  halfSize; curPoly[3].z =  halfSize;
  208.     DrawPolygon4(curPoly);
  209.  
  210.     curPoly[0].x = -halfSize; curPoly[0].y = -halfSize; curPoly[0].z = -halfSize;
  211.     curPoly[1].x = -halfSize; curPoly[1].y = -halfSize; curPoly[1].z =  halfSize;
  212.     curPoly[2].x = -halfSize; curPoly[2].y =  halfSize; curPoly[2].z =  halfSize;
  213.     curPoly[3].x = -halfSize; curPoly[3].y =  halfSize; curPoly[3].z = -halfSize;
  214.     DrawPolygon4(curPoly);
  215.  
  216.     curPoly[0].x =  halfSize; curPoly[0].y = -halfSize; curPoly[0].z = -halfSize;
  217.     curPoly[1].x =  halfSize; curPoly[1].y = -halfSize; curPoly[1].z =  halfSize;
  218.     curPoly[2].x =  halfSize; curPoly[2].y =  halfSize; curPoly[2].z =  halfSize;
  219.     curPoly[3].x =  halfSize; curPoly[3].y =  halfSize; curPoly[3].z = -halfSize;
  220.     DrawPolygon4(curPoly);
  221.  
  222.     return self;
  223. }
  224.  
  225. - drawGraph:(int)detail: (int)theta: (int)phi: (bigArray)graphArray
  226. {
  227.     int i=0,j=0, xCount=0, zCount=0;
  228.     PointThreeD curPoly[4];
  229.     
  230.     float partScale=0.0;
  231.     float count=0.0;
  232.     
  233.     count=PI/detail*4;
  234.  
  235.     sinTheta = sin(theta/180.0*PI);
  236.     sinPhi = sin(-phi/180.0*PI);
  237.     cosTheta = cos(theta/180.0*PI);
  238.     cosPhi = cos(-phi/180.0*PI);
  239.     size = [graphControllerPtr readSize];
  240.     perspective = [graphControllerPtr readPerspective];
  241.     halfSize = size / 2;
  242.     partScale=size / detail;
  243.     vanish = size;
  244.  
  245.     PSsetgray(0);
  246.     for (i=0; i<detail; i++)
  247.     {
  248.         zCount = i-detail / 2;
  249.         for (j=0; j<detail; j++)
  250.         {
  251.             xCount = j - detail / 2;
  252.             curPoly[0].x=xCount*partScale;
  253.             curPoly[0].y=sin(i*count)*cos(j*count)*partScale;
  254.             curPoly[0].z=zCount*partScale;
  255.             
  256.             curPoly[1].x=xCount*partScale;
  257.             curPoly[1].y=sin((i+1)*count)*cos(j*count)*partScale;
  258.             curPoly[1].z=(zCount+1)*partScale;
  259.  
  260.             curPoly[2].x=(xCount+1)*partScale;
  261.             curPoly[2].y=sin((i+1)*count)*cos((j+1)*count)*partScale;
  262.             curPoly[2].z=(zCount+1)*partScale;
  263.  
  264.             curPoly[3].x=(xCount+1)*partScale;
  265.             curPoly[3].y=sin(i*count)*cos((j+1)*count)*partScale;
  266.             curPoly[3].z=zCount*partScale;
  267.  
  268.             DrawPolygon4(curPoly);
  269.         }
  270.     }
  271.     
  272.     return self;
  273. }
  274.  
  275. - mouseDown:(NXEvent *)e
  276. {
  277.     NXPoint startPoint;
  278.     
  279.     oldMask = [window addToEventMask:NX_MOUSEDRAGGEDMASK];
  280.     startPoint = e->location;
  281.     xSlider = (int) (startPoint.x);
  282.     ySlider = (int) (startPoint.y);
  283.     return self;
  284. }    
  285.  
  286. - mouseDragged:(NXEvent *)e
  287. {
  288.     NXPoint    currentPoint;    
  289.     int tempX, tempY;
  290.  
  291.     currentPoint = e->location;
  292.     tempX = (int) currentPoint.x;
  293.     tempY = (int) currentPoint.y;
  294.     tempX = (int) (tempX - xSlider)*mouseScale;
  295.     tempY = (int) (tempY - ySlider)*mouseScale;
  296.  
  297.     if ((tempX != 0) || (tempY != 0))
  298.     {
  299.         tempX=[graphControllerPtr readTheta]+tempX;
  300.         if (tempX > 180) tempX=180;
  301.         if (tempX < -180) tempX=-180;
  302.         [graphControllerPtr writeTheta:tempX];
  303.         [graphControllerPtr slideThetaSlider:tempX];
  304.         tempY=[graphControllerPtr readPhi]+tempY;
  305.         if (tempY > 90) tempY=90;
  306.         if (tempY < -90) tempY=-90;
  307.         [graphControllerPtr writePhi:tempY];
  308.         [graphControllerPtr slidePhiSlider:tempY];
  309.         [self display];
  310.         NXPing();
  311.         xSlider = (int) currentPoint.x;
  312.         ySlider = (int) currentPoint.y;
  313.     }
  314.     return self;
  315. }
  316.  
  317. - mouseUp:(NXEvent *)e
  318. {
  319.     [window setEventMask:oldMask];
  320.     return self;
  321. }
  322.  
  323.  
  324. @end
  325.