You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.9KB

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