home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d306 / rexxplplot.lha / RexxPlPlot / src / src.zoo / plnxtv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-06  |  7.6 KB  |  268 lines

  1. /* Draws the next view of a 3-d plot. The physical coordinates of */
  2. /* the points for the next view are placed in the n points of arrays  */
  3. /* u and v. The silhouette found so far is stored in the heap as a */
  4. /* set of m peak points. */
  5.  
  6. /* Define the heap size.  This can be increased if necessary.  Someday */
  7. /* I hope to just allocate a heap space of minimum required size.      */
  8. #define  heaps    4000
  9.  
  10. #include <stdio.h>
  11. #ifdef AZTEC_C
  12. extern char *malloc();
  13. #else
  14. #include <stdlib.h>
  15. #endif
  16. #include "plplot.h"
  17. #include "declare.h"
  18.  
  19. static int m, x, y;
  20.  
  21. void plnxtv(u,v,n,init)
  22. int u[], v[], n, init;
  23. {
  24.       int xx;
  25.       int i, j, first;
  26.       int sx1, sx2, sy1, sy2;
  27.       int su1, su2, sv1, sv2;
  28.       int cx, cy, px, py;
  29.       int seg, ptold, lstold, pthi, pnewhi, newhi, change, ochange;
  30.       
  31.       first = 1;
  32.       pnewhi = 0;
  33.       
  34.       /* For the initial set of points, just display them and store them as */
  35.       /* the peak points. */
  36.  
  37.       if (init == 1) {
  38.          if( heap3 == NULL)  /* heap not yet allocated so ... */
  39.             if(( heap3 = (int *)malloc((unsigned)(heaps*sizeof(int)))) == NULL)
  40.                fatal("Not enough heap space in PLNXTV.");
  41.          y = heaps - n;
  42.          x = y - n;
  43.          movphy(u[0],v[0]);
  44.          heap3[x] = u[0];
  45.          heap3[y] = v[0];
  46.          for (i=1; i<n; i++){
  47.            draphy(u[i],v[i]);
  48.            heap3[x+i] = u[i];
  49.            heap3[y+i] = v[i];
  50.          }
  51.          m = n;
  52.          return;
  53.       }
  54.  
  55.       /* Otherwise, we need to consider hidden-line removal problem. We scan */
  56.       /* over the points in both the old (i.e. heap3[x...] and heap3[y...]) and */
  57.       /* new (i.e. u[] and v[]) arrays in order of increasing x coordinate. */
  58.       /* At each stage, we find the line segment in the other array (if one */
  59.       /* exists) that straddles the x coordinate of the point. We */
  60.       /* have to determine if the point lies above or below the line segment, */
  61.       /* and to check if the below/above status has changed since the last */
  62.       /* point. */
  63.  
  64.       /* If init = 2 we do not update the view, this is useful for drawing */
  65.       /* lines on the graph after we are done plotting points.  Hidden line */
  66.       /* removal is still done, but the view is not updated. */
  67.       xx = 0;
  68.       i = 0;
  69.       j = 0;
  70.  
  71.       /* (heap3[x+i], heap3[y+i]) is the i'th point in the old array */
  72.       /* (u[j], v[j])           is the j'th point in the new array */
  73.  
  74.       while (i < m || j < n) {
  75.  
  76.         /* The coordinates of the point under consideration are (px,py). */
  77.         /* The line segment joins (sx1,sy1) to (sx2,sy2). */
  78.  
  79.         /* "ptold" is true if the point lies in the old array. We set it */
  80.         /*  by comparing the x coordinates of the i'th old point */
  81.         /*  and the j'th new point, being careful if we have fallen past */
  82.         /*  the edges. Having found the point, load up the point and */
  83.         /*  segment coordinates appropriately. */
  84.  
  85.         ptold = ((heap3[x+i] < u[j] && i<m) || j>= n);
  86.         if (ptold) {
  87.           px = heap3[x+i];
  88.           py = heap3[y+i];
  89.           seg = j>0 && j<n;
  90.           if (seg) {
  91.             sx1 = u[j-1];
  92.             sy1 = v[j-1];
  93.             sx2 = u[j];
  94.             sy2 = v[j];
  95.           }
  96.         }
  97.         else {
  98.           px = u[j];
  99.           py = v[j];
  100.           seg = i>0 && i<m;
  101.           if (seg) {
  102.             sx1 = heap3[x+i-1];
  103.             sy1 = heap3[y+i-1];
  104.             sx2 = heap3[x+i];
  105.             sy2 = heap3[y+i];
  106.           }
  107.         }
  108.  
  109.         /* Now determine if the point is higher than the segment, using the */
  110.         /* logical function "above". We also need to know if it is */
  111.         /* the old view or the new view that is higher. "newhi" is set true */
  112.         /* if the new view is higher than the old */
  113.  
  114.         if(seg)
  115.             pthi = plabv(px,py,sx1,sy1,sx2,sy2);
  116.         else
  117.             pthi = 1;
  118.  
  119.         newhi = (ptold && !pthi) || (!ptold && pthi);
  120.         change = (newhi && !pnewhi) || (!newhi && pnewhi);
  121.  
  122.         /* There is a new intersection point to put in the peak array if the */
  123.         /* state of "newhi" changes */
  124.  
  125.         if (first) {
  126.           movphy(px,py);
  127.           first = 0;
  128.           lstold = ptold;
  129.           if (init != 2) {
  130.             heap3[xx] = px;
  131.             heap3[xx+1] = py;
  132.             xx = xx + 2;
  133.           }
  134.           pthi = 0;
  135.           ochange = 0;
  136.         }
  137.         else if (change) {
  138.           /* Take care of special cases at end of arrays.  If init is 2 the */
  139.           /* endpoints are not connected to the old view. */
  140.           if (init==2 && ((!ptold && j==0)||(ptold && i==0))) {
  141.             movphy(px,py);
  142.             lstold = ptold;
  143.             pthi = 0;
  144.             ochange = 0;
  145.           }
  146.           else if (init==2 && ((!ptold && i>=m)||(ptold && j>=n))) {
  147.             movphy(px,py);
  148.             lstold = ptold;
  149.             pthi = 0;
  150.             ochange = 0;
  151.           }
  152.           /* If init is not 2 then we do want to connect the current line */
  153.           /* with the previous view at the endpoints.  */
  154.           /* Also find intersection point with old view. */
  155.           else  {
  156.             if (i == 0) {
  157.               sx1 = heap3[x];
  158.               sy1 = -1;
  159.               sx2 = heap3[x];
  160.               sy2 = heap3[y];
  161.             }
  162.             else if (i >= m) {
  163.               sx1 = heap3[x+m-1];
  164.               sy1 = heap3[y+m-1];
  165.               sx2 = heap3[x+m-1];
  166.               sy2 = -1;
  167.             }
  168.             else {
  169.               sx1 = heap3[x+i-1];
  170.               sy1 = heap3[y+i-1];
  171.               sx2 = heap3[x+i];
  172.               sy2 = heap3[y+i];
  173.             }
  174.  
  175.             if (j == 0) {
  176.               su1 = u[0];
  177.               sv1 = -1;
  178.               su2 = u[0];
  179.               sv2 = v[0];
  180.             }
  181.             else if (j >= n) {
  182.               su1 = u[n-1];
  183.               sv1 = v[n-1];
  184.               su2 = u[n];
  185.               sv2 = -1;
  186.             }
  187.             else  {
  188.               su1 = u[j-1];
  189.               sv1 = v[j-1];
  190.               su2 = u[j];
  191.               sv2 = v[j];
  192.             }
  193.  
  194.             /* Determine the intersection */
  195.             pl3cut(sx1,sy1,sx2,sy2,su1,sv1,su2,sv2,&cx,&cy);
  196.             if (cx == px && cy == py)  {
  197.               if (lstold && !ochange)
  198.                 movphy(px,py);
  199.               else 
  200.                 draphy(px,py); 
  201.  
  202.               if (init != 2) {
  203.                 heap3[xx] = px;
  204.                 heap3[xx+1] = py;
  205.                 xx = xx+2;
  206.               }
  207.               lstold = 1;
  208.               pthi = 0;
  209.             }
  210.             else {
  211.               if (lstold && !ochange)
  212.                  movphy(cx,cy);
  213.               else
  214.                  draphy(cx,cy);
  215.  
  216.               lstold = 1;
  217.               if (init != 2) {
  218.                 heap3[xx] = cx;
  219.                 heap3[xx+1] = cy;
  220.                 xx = xx+2;
  221.               }
  222.             }
  223.             ochange =1;
  224.           }
  225.         }
  226.  
  227.         /* If point is high then draw plot to point and update view. */
  228.         if (pthi) {
  229.           if (lstold && ptold)
  230.             movphy(px,py);
  231.           else 
  232.             draphy(px,py); 
  233.  
  234.           if (init != 2) {
  235.             heap3[xx] = px;
  236.             heap3[xx+1] = py;
  237.             xx = xx+2;
  238.           }
  239.           lstold = ptold;
  240.           ochange = 0;
  241.         }
  242.  
  243.         pnewhi = newhi;
  244.  
  245.         if (ptold)
  246.           i = i+1;
  247.         else
  248.           j = j+1;
  249.  
  250.         if (xx>=x) fatal("Heap overflow in plnxtv.");
  251.       }
  252.  
  253.       /* Transfer the peak points to the RHS of the heap */
  254.  
  255.       if(init != 2) {
  256.         m = xx/2;
  257.         xx = 0;
  258.         y = heaps - m;
  259.         x = y - m;
  260.         for(i=0; i<m; i++) {
  261.           heap3[x+i] = heap3[xx];
  262.           heap3[y+i] = heap3[xx+1];
  263.           xx = xx+2;
  264.         }
  265.       }
  266. }
  267.  
  268.