2 // "$Id: fl_curve.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Bezier curve functions for the Fast Light Tool Kit (FLTK).
6 // Copyright 1998-2010 by Bill Spitzak and others.
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.
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.
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
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
30 \brief Utility for drawing Bezier curves, adding the points to the
31 current fl_begin/fl_vertex/fl_end path.
33 Incremental math implementation:
34 I very much doubt this is optimal! From Foley/vanDam page 511.
35 If anybody has a better algorithm, please send it!
38 #include <FL/fl_draw.H>
41 void Fl_Graphics_Driver::curve(double X0
, double Y0
,
44 double X3
, double Y3
) {
46 double x
= fl_transform_x(X0
,Y0
);
47 double y
= fl_transform_y(X0
,Y0
);
50 fl_transformed_vertex(x
,y
);
52 double x1
= fl_transform_x(X1
,Y1
);
53 double yy1
= fl_transform_y(X1
,Y1
);
54 double x2
= fl_transform_x(X2
,Y2
);
55 double y2
= fl_transform_y(X2
,Y2
);
56 double x3
= fl_transform_x(X3
,Y3
);
57 double y3
= fl_transform_y(X3
,Y3
);
60 double a
= fabs((x
-x2
)*(y3
-yy1
)-(y
-y2
)*(x3
-x1
));
61 double b
= fabs((x
-x3
)*(y2
-yy1
)-(y
-y3
)*(x2
-x1
));
64 // use that to guess at the number of segments:
65 int n
= int(sqrt(a
)/4);
67 if (n
> 100) n
= 100; // make huge curves not hang forever
71 // calculate the coefficients of 3rd order equation:
72 double xa
= (x3
-3*x2
+3*x1
-x
);
73 double xb
= 3*(x2
-2*x1
+x
);
75 // calculate the forward differences:
76 double dx1
= ((xa
*e
+xb
)*e
+xc
)*e
;
77 double dx3
= 6*xa
*e
*e
*e
;
78 double dx2
= dx3
+ 2*xb
*e
*e
;
80 // calculate the coefficients of 3rd order equation:
81 double ya
= (y3
-3*y2
+3*yy1
-y
);
82 double yb
= 3*(y2
-2*yy1
+y
);
83 double yc
= 3*(yy1
-y
);
84 // calculate the forward differences:
85 double dy1
= ((ya
*e
+yb
)*e
+yc
)*e
;
86 double dy3
= 6*ya
*e
*e
*e
;
87 double dy2
= dy3
+ 2*yb
*e
*e
;
89 // draw points 1 .. n-2:
90 for (int m
=2; m
<n
; m
++) {
97 fl_transformed_vertex(x
,y
);
101 fl_transformed_vertex(x
+dx1
, y
+dy1
);
105 fl_transformed_vertex(x3
,y3
);
109 // End of "$Id: fl_curve.cxx 7903 2010-11-28 21:06:39Z matt $".