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.1KB

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