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.

193 lines
7.7KB

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