Audio plugin host https://kx.studio/carla
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.

juce_RelativeParallelogram.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. RelativeParallelogram::RelativeParallelogram()
  21. {
  22. }
  23. RelativeParallelogram::RelativeParallelogram (const Rectangle<float>& r)
  24. : topLeft (r.getTopLeft()), topRight (r.getTopRight()), bottomLeft (r.getBottomLeft())
  25. {
  26. }
  27. RelativeParallelogram::RelativeParallelogram (const RelativePoint& topLeft_, const RelativePoint& topRight_, const RelativePoint& bottomLeft_)
  28. : topLeft (topLeft_), topRight (topRight_), bottomLeft (bottomLeft_)
  29. {
  30. }
  31. RelativeParallelogram::RelativeParallelogram (const String& topLeft_, const String& topRight_, const String& bottomLeft_)
  32. : topLeft (topLeft_), topRight (topRight_), bottomLeft (bottomLeft_)
  33. {
  34. }
  35. RelativeParallelogram::~RelativeParallelogram()
  36. {
  37. }
  38. void RelativeParallelogram::resolveThreePoints (Point<float>* points, Expression::Scope* const scope) const
  39. {
  40. points[0] = topLeft.resolve (scope);
  41. points[1] = topRight.resolve (scope);
  42. points[2] = bottomLeft.resolve (scope);
  43. }
  44. void RelativeParallelogram::resolveFourCorners (Point<float>* points, Expression::Scope* const scope) const
  45. {
  46. resolveThreePoints (points, scope);
  47. points[3] = points[1] + (points[2] - points[0]);
  48. }
  49. const Rectangle<float> RelativeParallelogram::getBounds (Expression::Scope* const scope) const
  50. {
  51. Point<float> points[4];
  52. resolveFourCorners (points, scope);
  53. return Rectangle<float>::findAreaContainingPoints (points, 4);
  54. }
  55. void RelativeParallelogram::getPath (Path& path, Expression::Scope* const scope) const
  56. {
  57. Point<float> points[4];
  58. resolveFourCorners (points, scope);
  59. path.startNewSubPath (points[0]);
  60. path.lineTo (points[1]);
  61. path.lineTo (points[3]);
  62. path.lineTo (points[2]);
  63. path.closeSubPath();
  64. }
  65. AffineTransform RelativeParallelogram::resetToPerpendicular (Expression::Scope* const scope)
  66. {
  67. Point<float> corners[3];
  68. resolveThreePoints (corners, scope);
  69. const Line<float> top (corners[0], corners[1]);
  70. const Line<float> left (corners[0], corners[2]);
  71. const Point<float> newTopRight (corners[0] + Point<float> (top.getLength(), 0.0f));
  72. const Point<float> newBottomLeft (corners[0] + Point<float> (0.0f, left.getLength()));
  73. topRight.moveToAbsolute (newTopRight, scope);
  74. bottomLeft.moveToAbsolute (newBottomLeft, scope);
  75. return AffineTransform::fromTargetPoints (corners[0], corners[0],
  76. corners[1], newTopRight,
  77. corners[2], newBottomLeft);
  78. }
  79. bool RelativeParallelogram::isDynamic() const
  80. {
  81. return topLeft.isDynamic() || topRight.isDynamic() || bottomLeft.isDynamic();
  82. }
  83. bool RelativeParallelogram::operator== (const RelativeParallelogram& other) const noexcept
  84. {
  85. return topLeft == other.topLeft && topRight == other.topRight && bottomLeft == other.bottomLeft;
  86. }
  87. bool RelativeParallelogram::operator!= (const RelativeParallelogram& other) const noexcept
  88. {
  89. return ! operator== (other);
  90. }
  91. Point<float> RelativeParallelogram::getInternalCoordForPoint (const Point<float>* const corners, Point<float> target) noexcept
  92. {
  93. const Point<float> tr (corners[1] - corners[0]);
  94. const Point<float> bl (corners[2] - corners[0]);
  95. target -= corners[0];
  96. return Point<float> (Line<float> (Point<float>(), tr).getIntersection (Line<float> (target, target - bl)).getDistanceFromOrigin(),
  97. Line<float> (Point<float>(), bl).getIntersection (Line<float> (target, target - tr)).getDistanceFromOrigin());
  98. }
  99. Point<float> RelativeParallelogram::getPointForInternalCoord (const Point<float>* const corners, const Point<float> point) noexcept
  100. {
  101. return corners[0]
  102. + Line<float> (Point<float>(), corners[1] - corners[0]).getPointAlongLine (point.x)
  103. + Line<float> (Point<float>(), corners[2] - corners[0]).getPointAlongLine (point.y);
  104. }
  105. Rectangle<float> RelativeParallelogram::getBoundingBox (const Point<float>* const p) noexcept
  106. {
  107. const Point<float> points[] = { p[0], p[1], p[2], p[1] + (p[2] - p[0]) };
  108. return Rectangle<float>::findAreaContainingPoints (points, 4);
  109. }
  110. } // namespace juce