juce_RelativeParallelogram.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. RelativeParallelogram::RelativeParallelogram()
  18. {
  19. }
  20. RelativeParallelogram::RelativeParallelogram (const Rectangle<float>& r)
  21. : topLeft (r.getTopLeft()), topRight (r.getTopRight()), bottomLeft (r.getBottomLeft())
  22. {
  23. }
  24. RelativeParallelogram::RelativeParallelogram (const RelativePoint& topLeft_, const RelativePoint& topRight_, const RelativePoint& bottomLeft_)
  25. : topLeft (topLeft_), topRight (topRight_), bottomLeft (bottomLeft_)
  26. {
  27. }
  28. RelativeParallelogram::RelativeParallelogram (const String& topLeft_, const String& topRight_, const String& bottomLeft_)
  29. : topLeft (topLeft_), topRight (topRight_), bottomLeft (bottomLeft_)
  30. {
  31. }
  32. RelativeParallelogram::~RelativeParallelogram()
  33. {
  34. }
  35. void RelativeParallelogram::resolveThreePoints (Point<float>* points, Expression::Scope* const scope) const
  36. {
  37. points[0] = topLeft.resolve (scope);
  38. points[1] = topRight.resolve (scope);
  39. points[2] = bottomLeft.resolve (scope);
  40. }
  41. void RelativeParallelogram::resolveFourCorners (Point<float>* points, Expression::Scope* const scope) const
  42. {
  43. resolveThreePoints (points, scope);
  44. points[3] = points[1] + (points[2] - points[0]);
  45. }
  46. const Rectangle<float> RelativeParallelogram::getBounds (Expression::Scope* const scope) const
  47. {
  48. Point<float> points[4];
  49. resolveFourCorners (points, scope);
  50. return Rectangle<float>::findAreaContainingPoints (points, 4);
  51. }
  52. void RelativeParallelogram::getPath (Path& path, Expression::Scope* const scope) const
  53. {
  54. Point<float> points[4];
  55. resolveFourCorners (points, scope);
  56. path.startNewSubPath (points[0]);
  57. path.lineTo (points[1]);
  58. path.lineTo (points[3]);
  59. path.lineTo (points[2]);
  60. path.closeSubPath();
  61. }
  62. AffineTransform RelativeParallelogram::resetToPerpendicular (Expression::Scope* const scope)
  63. {
  64. Point<float> corners[3];
  65. resolveThreePoints (corners, scope);
  66. const Line<float> top (corners[0], corners[1]);
  67. const Line<float> left (corners[0], corners[2]);
  68. const Point<float> newTopRight (corners[0] + Point<float> (top.getLength(), 0.0f));
  69. const Point<float> newBottomLeft (corners[0] + Point<float> (0.0f, left.getLength()));
  70. topRight.moveToAbsolute (newTopRight, scope);
  71. bottomLeft.moveToAbsolute (newBottomLeft, scope);
  72. return AffineTransform::fromTargetPoints (corners[0].x, corners[0].y, corners[0].x, corners[0].y,
  73. corners[1].x, corners[1].y, newTopRight.x, newTopRight.y,
  74. corners[2].x, corners[2].y, newBottomLeft.x, newBottomLeft.y);
  75. }
  76. bool RelativeParallelogram::isDynamic() const
  77. {
  78. return topLeft.isDynamic() || topRight.isDynamic() || bottomLeft.isDynamic();
  79. }
  80. bool RelativeParallelogram::operator== (const RelativeParallelogram& other) const noexcept
  81. {
  82. return topLeft == other.topLeft && topRight == other.topRight && bottomLeft == other.bottomLeft;
  83. }
  84. bool RelativeParallelogram::operator!= (const RelativeParallelogram& other) const noexcept
  85. {
  86. return ! operator== (other);
  87. }
  88. Point<float> RelativeParallelogram::getInternalCoordForPoint (const Point<float>* const corners, Point<float> target) noexcept
  89. {
  90. const Point<float> tr (corners[1] - corners[0]);
  91. const Point<float> bl (corners[2] - corners[0]);
  92. target -= corners[0];
  93. return Point<float> (Line<float> (Point<float>(), tr).getIntersection (Line<float> (target, target - bl)).getDistanceFromOrigin(),
  94. Line<float> (Point<float>(), bl).getIntersection (Line<float> (target, target - tr)).getDistanceFromOrigin());
  95. }
  96. Point<float> RelativeParallelogram::getPointForInternalCoord (const Point<float>* const corners, const Point<float> point) noexcept
  97. {
  98. return corners[0]
  99. + Line<float> (Point<float>(), corners[1] - corners[0]).getPointAlongLine (point.x)
  100. + Line<float> (Point<float>(), corners[2] - corners[0]).getPointAlongLine (point.y);
  101. }
  102. Rectangle<float> RelativeParallelogram::getBoundingBox (const Point<float>* const p) noexcept
  103. {
  104. const Point<float> points[] = { p[0], p[1], p[2], p[1] + (p[2] - p[0]) };
  105. return Rectangle<float>::findAreaContainingPoints (points, 4);
  106. }