The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

137 lines
5.1KB

  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. RelativeParallelogram::RelativeParallelogram()
  20. {
  21. }
  22. RelativeParallelogram::RelativeParallelogram (const Rectangle<float>& r)
  23. : topLeft (r.getTopLeft()), topRight (r.getTopRight()), bottomLeft (r.getBottomLeft())
  24. {
  25. }
  26. RelativeParallelogram::RelativeParallelogram (const RelativePoint& topLeft_, const RelativePoint& topRight_, const RelativePoint& bottomLeft_)
  27. : topLeft (topLeft_), topRight (topRight_), bottomLeft (bottomLeft_)
  28. {
  29. }
  30. RelativeParallelogram::RelativeParallelogram (const String& topLeft_, const String& topRight_, const String& bottomLeft_)
  31. : topLeft (topLeft_), topRight (topRight_), bottomLeft (bottomLeft_)
  32. {
  33. }
  34. RelativeParallelogram::~RelativeParallelogram()
  35. {
  36. }
  37. void RelativeParallelogram::resolveThreePoints (Point<float>* points, Expression::Scope* const scope) const
  38. {
  39. points[0] = topLeft.resolve (scope);
  40. points[1] = topRight.resolve (scope);
  41. points[2] = bottomLeft.resolve (scope);
  42. }
  43. void RelativeParallelogram::resolveFourCorners (Point<float>* points, Expression::Scope* const scope) const
  44. {
  45. resolveThreePoints (points, scope);
  46. points[3] = points[1] + (points[2] - points[0]);
  47. }
  48. const Rectangle<float> RelativeParallelogram::getBounds (Expression::Scope* const scope) const
  49. {
  50. Point<float> points[4];
  51. resolveFourCorners (points, scope);
  52. return Rectangle<float>::findAreaContainingPoints (points, 4);
  53. }
  54. void RelativeParallelogram::getPath (Path& path, Expression::Scope* const scope) const
  55. {
  56. Point<float> points[4];
  57. resolveFourCorners (points, scope);
  58. path.startNewSubPath (points[0]);
  59. path.lineTo (points[1]);
  60. path.lineTo (points[3]);
  61. path.lineTo (points[2]);
  62. path.closeSubPath();
  63. }
  64. AffineTransform RelativeParallelogram::resetToPerpendicular (Expression::Scope* const scope)
  65. {
  66. Point<float> corners[3];
  67. resolveThreePoints (corners, scope);
  68. const Line<float> top (corners[0], corners[1]);
  69. const Line<float> left (corners[0], corners[2]);
  70. const Point<float> newTopRight (corners[0] + Point<float> (top.getLength(), 0.0f));
  71. const Point<float> newBottomLeft (corners[0] + Point<float> (0.0f, left.getLength()));
  72. topRight.moveToAbsolute (newTopRight, scope);
  73. bottomLeft.moveToAbsolute (newBottomLeft, scope);
  74. return AffineTransform::fromTargetPoints (corners[0].x, corners[0].y, corners[0].x, corners[0].y,
  75. corners[1].x, corners[1].y, newTopRight.x, newTopRight.y,
  76. corners[2].x, corners[2].y, newBottomLeft.x, newBottomLeft.y);
  77. }
  78. bool RelativeParallelogram::isDynamic() const
  79. {
  80. return topLeft.isDynamic() || topRight.isDynamic() || bottomLeft.isDynamic();
  81. }
  82. bool RelativeParallelogram::operator== (const RelativeParallelogram& other) const noexcept
  83. {
  84. return topLeft == other.topLeft && topRight == other.topRight && bottomLeft == other.bottomLeft;
  85. }
  86. bool RelativeParallelogram::operator!= (const RelativeParallelogram& other) const noexcept
  87. {
  88. return ! operator== (other);
  89. }
  90. Point<float> RelativeParallelogram::getInternalCoordForPoint (const Point<float>* const corners, Point<float> target) noexcept
  91. {
  92. const Point<float> tr (corners[1] - corners[0]);
  93. const Point<float> bl (corners[2] - corners[0]);
  94. target -= corners[0];
  95. return Point<float> (Line<float> (Point<float>(), tr).getIntersection (Line<float> (target, target - bl)).getDistanceFromOrigin(),
  96. Line<float> (Point<float>(), bl).getIntersection (Line<float> (target, target - tr)).getDistanceFromOrigin());
  97. }
  98. Point<float> RelativeParallelogram::getPointForInternalCoord (const Point<float>* const corners, const Point<float> point) noexcept
  99. {
  100. return corners[0]
  101. + Line<float> (Point<float>(), corners[1] - corners[0]).getPointAlongLine (point.x)
  102. + Line<float> (Point<float>(), corners[2] - corners[0]).getPointAlongLine (point.y);
  103. }
  104. Rectangle<float> RelativeParallelogram::getBoundingBox (const Point<float>* const p) noexcept
  105. {
  106. const Point<float> points[] = { p[0], p[1], p[2], p[1] + (p[2] - p[0]) };
  107. return Rectangle<float>::findAreaContainingPoints (points, 4);
  108. }