home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / fl_curve.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  3.1 KB  |  100 lines

  1. //
  2. // "$Id: fl_curve.cxx,v 1.4 1999/01/07 19:17:37 mike Exp $"
  3. //
  4. // Bezier curve functions for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Utility for drawing Bezier curves, adding the points to
  27. // the current fl_begin/fl_vertex/fl_end path.
  28. // Incremental math implementation:
  29. // I very much doubt this is optimal!  From Foley/vanDam page 511.
  30. // If anybody has a better algorithim, please send it!
  31.  
  32. #include <FL/fl_draw.H>
  33. #include <math.h>
  34.  
  35. void fl_curve(double X0, double Y0,
  36.           double X1, double Y1,
  37.           double X2, double Y2,
  38.           double X3, double Y3) {
  39.   double x = fl_transform_x(X0,Y0);
  40.   double y = fl_transform_y(X0,Y0);
  41.   double x1 = fl_transform_x(X1,Y1);
  42.   double y1 = fl_transform_y(X1,Y1);
  43.   double x2 = fl_transform_x(X2,Y2);
  44.   double y2 = fl_transform_y(X2,Y2);
  45.   double x3 = fl_transform_x(X3,Y3);
  46.   double y3 = fl_transform_y(X3,Y3);
  47.  
  48.   int n; { // find smaller size of bounding box
  49.     double lx = x; if (x1<lx) lx=x1; if (x2<lx) lx=x2; if (x3<lx) lx=x3;
  50.     double rx = x; if (x1>rx) rx=x1; if (x2>rx) rx=x2; if (x3>rx) rx=x3;
  51.     double ly = y; if (y1<ly) ly=y1; if (y2<ly) ly=y2; if (y3<ly) ly=y3;
  52.     double ry = y; if (y1>ry) ry=y1; if (y2>ry) ry=y2; if (y3>ry) ry=y3;
  53.     // calculate number of pieces to cut curve into:
  54.     n = int((rx-lx+ry-ly)/8); if (n < 3) n = 3;
  55.   }
  56.   double e = 1.0/n;
  57.  
  58.   // calculate the coefficients of 3rd order equation:
  59.   double xa = (x3-3*x2+3*x1-x);
  60.   double xb = 3*(x2-2*x1+x);
  61.   double xc = 3*(x1-x);
  62.   // calculate the forward differences:
  63.   double dx1 = ((xa*e+xb)*e+xc)*e;
  64.   double dx3 = 6*xa*e*e*e;
  65.   double dx2 = dx3 + 2*xb*e*e;
  66.  
  67.   // calculate the coefficients of 3rd order equation:
  68.   double ya = (y3-3*y2+3*y1-y);
  69.   double yb = 3*(y2-2*y1+y);
  70.   double yc = 3*(y1-y);
  71.   // calculate the forward differences:
  72.   double dy1 = ((ya*e+yb)*e+yc)*e;
  73.   double dy3 = 6*ya*e*e*e;
  74.   double dy2 = dy3 + 2*yb*e*e;
  75.  
  76.   // draw point 0:
  77.   fl_transformed_vertex(x,y);
  78.  
  79.   // draw points 1 .. n-2:
  80.   for (int m=2; m<n; m++) {
  81.     x += dx1;
  82.     dx1 += dx2;
  83.     dx2 += dx3;
  84.     y += dy1;
  85.     dy1 += dy2;
  86.     dy2 += dy3;
  87.     fl_transformed_vertex(x,y);
  88.   }
  89.  
  90.   // draw point n-1:
  91.   fl_transformed_vertex(x+dx1, y+dy1);
  92.  
  93.   // draw point n:
  94.   fl_transformed_vertex(x3,y3);
  95. }
  96.  
  97. //
  98. // End of "$Id: fl_curve.cxx,v 1.4 1999/01/07 19:17:37 mike Exp $".
  99. //
  100.