home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / imagetoo / imagetl1.lha / Imagetool / src+obj / draw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  4.5 KB  |  219 lines

  1. /* cat > headers/draw.h << "EOF" */
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  3. /* draw.h: header for draw.c file            */
  4. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  5. /* SCCS information: %W%    %G% - NCSA */
  6.  
  7. #define    draw_h        1
  8.  
  9. #include "all.h"
  10. #include "newext.h"
  11.  
  12.     static int *above;
  13.     static int *below;
  14.  
  15. /* EOF */
  16. /* cat > src+obj/draw/draw_done.c << "EOF" */
  17. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  18. /* draw_done: done drawing                */
  19. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  20. /* SCCS information: %W%    %G% - NCSA */
  21.  
  22. /* #include "draw.h" */
  23.  
  24. void 
  25. draw_done ()
  26. {
  27.     free (above);
  28.     free (below);
  29. }
  30. /* EOF */
  31. /* cat > src+obj/draw/draw_hline.c << "EOF" */
  32. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  33. /* draw_hline: Draw a line (may be just part of it    */
  34. /*           will show) and set the hiding boundaries */
  35. /*           accordingly. The two end points are    */
  36. /*           given in (x1,y1) and (x2,y2).        */
  37. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  38. /* SCCS information: %W%    %G% - NCSA */
  39.  
  40. /* #include "draw.h" */
  41.  
  42. void 
  43. draw_hline (x1, y1, x2, y2)
  44.     int             x1, y1, x2, y2;
  45. {
  46.     int             xpos, ypos, xi, yi, xj, yj;
  47.     int             i, xx, yy, tmp;
  48.     bool            abv, blw;
  49.     double          dy, real_yi;
  50.     void            intersect ();
  51.  
  52.     if (x1 == x2)
  53.     {        /* the vertical line is vertical */
  54.         if (y1 == y2)
  55.             return;
  56.         if (y1 < y2)
  57.         {
  58.             tmp = y1;
  59.             y1 = y2;
  60.             y2 = tmp;
  61.         }
  62.         if (y1 > above[x1])
  63.         {
  64.             plot_line (x1, y1, x1, MAX (y2, above[x1]));
  65.             above[x1] = y1;
  66.         }
  67.         if (y2 < below[x2])
  68.         {
  69.             plot_line (x1, MIN (y1, below[x1]), x1, y2);
  70.             below[x1] = y2;
  71.         }
  72.         return;
  73.     }
  74.  
  75.     if (x2 < x1)
  76.     {
  77.         tmp = x1;
  78.         x1 = x2;
  79.         x2 = tmp;
  80.         tmp = y1;
  81.         y1 = y2;
  82.         y2 = tmp;
  83.     }
  84.     dy = (double) (y2 - y1) / (double) (x2 - x1);
  85.     xi = x1;
  86.     yi = y1;
  87.     abv = blw = FALSE;
  88.     if (yi >= above[xi])
  89.     {
  90.         xpos = xi;
  91.         ypos = yi;
  92.         abv = TRUE;
  93.     }
  94.     if (yi <= below[xi])
  95.     {
  96.         xpos = xi;
  97.         ypos = yi;
  98.         blw = TRUE;
  99.     }
  100.     real_yi = (double) yi;
  101.  
  102.     do
  103.     {
  104.         if (abv)
  105.         {
  106.                 /* If hitting the boundary while approaching
  107.                    from above draw the line up to there. */
  108.             if (yi < above[xi])
  109.             {
  110.                 intersect (above, xj, yj, xi, yi, &xx, &yy);
  111.                 plot_line (xpos, ypos, xx, yy);
  112.                 abv = FALSE;
  113.             }
  114.             else
  115.                 above[xi] = yi;
  116.         }
  117.         if (blw)
  118.         {
  119.             if (yi > below[xi])
  120.             {
  121.                 intersect (below, xj, yj, xi, yi, &xx, &yy);
  122.                 plot_line (xpos, ypos, xx, yy);
  123.                 blw = FALSE;
  124.             }
  125.             else
  126.                 below[xi] = yi;
  127.         }
  128.         if (!abv && !blw)
  129.         {
  130.             if (yi >= above[xi])
  131.             {
  132.                 intersect (above, xj, yj, xi, yi, &xpos, &ypos);
  133.                 above[xi] = yi;
  134.                 abv = TRUE;
  135.             }
  136.             if (yi <= below[xi])
  137.             {
  138.                 intersect (below, xj, yj, xi, yi, &xpos, &ypos);
  139.                 below[xi] = yi;
  140.                 blw = TRUE;
  141.             }
  142.         }
  143.         xj = xi++;
  144.         yj = yi;
  145.         real_yi += dy;
  146.         yi = ROUND (real_yi);
  147.     } while (xi < x2);
  148.  
  149.     if (abv || blw)
  150.     {
  151.         plot_line (xpos, ypos, x2, y2);
  152.     }
  153. }
  154. /* EOF */
  155. /* cat > src+obj/draw/draw_init.c << "EOF" */
  156. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  157. /* draw_init: initialize drawing            */
  158. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  159. /* SCCS information: %W%    %G% - NCSA */
  160.  
  161. /* #include "draw.h" */
  162.  
  163. draw_init ()
  164. {
  165.     int             i;
  166.  
  167.     if ((above = (int *) malloc ((PRECISION + 1) * sizeof (int))) == NULL)
  168.     {
  169.         msg_write ("Error: Not enough memory to create scan line depth upper bound.");
  170.         return (-1);    /* some nonzero value to indicate an error */
  171.     }
  172.     if ((below = (int *) malloc ((PRECISION + 1) * sizeof (int))) == NULL)
  173.     {
  174.         msg_write ("Error: Not enough memory to create scan line depth lower bound.");
  175.         free (above);
  176.         return (-1);    /* some nonzero value to indicate an error */
  177.     }
  178.  
  179.         /* set up hiden line boundaries */
  180.     for (i = 0; i <= PRECISION; i++)
  181.     {
  182.         above[i] = -LARGE;
  183.         below[i] = LARGE;
  184.     }
  185.     return (0);
  186. }
  187. /* EOF */
  188. /* cat > src+obj/draw/intersect.c << "EOF" */
  189. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  190. /* intersect: Calculate the intersection of two lines    */
  191. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  192. /* SCCS information: %W%    %G% - NCSA */
  193.  
  194. /* #include "draw.h" */
  195.  
  196. void 
  197. intersect (bound, xj, yj, xi, yi, retx, rety)
  198.     int             bound[], xj, yj, xi, yi, *retx, *rety;
  199. {
  200.     double          a1, b1, a2, b2, x0, y0;
  201.  
  202.     if (yi == bound[xi] && yj == bound[xj])
  203.     {
  204.         *retx = xi;
  205.         *rety = yi;
  206.         return;
  207.     }
  208.     a1 = (double) (yi - yj);
  209.     b1 = (double) (yj - a1 * xj);
  210.     a2 = (double) (bound[xi] - bound[xj]);
  211.     b2 = (double) (bound[xj] - a2 * xj);
  212.     x0 = (b2 - b1) / (a1 - a2);
  213.     y0 = a1 * x0 + b1;
  214.     *retx = ROUND (x0);
  215.     *rety = ROUND (y0);
  216.     return;
  217. }
  218. /* EOF */
  219.