ContScribeCircle2.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/Hypersphere.h>
  9. #include <Mathematics/LinearSystem.h>
  10. namespace WwiseGTE
  11. {
  12. // All functions return 'true' if the circle has been constructed, 'false'
  13. // otherwise (input points are linearly dependent).
  14. // Circle circumscribing triangle.
  15. template <typename Real>
  16. bool Circumscribe(Vector2<Real> const& v0, Vector2<Real> const& v1,
  17. Vector2<Real> const& v2, Circle2<Real>& circle)
  18. {
  19. Vector2<Real> e10 = v1 - v0;
  20. Vector2<Real> e20 = v2 - v0;
  21. Matrix2x2<Real> A{ e10[0], e10[1], e20[0], e20[1] };
  22. Vector2<Real> B{ (Real)0.5 * Dot(e10, e10), (Real)0.5 * Dot(e20, e20) };
  23. Vector2<Real> solution;
  24. if (LinearSystem<Real>::Solve(A, B, solution))
  25. {
  26. circle.center = v0 + solution;
  27. circle.radius = Length(solution);
  28. return true;
  29. }
  30. return false;
  31. }
  32. // Circle inscribing triangle.
  33. template <typename Real>
  34. bool Inscribe(Vector2<Real> const& v0, Vector2<Real> const& v1,
  35. Vector2<Real> const& v2, Circle2<Real>& circle)
  36. {
  37. Vector2<Real> d10 = v1 - v0;
  38. Vector2<Real> d20 = v2 - v0;
  39. Vector2<Real> d21 = v2 - v1;
  40. Real len10 = Length(d10);
  41. Real len20 = Length(d20);
  42. Real len21 = Length(d21);
  43. Real perimeter = len10 + len20 + len21;
  44. if (perimeter > (Real)0)
  45. {
  46. Real inv = (Real)1 / perimeter;
  47. len10 *= inv;
  48. len20 *= inv;
  49. len21 *= inv;
  50. circle.center = len21 * v0 + len20 * v1 + len10 * v2;
  51. circle.radius = inv * std::fabs(DotPerp(d10, d20));
  52. return circle.radius > (Real)0;
  53. }
  54. return false;
  55. }
  56. }