2 // "$Id: fl_arc.cxx 7903 2010-11-28 21:06:39Z matt $"
4 // Arc 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 functions for drawing arcs and circles.
33 // Utility for drawing arcs and circles. They are added to
34 // the current fl_begin/fl_vertex/fl_end path.
35 // Incremental math implementation:
37 #include <FL/fl_draw.H>
40 // avoid problems with some platforms that don't
42 static double _fl_hypot(double x
, double y
) {
43 return sqrt(x
*x
+ y
*y
);
46 void Fl_Graphics_Driver::arc(double x
, double y
, double r
, double start
, double end
) {
48 // draw start point accurately:
50 double A
= start
*(M_PI
/180); // Initial angle (radians)
51 double X
= r
*cos(A
); // Initial displacement, (X,Y)
52 double Y
= -r
*sin(A
); // from center to initial point
53 fl_vertex(x
+X
,y
+Y
); // Insert initial point
55 // Maximum arc length to approximate with chord with error <= 0.125
58 double r1
= _fl_hypot(fl_transform_dx(r
,0), // Horizontal "radius"
59 fl_transform_dy(r
,0));
60 double r2
= _fl_hypot(fl_transform_dx(0,r
), // Vertical "radius"
61 fl_transform_dy(0,r
));
63 if (r1
> r2
) r1
= r2
; // r1 = minimum "radius"
64 if (r1
< 2.) r1
= 2.; // radius for circa 9 chords/circle
66 epsilon
= 2*acos(1.0 - 0.125/r1
); // Maximum arc angle
68 A
= end
*(M_PI
/180) - A
; // Displacement angle (radians)
69 int i
= int(ceil(fabs(A
)/epsilon
)); // Segments in approximation
72 epsilon
= A
/i
; // Arc length for equal-size steps
73 double cos_e
= cos(epsilon
); // Rotation coefficients
74 double sin_e
= sin(epsilon
);
76 double Xnew
= cos_e
*X
+ sin_e
*Y
;
77 Y
= -sin_e
*X
+ cos_e
*Y
;
78 fl_vertex(x
+ (X
=Xnew
), y
+ Y
);
83 #if 0 // portable version. X-specific one in fl_vertex.cxx
84 void fl_circle(double x
,double y
,double r
) {
85 _fl_arc(x
, y
, r
, r
, 0, 360);
90 // End of "$Id: fl_arc.cxx 7903 2010-11-28 21:06:39Z matt $".