g_arc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* Lookup table listing relative chordal deviation, i.e. the factor
  16. (chordal deviation)/(chord length), for binary subdivisions of the arc
  17. sizes we support.
  18. The symbolic names for these arc sizes, e.g. QUARTER_ARC and
  19. THREE_QUARTER_ARC, are defined in extern.h. The constant
  20. MAX_ARC_SUBDIVISIONS, which can in principle be device dependent, is
  21. also defined there. It should be no greater than
  22. TABULATED_ARC_SUBDIVISIONS, the length of each entry in the lookup
  23. table.
  24. The formula
  25. relative_deviation = (1 - cos (theta/2)) / (2 * sin (theta/2)),
  26. where theta = subtended angle, was used to compute the numbers in the
  27. lookup tables. The formula is justified as follows. Let
  28. s = chordal deviation / radius (so that 0 <= s <= 2)
  29. h = half chord length / radius.
  30. Then by elementary trigonometry, s = 1 - cos(theta/2) and
  31. h = sin(theta/2), where theta = angle subtended by the chord. Since
  32. the relative chordal deviation is s/2h, this justifies the formula.
  33. Incidentally, h = sqrt(s * (2-s))), irrespective of theta.
  34. There is an alternative derivation of the numbers in these tables,
  35. tail-recursive and elegant, which uses only sqrt(). Let s' and h' be
  36. the values of s and h when the arc is bisected, i.e. theta is divided by
  37. two. Using half-angle formulae it is easy to check that
  38. s' = 1 - sqrt (1 - s / 2)
  39. h' = h / 2 (1 - s')
  40. (The first of these formulae is well-known; see the article of Ken
  41. Turkowski <turk@apple.com> in Graphics Gems V, where he approximates the
  42. right-hand side by s/4.) So the pair (s',h') may be computed from the
  43. pair (s,h). By updating (s,h) with every bisection, one can generate a
  44. table of successive values of the quotient s/2h, i.e. the relative
  45. chordal deviation.
  46. */
  47. /* Maximum number of times a circular or elliptic arc is recursively
  48. subdivided, when it is being approximated by an inscribed polyline. The
  49. polyline will contain no more than 2**MAX_ARC_SUBDIVISIONS line
  50. segments. MAX_ARC_SUBDIVISIONS must be no larger than
  51. TABULATED_ARC_SUBDIVISIONS below (the size of the tables in g_arc.h). */
  52. #define MAX_ARC_SUBDIVISIONS 5 /* to avoid buffer overflow on HP7550[A|B] */
  53. #define TABULATED_ARC_SUBDIVISIONS 15 /* length of each table entry */
  54. /* Types of circular/elliptic arc. These index into the doubly indexed
  55. table of `relative chordal deviations' below. */
  56. #define NUM_ARC_TYPES 3
  57. #define QUARTER_ARC 0
  58. #define HALF_ARC 1
  59. #define THREE_QUARTER_ARC 2
  60. #define USER_DEFINED_ARC -1 /* does not index into table */
  61. static const double _chord_table[NUM_ARC_TYPES][TABULATED_ARC_SUBDIVISIONS] =
  62. {
  63. { /* Quarter Arc */
  64. 0.20710678, /* for arc subtending 90 degrees */
  65. 0.099456184, /* for arc subtending 45 degrees */
  66. 0.049245702, /* for arc subtending 22.5 degrees */
  67. 0.024563425, /* for arc subtending 11.25 degrees */
  68. 0.012274311, /* etc. */
  69. 0.0061362312,
  70. 0.0030680001,
  71. 0.0015339856,
  72. 0.000766991,
  73. 0.00038349527,
  74. 0.00019174761,
  75. 9.58738e-05,
  76. 4.79369e-05,
  77. 2.396845e-05,
  78. 1.1984225e-05
  79. },
  80. { /* Half Arc */
  81. 0.5, /* for arc subtending 180 degrees */
  82. 0.20710678, /* for arc subtending 90 degrees */
  83. 0.099456184, /* for arc subtending 45 degrees */
  84. 0.049245702, /* for arc subtending 22.5 degrees */
  85. 0.024563425, /* for arc subtending 11.25 degrees */
  86. 0.012274311, /* etc. */
  87. 0.0061362312,
  88. 0.0030680001,
  89. 0.0015339856,
  90. 0.000766991,
  91. 0.00038349527,
  92. 0.00019174761,
  93. 9.58738e-05,
  94. 4.79369e-05,
  95. 2.396845e-05
  96. },
  97. { /* Three Quarter Arc */
  98. 1.2071068, /* for arc subtending 270 degrees */
  99. 0.33408932, /* for arc subtending 135 degrees */
  100. 0.15167334, /* for arc subtending 67.5 degrees */
  101. 0.074167994, /* for arc subtending 33.75 degrees */
  102. 0.036882216, /* etc. */
  103. 0.01841609,
  104. 0.0092049244,
  105. 0.0046020723,
  106. 0.0023009874,
  107. 0.0011504876,
  108. 0.00057524305,
  109. 0.00028762143,
  110. 0.0001438107,
  111. 7.190535e-05,
  112. 3.5952675e-05
  113. }
  114. };