Audio plugin host https://kx.studio/carla
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.

179 lines
7.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. Represents a parallelogram that is defined by 3 points.
  18. @see Rectangle, Point, Line
  19. @tags{Graphics}
  20. */
  21. template <typename ValueType>
  22. class Parallelogram
  23. {
  24. public:
  25. //==============================================================================
  26. /** Creates a parallelogram with zero size at the origin.
  27. */
  28. Parallelogram() = default;
  29. /** Creates a copy of another parallelogram. */
  30. Parallelogram (const Parallelogram&) = default;
  31. /** Creates a parallelogram based on 3 points. */
  32. Parallelogram (Point<ValueType> topLeftPosition,
  33. Point<ValueType> topRightPosition,
  34. Point<ValueType> bottomLeftPosition) noexcept
  35. : topLeft (topLeftPosition), topRight (topRightPosition), bottomLeft (bottomLeftPosition)
  36. {
  37. }
  38. /** Creates a parallelogram from a rectangle. */
  39. Parallelogram (Rectangle<ValueType> rectangle) noexcept
  40. : topLeft (rectangle.getTopLeft()),
  41. topRight (rectangle.getTopRight()),
  42. bottomLeft (rectangle.getBottomLeft())
  43. {
  44. }
  45. Parallelogram& operator= (const Parallelogram&) = default;
  46. /** Destructor. */
  47. ~Parallelogram() = default;
  48. //==============================================================================
  49. /** Returns true if the parallelogram has a width or height of more than zero. */
  50. bool isEmpty() const noexcept { return topLeft != topRight || topLeft != bottomLeft; }
  51. /** Returns true if the parallelogram's coordinates are all finite numbers, i.e. not NaN or infinity. */
  52. inline bool isFinite() const noexcept { return topLeft.isFinite() && topRight.isFinite() && bottomLeft.isFinite(); }
  53. /** Returns the width of the parallelogram (i.e. the straight-line distance between the top-left and top-right. */
  54. inline ValueType getWidth() const noexcept { return Line<ValueType> (topLeft, topRight).getLength(); }
  55. /** Returns the height of the parallelogram (i.e. the straight-line distance between the top-left and bottom-left. */
  56. inline ValueType getHeight() const noexcept { return Line<ValueType> (topLeft, bottomLeft).getLength(); }
  57. //==============================================================================
  58. /** Returns the parallelogram's top-left position as a Point. */
  59. Point<ValueType> getTopLeft() const noexcept { return topLeft; }
  60. /** Returns the parallelogram's top-right position as a Point. */
  61. Point<ValueType> getTopRight() const noexcept { return topRight; }
  62. /** Returns the parallelogram's bottom-left position as a Point. */
  63. Point<ValueType> getBottomLeft() const noexcept { return bottomLeft; }
  64. /** Returns the parallelogram's bottom-right position as a Point. */
  65. Point<ValueType> getBottomRight() const noexcept { return topRight + (bottomLeft - topLeft); }
  66. //==============================================================================
  67. /** Returns true if the two parallelograms are identical. */
  68. bool operator== (const Parallelogram& other) const noexcept { return topLeft == other.topLeft && topRight == other.topRight && bottomLeft == other.bottomLeft; }
  69. /** Returns true if the two parallelograms are not identical. */
  70. bool operator!= (const Parallelogram& other) const noexcept { return ! operator== (other); }
  71. //==============================================================================
  72. /** Returns a parallelogram which is the same as this one moved by a given amount. */
  73. Parallelogram operator+ (Point<ValueType> deltaPosition) const noexcept
  74. {
  75. auto p = *this;
  76. p += deltaPosition;
  77. return p;
  78. }
  79. /** Moves this parallelogram by a given amount. */
  80. Parallelogram& operator+= (Point<ValueType> deltaPosition) noexcept
  81. {
  82. topLeft += deltaPosition;
  83. topRight += deltaPosition;
  84. bottomLeft += deltaPosition;
  85. return *this;
  86. }
  87. /** Returns a parallelogram which is the same as this one moved by a given amount. */
  88. Parallelogram operator- (Point<ValueType> deltaPosition) const noexcept
  89. {
  90. return operator+ (-deltaPosition);
  91. }
  92. /** Moves this parallelogram by a given amount. */
  93. Parallelogram& operator-= (Point<ValueType> deltaPosition) noexcept
  94. {
  95. return operator-= (-deltaPosition);
  96. }
  97. /** Returns a parallelogram that has been scaled by the given amount, centred around the origin. */
  98. template <typename PointOrScalarType>
  99. Parallelogram operator* (PointOrScalarType scaleFactor) const noexcept
  100. {
  101. auto p = *this;
  102. p *= scaleFactor;
  103. return p;
  104. }
  105. /** Scales this parallelogram by the given amount, centred around the origin. */
  106. template <typename PointOrScalarType>
  107. Parallelogram operator*= (PointOrScalarType scaleFactor) noexcept
  108. {
  109. topLeft *= scaleFactor;
  110. topRight *= scaleFactor;
  111. bottomLeft *= scaleFactor;
  112. return *this;
  113. }
  114. //==============================================================================
  115. /** Returns a point within this parallelogram, specified as proportional coordinates.
  116. The relative X and Y values should be between 0 and 1, where 0 is the left or
  117. top of this parallelogram, and 1 is the right or bottom. (Out-of-bounds values
  118. will return a point outside the parallelogram).
  119. */
  120. Point<ValueType> getRelativePoint (Point<ValueType> relativePosition) const noexcept
  121. {
  122. return topLeft
  123. + (topRight - topLeft) * relativePosition.x
  124. + (bottomLeft - topLeft) * relativePosition.y;
  125. }
  126. /** Returns a transformed version of the parallelogram. */
  127. Parallelogram transformedBy (const AffineTransform& transform) const noexcept
  128. {
  129. auto p = *this;
  130. transform.transformPoints (p.topLeft.x, p.topLeft.y,
  131. p.topRight.x, p.topRight.y,
  132. p.bottomLeft.x, p.bottomLeft.y);
  133. return p;
  134. }
  135. /** Returns the smallest rectangle that encloses this parallelogram. */
  136. Rectangle<ValueType> getBoundingBox() const noexcept
  137. {
  138. const Point<ValueType> points[] = { topLeft, topRight, bottomLeft, getBottomRight() };
  139. return Rectangle<ValueType>::findAreaContainingPoints (points, 4);
  140. }
  141. Point<ValueType> topLeft, topRight, bottomLeft;
  142. };
  143. } // namespace juce