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.

184 lines
7.5KB

  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. //==============================================================================
  22. /**
  23. Represents a parallelogram that is defined by 3 points.
  24. @see Rectangle, Point, Line
  25. @tags{Graphics}
  26. */
  27. template <typename ValueType>
  28. class Parallelogram
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a parallelogram with zero size at the origin.
  33. */
  34. Parallelogram() = default;
  35. /** Creates a copy of another parallelogram. */
  36. Parallelogram (const Parallelogram&) = default;
  37. /** Creates a parallelogram based on 3 points. */
  38. Parallelogram (Point<ValueType> topLeftPosition,
  39. Point<ValueType> topRightPosition,
  40. Point<ValueType> bottomLeftPosition) noexcept
  41. : topLeft (topLeftPosition), topRight (topRightPosition), bottomLeft (bottomLeftPosition)
  42. {
  43. }
  44. /** Creates a parallelogram from a rectangle. */
  45. Parallelogram (Rectangle<ValueType> rectangle) noexcept
  46. : topLeft (rectangle.getTopLeft()),
  47. topRight (rectangle.getTopRight()),
  48. bottomLeft (rectangle.getBottomLeft())
  49. {
  50. }
  51. Parallelogram& operator= (const Parallelogram&) = default;
  52. /** Destructor. */
  53. ~Parallelogram() = default;
  54. //==============================================================================
  55. /** Returns true if the parallelogram has a width or height of more than zero. */
  56. bool isEmpty() const noexcept { return topLeft != topRight || topLeft != bottomLeft; }
  57. /** Returns true if the parallelogram's coordinates are all finite numbers, i.e. not NaN or infinity. */
  58. inline bool isFinite() const noexcept { return topLeft.isFinite() && topRight.isFinite() && bottomLeft.isFinite(); }
  59. /** Returns the width of the parallelogram (i.e. the straight-line distance between the top-left and top-right. */
  60. inline ValueType getWidth() const noexcept { return Line<ValueType> (topLeft, topRight).getLength(); }
  61. /** Returns the height of the parallelogram (i.e. the straight-line distance between the top-left and bottom-left. */
  62. inline ValueType getHeight() const noexcept { return Line<ValueType> (topLeft, bottomLeft).getLength(); }
  63. //==============================================================================
  64. /** Returns the parallelogram's top-left position as a Point. */
  65. Point<ValueType> getTopLeft() const noexcept { return topLeft; }
  66. /** Returns the parallelogram's top-right position as a Point. */
  67. Point<ValueType> getTopRight() const noexcept { return topRight; }
  68. /** Returns the parallelogram's bottom-left position as a Point. */
  69. Point<ValueType> getBottomLeft() const noexcept { return bottomLeft; }
  70. /** Returns the parallelogram's bottom-right position as a Point. */
  71. Point<ValueType> getBottomRight() const noexcept { return topRight + (bottomLeft - topLeft); }
  72. //==============================================================================
  73. /** Returns true if the two parallelograms are identical. */
  74. bool operator== (const Parallelogram& other) const noexcept { return topLeft == other.topLeft && topRight == other.topRight && bottomLeft == other.bottomLeft; }
  75. /** Returns true if the two parallelograms are not identical. */
  76. bool operator!= (const Parallelogram& other) const noexcept { return ! operator== (other); }
  77. //==============================================================================
  78. /** Returns a parallelogram which is the same as this one moved by a given amount. */
  79. Parallelogram operator+ (Point<ValueType> deltaPosition) const noexcept
  80. {
  81. auto p = *this;
  82. p += deltaPosition;
  83. return p;
  84. }
  85. /** Moves this parallelogram by a given amount. */
  86. Parallelogram& operator+= (Point<ValueType> deltaPosition) noexcept
  87. {
  88. topLeft += deltaPosition;
  89. topRight += deltaPosition;
  90. bottomLeft += deltaPosition;
  91. return *this;
  92. }
  93. /** Returns a parallelogram which is the same as this one moved by a given amount. */
  94. Parallelogram operator- (Point<ValueType> deltaPosition) const noexcept
  95. {
  96. return operator+ (-deltaPosition);
  97. }
  98. /** Moves this parallelogram by a given amount. */
  99. Parallelogram& operator-= (Point<ValueType> deltaPosition) noexcept
  100. {
  101. return operator-= (-deltaPosition);
  102. }
  103. /** Returns a parallelogram that has been scaled by the given amount, centred around the origin. */
  104. template <typename PointOrScalarType>
  105. Parallelogram operator* (PointOrScalarType scaleFactor) const noexcept
  106. {
  107. auto p = *this;
  108. p *= scaleFactor;
  109. return p;
  110. }
  111. /** Scales this parallelogram by the given amount, centred around the origin. */
  112. template <typename PointOrScalarType>
  113. Parallelogram operator*= (PointOrScalarType scaleFactor) noexcept
  114. {
  115. topLeft *= scaleFactor;
  116. topRight *= scaleFactor;
  117. bottomLeft *= scaleFactor;
  118. return *this;
  119. }
  120. //==============================================================================
  121. /** Returns a point within this parallelogram, specified as proportional coordinates.
  122. The relative X and Y values should be between 0 and 1, where 0 is the left or
  123. top of this parallelogram, and 1 is the right or bottom. (Out-of-bounds values
  124. will return a point outside the parallelogram).
  125. */
  126. Point<ValueType> getRelativePoint (Point<ValueType> relativePosition) const noexcept
  127. {
  128. return topLeft
  129. + (topRight - topLeft) * relativePosition.x
  130. + (bottomLeft - topLeft) * relativePosition.y;
  131. }
  132. /** Returns a transformed version of the parallelogram. */
  133. Parallelogram transformedBy (const AffineTransform& transform) const noexcept
  134. {
  135. auto p = *this;
  136. transform.transformPoints (p.topLeft, p.topRight, p.bottomLeft);
  137. return p;
  138. }
  139. /** Returns the smallest rectangle that encloses this parallelogram. */
  140. Rectangle<ValueType> getBoundingBox() const noexcept
  141. {
  142. const Point<ValueType> points[] = { topLeft, topRight, bottomLeft, getBottomRight() };
  143. return Rectangle<ValueType>::findAreaContainingPoints (points, 4);
  144. }
  145. Point<ValueType> topLeft, topRight, bottomLeft;
  146. };
  147. } // namespace juce