IntrLine2Circle2.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/FIQuery.h>
  9. #include <Mathematics/TIQuery.h>
  10. #include <Mathematics/DistPointLine.h>
  11. #include <Mathematics/Hypersphere.h>
  12. #include <Mathematics/Vector2.h>
  13. // The queries consider the circle to be a solid (disk).
  14. namespace WwiseGTE
  15. {
  16. template <typename Real>
  17. class TIQuery<Real, Line2<Real>, Circle2<Real>>
  18. {
  19. public:
  20. struct Result
  21. {
  22. bool intersect;
  23. };
  24. Result operator()(Line2<Real> const& line, Circle2<Real> const& circle)
  25. {
  26. Result result;
  27. DCPQuery<Real, Vector2<Real>, Line2<Real>> plQuery;
  28. auto plResult = plQuery(circle.center, line);
  29. result.intersect = (plResult.distance <= circle.radius);
  30. return result;
  31. }
  32. };
  33. template <typename Real>
  34. class FIQuery<Real, Line2<Real>, Circle2<Real>>
  35. {
  36. public:
  37. struct Result
  38. {
  39. bool intersect;
  40. int numIntersections;
  41. std::array<Real, 2> parameter;
  42. std::array<Vector2<Real>, 2> point;
  43. };
  44. Result operator()(Line2<Real> const& line, Circle2<Real> const& circle)
  45. {
  46. Result result;
  47. DoQuery(line.origin, line.direction, circle, result);
  48. for (int i = 0; i < result.numIntersections; ++i)
  49. {
  50. result.point[i] = line.origin + result.parameter[i] * line.direction;
  51. }
  52. return result;
  53. }
  54. protected:
  55. void DoQuery(Vector2<Real> const& lineOrigin,
  56. Vector2<Real> const& lineDirection, Circle2<Real> const& circle,
  57. Result& result)
  58. {
  59. // Intersection of a the line P+t*D and the circle |X-C| = R.
  60. // The line direction is unit length. The t-value is a
  61. // real-valued root to the quadratic equation
  62. // 0 = |t*D+P-C|^2 - R^2
  63. // = t^2 + 2*Dot(D,P-C)*t + |P-C|^2-R^2
  64. // = t^2 + 2*a1*t + a0
  65. // If there are two distinct roots, the order is t0 < t1.
  66. Vector2<Real> diff = lineOrigin - circle.center;
  67. Real a0 = Dot(diff, diff) - circle.radius * circle.radius;
  68. Real a1 = Dot(lineDirection, diff);
  69. Real discr = a1 * a1 - a0;
  70. if (discr > (Real)0)
  71. {
  72. Real root = std::sqrt(discr);
  73. result.intersect = true;
  74. result.numIntersections = 2;
  75. result.parameter[0] = -a1 - root;
  76. result.parameter[1] = -a1 + root;
  77. }
  78. else if (discr < (Real)0)
  79. {
  80. result.intersect = false;
  81. result.numIntersections = 0;
  82. }
  83. else // discr == 0
  84. {
  85. result.intersect = true;
  86. result.numIntersections = 1;
  87. result.parameter[0] = -a1;
  88. }
  89. }
  90. };
  91. }