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.

244 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #ifndef JUCE_POINT_H_INCLUDED
  18. #define JUCE_POINT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A pair of (x, y) coordinates.
  22. The ValueType template should be a primitive type such as int, float, double,
  23. rather than a class.
  24. @see Line, Path, AffineTransform
  25. */
  26. template <typename ValueType>
  27. class Point
  28. {
  29. public:
  30. /** Creates a point at the origin */
  31. Point() noexcept : x(), y() {}
  32. /** Creates a copy of another point. */
  33. Point (const Point& other) noexcept : x (other.x), y (other.y) {}
  34. /** Creates a point from an (x, y) position. */
  35. Point (ValueType initialX, ValueType initialY) noexcept : x (initialX), y (initialY) {}
  36. //==============================================================================
  37. /** Copies this point from another one. */
  38. Point& operator= (const Point& other) noexcept { x = other.x; y = other.y; return *this; }
  39. inline bool operator== (Point other) const noexcept { return x == other.x && y == other.y; }
  40. inline bool operator!= (Point other) const noexcept { return x != other.x || y != other.y; }
  41. /** Returns true if the point is (0, 0). */
  42. bool isOrigin() const noexcept { return x == ValueType() && y == ValueType(); }
  43. /** Returns true if the coordinates are finite values. */
  44. inline bool isFinite() const noexcept { return juce_isfinite(x) && juce_isfinite(y); }
  45. /** Returns the point's x coordinate. */
  46. inline ValueType getX() const noexcept { return x; }
  47. /** Returns the point's y coordinate. */
  48. inline ValueType getY() const noexcept { return y; }
  49. /** Sets the point's x coordinate. */
  50. inline void setX (ValueType newX) noexcept { x = newX; }
  51. /** Sets the point's y coordinate. */
  52. inline void setY (ValueType newY) noexcept { y = newY; }
  53. /** Returns a point which has the same Y position as this one, but a new X. */
  54. Point withX (ValueType newX) const noexcept { return Point (newX, y); }
  55. /** Returns a point which has the same X position as this one, but a new Y. */
  56. Point withY (ValueType newY) const noexcept { return Point (x, newY); }
  57. /** Changes the point's x and y coordinates. */
  58. void setXY (ValueType newX, ValueType newY) noexcept { x = newX; y = newY; }
  59. /** Adds a pair of coordinates to this value. */
  60. void addXY (ValueType xToAdd, ValueType yToAdd) noexcept { x += xToAdd; y += yToAdd; }
  61. //==============================================================================
  62. /** Returns a point with a given offset from this one. */
  63. Point translated (ValueType deltaX, ValueType deltaY) const noexcept { return Point (x + deltaX, y + deltaY); }
  64. /** Adds two points together */
  65. Point operator+ (Point other) const noexcept { return Point (x + other.x, y + other.y); }
  66. /** Adds another point's coordinates to this one */
  67. Point& operator+= (Point other) noexcept { x += other.x; y += other.y; return *this; }
  68. /** Subtracts one points from another */
  69. Point operator- (Point other) const noexcept { return Point (x - other.x, y - other.y); }
  70. /** Subtracts another point's coordinates to this one */
  71. Point& operator-= (Point other) noexcept { x -= other.x; y -= other.y; return *this; }
  72. /** Multiplies two points together */
  73. template <typename OtherType>
  74. Point operator* (Point<OtherType> other) const noexcept { return Point ((ValueType) (x * other.x), (ValueType) (y * other.y)); }
  75. /** Multiplies another point's coordinates to this one */
  76. template <typename OtherType>
  77. Point& operator*= (Point<OtherType> other) noexcept { *this = *this * other; return *this; }
  78. /** Divides one point by another */
  79. template <typename OtherType>
  80. Point operator/ (Point<OtherType> other) const noexcept { return Point ((ValueType) (x / other.x), (ValueType) (y / other.y)); }
  81. /** Divides this point's coordinates by another */
  82. template <typename OtherType>
  83. Point& operator/= (Point<OtherType> other) noexcept { *this = *this / other; return *this; }
  84. /** Returns a point whose coordinates are multiplied by a given scalar value. */
  85. template <typename FloatType>
  86. Point operator* (FloatType multiplier) const noexcept { return Point ((ValueType) (x * multiplier), (ValueType) (y * multiplier)); }
  87. /** Returns a point whose coordinates are divided by a given scalar value. */
  88. template <typename FloatType>
  89. Point operator/ (FloatType divisor) const noexcept { return Point ((ValueType) (x / divisor), (ValueType) (y / divisor)); }
  90. /** Multiplies the point's coordinates by a scalar value. */
  91. template <typename FloatType>
  92. Point& operator*= (FloatType multiplier) noexcept { x = (ValueType) (x * multiplier); y = (ValueType) (y * multiplier); return *this; }
  93. /** Divides the point's coordinates by a scalar value. */
  94. template <typename FloatType>
  95. Point& operator/= (FloatType divisor) noexcept { x = (ValueType) (x / divisor); y = (ValueType) (y / divisor); return *this; }
  96. /** Returns the inverse of this point. */
  97. Point operator-() const noexcept { return Point (-x, -y); }
  98. //==============================================================================
  99. /** This type will be double if the Point's type is double, otherwise it will be float. */
  100. typedef typename TypeHelpers::SmallestFloatType<ValueType>::type FloatType;
  101. //==============================================================================
  102. /** Returns the straight-line distance between this point and the origin. */
  103. ValueType getDistanceFromOrigin() const noexcept { return juce_hypot (x, y); }
  104. /** Returns the straight-line distance between this point and another one. */
  105. ValueType getDistanceFrom (Point other) const noexcept { return juce_hypot (x - other.x, y - other.y); }
  106. /** Returns the square of the straight-line distance between this point and the origin. */
  107. ValueType getDistanceSquaredFromOrigin() const noexcept { return x * x + y * y; }
  108. /** Returns the square of the straight-line distance between this point and another one. */
  109. ValueType getDistanceSquaredFrom (Point other) const noexcept { return (*this - other).getDistanceSquaredFromOrigin(); }
  110. /** Returns the angle from this point to another one.
  111. Taking this point to be the centre of a circle, and the other point being a position on
  112. the circumference, the return value is the number of radians clockwise from the 12 o'clock
  113. direction.
  114. So 12 o'clock = 0, 3 o'clock = Pi/2, 6 o'clock = Pi, 9 o'clock = -Pi/2
  115. */
  116. FloatType getAngleToPoint (Point other) const noexcept
  117. {
  118. return static_cast<FloatType> (std::atan2 (static_cast<FloatType> (other.x - x),
  119. static_cast<FloatType> (y - other.y)));
  120. }
  121. /** Returns the point that would be reached by rotating this point clockwise
  122. about the origin by the specified angle.
  123. */
  124. Point rotatedAboutOrigin (ValueType angleRadians) const noexcept
  125. {
  126. return Point (x * std::cos (angleRadians) - y * std::sin (angleRadians),
  127. x * std::sin (angleRadians) + y * std::cos (angleRadians));
  128. }
  129. /** Taking this point to be the centre of a circle, this returns a point on its circumference.
  130. @param radius the radius of the circle.
  131. @param angle the angle of the point, in radians clockwise from the 12 o'clock position.
  132. */
  133. Point<FloatType> getPointOnCircumference (float radius, float angle) const noexcept
  134. {
  135. return Point<FloatType> (static_cast<FloatType> (x + radius * std::sin (angle)),
  136. static_cast<FloatType> (y - radius * std::cos (angle)));
  137. }
  138. /** Taking this point to be the centre of an ellipse, this returns a point on its circumference.
  139. @param radiusX the horizontal radius of the circle.
  140. @param radiusY the vertical radius of the circle.
  141. @param angle the angle of the point, in radians clockwise from the 12 o'clock position.
  142. */
  143. Point<FloatType> getPointOnCircumference (float radiusX, float radiusY, float angle) const noexcept
  144. {
  145. return Point<FloatType> (static_cast<FloatType> (x + radiusX * std::sin (angle)),
  146. static_cast<FloatType> (y - radiusY * std::cos (angle)));
  147. }
  148. /** Returns the dot-product of two points (x1 * x2 + y1 * y2). */
  149. FloatType getDotProduct (Point other) const noexcept { return x * other.x + y * other.y; }
  150. //==============================================================================
  151. /** Uses a transform to change the point's coordinates.
  152. This will only compile if ValueType = float!
  153. @see AffineTransform::transformPoint
  154. */
  155. void applyTransform (const AffineTransform& transform) noexcept { transform.transformPoint (x, y); }
  156. /** Returns the position of this point, if it is transformed by a given AffineTransform. */
  157. Point transformedBy (const AffineTransform& transform) const noexcept
  158. {
  159. return Point (static_cast<ValueType> (transform.mat00 * x + transform.mat01 * y + transform.mat02),
  160. static_cast<ValueType> (transform.mat10 * x + transform.mat11 * y + transform.mat12));
  161. }
  162. //==============================================================================
  163. /** Casts this point to a Point<int> object. */
  164. Point<int> toInt() const noexcept { return Point<int> (static_cast<int> (x), static_cast<int> (y)); }
  165. /** Casts this point to a Point<float> object. */
  166. Point<float> toFloat() const noexcept { return Point<float> (static_cast<float> (x), static_cast<float> (y)); }
  167. /** Casts this point to a Point<double> object. */
  168. Point<double> toDouble() const noexcept { return Point<double> (static_cast<double> (x), static_cast<double> (y)); }
  169. /** Casts this point to a Point<int> object using roundToInt() to convert the values. */
  170. Point<int> roundToInt() const noexcept { return Point<int> (juce::roundToInt (x), juce::roundToInt (y)); }
  171. /** Returns the point as a string in the form "x, y". */
  172. String toString() const { return String (x) + ", " + String (y); }
  173. //==============================================================================
  174. ValueType x; /**< The point's X coordinate. */
  175. ValueType y; /**< The point's Y coordinate. */
  176. };
  177. /** Multiplies the point's coordinates by a scalar value. */
  178. template <typename ValueType>
  179. Point<ValueType> operator* (ValueType value, Point<ValueType> p) noexcept { return p * value; }
  180. #endif // JUCE_POINT_H_INCLUDED