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.

142 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. BEGIN_JUCE_NAMESPACE
  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. const 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].getX(), corners[0].getY(), corners[0].getX(), corners[0].getY(),
  76. corners[1].getX(), corners[1].getY(), newTopRight.getX(), newTopRight.getY(),
  77. corners[2].getX(), corners[2].getY(), newBottomLeft.getX(), newBottomLeft.getY());
  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. const 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. const 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.getX())
  103. + Line<float> (Point<float>(), corners[2] - corners[0]).getPointAlongLine (point.getY());
  104. }
  105. const 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. END_JUCE_NAMESPACE