DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame^] | 1 | // |
| 2 | // "$Id: fl_arc.cxx 7903 2010-11-28 21:06:39Z matt $" |
| 3 | // |
| 4 | // Arc 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. |
| 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 on the following page: |
| 24 | // |
| 25 | // http://www.fltk.org/str.php |
| 26 | // |
| 27 | |
| 28 | /** |
| 29 | \file fl_arc.cxx |
| 30 | \brief Utility functions for drawing arcs and circles. |
| 31 | */ |
| 32 | |
| 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: |
| 36 | |
| 37 | #include <FL/fl_draw.H> |
| 38 | #include <FL/math.h> |
| 39 | |
| 40 | // avoid problems with some platforms that don't |
| 41 | // implement hypot. |
| 42 | static double _fl_hypot(double x, double y) { |
| 43 | return sqrt(x*x + y*y); |
| 44 | } |
| 45 | |
| 46 | void Fl_Graphics_Driver::arc(double x, double y, double r, double start, double end) { |
| 47 | |
| 48 | // draw start point accurately: |
| 49 | |
| 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 |
| 54 | |
| 55 | // Maximum arc length to approximate with chord with error <= 0.125 |
| 56 | |
| 57 | double epsilon; { |
| 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)); |
| 62 | |
| 63 | if (r1 > r2) r1 = r2; // r1 = minimum "radius" |
| 64 | if (r1 < 2.) r1 = 2.; // radius for circa 9 chords/circle |
| 65 | |
| 66 | epsilon = 2*acos(1.0 - 0.125/r1); // Maximum arc angle |
| 67 | } |
| 68 | A = end*(M_PI/180) - A; // Displacement angle (radians) |
| 69 | int i = int(ceil(fabs(A)/epsilon)); // Segments in approximation |
| 70 | |
| 71 | if (i) { |
| 72 | epsilon = A/i; // Arc length for equal-size steps |
| 73 | double cos_e = cos(epsilon); // Rotation coefficients |
| 74 | double sin_e = sin(epsilon); |
| 75 | do { |
| 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); |
| 79 | } while (--i); |
| 80 | } |
| 81 | } |
| 82 | |
| 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); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | // |
| 90 | // End of "$Id: fl_arc.cxx 7903 2010-11-28 21:06:39Z matt $". |
| 91 | // |