home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xfig2.8 / part02 / graphics.c < prev    next >
C/C++ Source or Header  |  1990-07-02  |  3KB  |  105 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : March 1988
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "resources.h"
  12. #include "paintop.h"
  13.  
  14. /****************************************************************************
  15.  
  16.     The following spline drawing routine is from 
  17.  
  18.     "An Algorithm for High-Speed Curve Generation" 
  19.     by George Merrill Chaikin,
  20.     Computer Graphics and Image Processing, 3, Academic Press, 
  21.     1974, 346-349.
  22.  
  23.     and
  24.  
  25.     "On Chaikin's Algorithm" by R. F. Riesenfeld,
  26.     Computer Graphics and Image Processing, 4, Academic Press, 
  27.     1975, 304-310.
  28.  
  29. *****************************************************************************/
  30.  
  31. #define        round(x)    ((int) (x + .5))
  32. #define        half(z1, z2)    ((z1+z2)/2.0)
  33. #define        THRESHOLD    5
  34.  
  35. /* iterative version */
  36. /* because we draw the spline with small line segments, the style
  37.    parameter doesn't work */
  38.  
  39. quadratic_spline(a1, b1, a2, b2, a3, b3, a4, b4, op, thick, style, style_val)
  40. float    a1, b1, a2, b2, a3, b3, a4, b4;
  41. int    op, thick, style;
  42. float    style_val;
  43. {
  44.     float    x1, y1, x2, y2, x3, y3, x4, y4;
  45.     float    xmid, ymid;
  46.  
  47.     clear_stack();
  48.     push(a1, b1, a2, b2, a3, b3, a4, b4);
  49.  
  50.     while(pop(&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4)) {
  51.         xmid = half(x2, x3);
  52.         ymid = half(y2, y3);
  53.         if (fabs(x1 - xmid) < THRESHOLD && fabs(y1 - ymid) < THRESHOLD) {
  54.         pw_vector(canvas_win, round(x1), round(y1), 
  55.             round(xmid), round(ymid), op, thick, style, style_val);
  56.         }
  57.         else {
  58.         push(x1, y1, half(x1, x2), half(y1, y2),
  59.             half(x2, xmid), half(y2, ymid), xmid, ymid);
  60.         }
  61.  
  62.         if (fabs(xmid - x4) < THRESHOLD && fabs(ymid - y4) < THRESHOLD) {
  63.         pw_vector(canvas_win, round(xmid), round(ymid), 
  64.             round(x4), round(y4), op, thick, style, style_val);
  65.         }
  66.         else {
  67.         push(xmid, ymid, half(xmid, x3), half(ymid, y3),
  68.             half(x3, x4), half(y3, y4), x4, y4);
  69.         }
  70.         }
  71.     }
  72.  
  73. /* because we draw the spline with small line segments, the style
  74.    parameter doesn't work */
  75.  
  76. bezier_spline(a0, b0, a1, b1, a2, b2, a3, b3, op, thick, style, style_val)
  77. float    a0, b0, a1, b1, a2, b2, a3, b3;
  78. int    op, thick, style;
  79. float    style_val;
  80. {
  81.     float    x0, y0, x1, y1, x2, y2, x3, y3;
  82.     float    sx1, sy1, sx2, sy2, tx, ty, tx1, ty1, tx2, ty2, xmid, ymid;
  83.  
  84.     clear_stack();
  85.     push(a0, b0, a1, b1, a2, b2, a3, b3);
  86.  
  87.     while(pop(&x0, &y0, &x1, &y1, &x2, &y2, &x3, &y3)) {
  88.         if (fabs(x0 - x3) < THRESHOLD && fabs(y0 - y3) < THRESHOLD) {
  89.         pw_vector(canvas_win, round(x0), round(y0), 
  90.             round(x3), round(y3), op, thick, style, style_val);
  91.         }
  92.         else {
  93.         tx = half(x1, x2);    ty = half(y1, y2);
  94.         sx1 = half(x0, x1);    sy1 = half(y0, y1);
  95.         sx2 = half(sx1, tx);    sy2 = half(sy1, ty);
  96.         tx2 = half(x2, x3);    ty2 = half(y2, y3);
  97.         tx1 = half(tx2, tx);    ty1 = half(ty2, ty);
  98.         xmid = half(sx2, tx1);    ymid = half(sy2, ty1);
  99.  
  100.         push(x0, y0, sx1, sy1, sx2, sy2, xmid, ymid);
  101.         push(xmid, ymid, tx1, ty1, tx2, ty2, x3, y3);
  102.         }
  103.         }
  104.     }
  105.