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