Arc2.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Vector2.h>
  9. // The circle containing the arc is represented as |X-C| = R where C is the
  10. // center and R is the radius. The arc is defined by two points end0 and
  11. // end1 on the circle so that end1 is obtained from end0 by traversing
  12. // counterclockwise. The application is responsible for ensuring that end0
  13. // and end1 are on the circle and that they are properly ordered.
  14. namespace WwiseGTE
  15. {
  16. template <typename Real>
  17. class Arc2
  18. {
  19. public:
  20. // Construction and destruction. The default constructor sets the
  21. // center to (0,0), radius to 1, end0 to (1,0), and end1 to (0,1).
  22. Arc2()
  23. :
  24. center(Vector2<Real>::Zero()),
  25. radius((Real)1)
  26. {
  27. end[0] = Vector2<Real>::Unit(0);
  28. end[1] = Vector2<Real>::Unit(1);
  29. }
  30. Arc2(Vector2<Real> const& inCenter, Real inRadius,
  31. Vector2<Real>const& inEnd0, Vector2<Real>const& inEnd1)
  32. :
  33. center(inCenter),
  34. radius(inRadius)
  35. {
  36. end[0] = inEnd0;
  37. end[1] = inEnd1;
  38. }
  39. // Test whether P is on the arc. The application must ensure that P
  40. // is on the circle; that is, |P-C| = R. This test works for any
  41. // angle between B-C and A-C, not just those between 0 and pi
  42. // radians.
  43. bool Contains(Vector2<Real> const& p) const
  44. {
  45. // Assert: |P-C| = R where P is the input point, C is the circle
  46. // center and R is the circle radius. For P to be on the arc from
  47. // A to B, it must be on the side of the plane containing A with
  48. // normal N = Perp(B-A) where Perp(u,v) = (v,-u).
  49. Vector2<Real> diffPE0 = p - end[0];
  50. Vector2<Real> diffE1E0 = end[1] - end[0];
  51. Real dotPerp = DotPerp(diffPE0, diffE1E0);
  52. return dotPerp >= (Real)0;
  53. }
  54. Vector2<Real> center;
  55. Real radius;
  56. std::array<Vector2<Real>, 2> end;
  57. public:
  58. // Comparisons to support sorted containers.
  59. bool operator==(Arc2 const& arc) const
  60. {
  61. return center == arc.center && radius == arc.radius
  62. && end[0] == arc.end[0] && end[1] == arc.end[1];
  63. }
  64. bool operator!=(Arc2 const& arc) const
  65. {
  66. return !operator==(arc);
  67. }
  68. bool operator< (Arc2 const& arc) const
  69. {
  70. if (center < arc.center)
  71. {
  72. return true;
  73. }
  74. if (center > arc.center)
  75. {
  76. return false;
  77. }
  78. if (radius < arc.radius)
  79. {
  80. return true;
  81. }
  82. if (radius > arc.radius)
  83. {
  84. return false;
  85. }
  86. if (end[0] < arc.end[0])
  87. {
  88. return true;
  89. }
  90. if (end[0] > arc.end[0])
  91. {
  92. return false;
  93. }
  94. return end[1] < arc.end[1];
  95. }
  96. bool operator<=(Arc2 const& arc) const
  97. {
  98. return !arc.operator<(*this);
  99. }
  100. bool operator> (Arc2 const& arc) const
  101. {
  102. return arc.operator<(*this);
  103. }
  104. bool operator>=(Arc2 const& arc) const
  105. {
  106. return !operator<(arc);
  107. }
  108. };
  109. }