themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / fl_curve.cxx
blob2f8655232cf4bdb2f749c6cebc73a9c2be911e20
1 //
2 // "$Id: fl_curve.cxx 7903 2010-11-28 21:06:39Z matt $"
3 //
4 // Bezier curve functions for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 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.
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
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 /**
29 \file fl_curve.cxx
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>
39 #include <math.h>
41 void Fl_Graphics_Driver::curve(double X0, double Y0,
42 double X1, double Y1,
43 double X2, double Y2,
44 double X3, double Y3) {
46 double x = fl_transform_x(X0,Y0);
47 double y = fl_transform_y(X0,Y0);
49 // draw point 0:
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);
59 // find the area:
60 double a = fabs((x-x2)*(y3-yy1)-(y-y2)*(x3-x1));
61 double b = fabs((x-x3)*(y2-yy1)-(y-y3)*(x2-x1));
62 if (b > a) a = b;
64 // use that to guess at the number of segments:
65 int n = int(sqrt(a)/4);
66 if (n > 1) {
67 if (n > 100) n = 100; // make huge curves not hang forever
69 double e = 1.0/n;
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);
74 double xc = 3*(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++) {
91 x += dx1;
92 dx1 += dx2;
93 dx2 += dx3;
94 y += dy1;
95 dy1 += dy2;
96 dy2 += dy3;
97 fl_transformed_vertex(x,y);
100 // draw point n-1:
101 fl_transformed_vertex(x+dx1, y+dy1);
104 // draw point n:
105 fl_transformed_vertex(x3,y3);
109 // End of "$Id: fl_curve.cxx 7903 2010-11-28 21:06:39Z matt $".