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.

226 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 the point's x coordinate. */
  44. inline ValueType getX() const noexcept { return x; }
  45. /** Returns the point's y coordinate. */
  46. inline ValueType getY() const noexcept { return y; }
  47. /** Sets the point's x coordinate. */
  48. inline void setX (ValueType newX) noexcept { x = newX; }
  49. /** Sets the point's y coordinate. */
  50. inline void setY (ValueType newY) noexcept { y = newY; }
  51. /** Returns a point which has the same Y position as this one, but a new X. */
  52. Point withX (ValueType newX) const noexcept { return Point (newX, y); }
  53. /** Returns a point which has the same X position as this one, but a new Y. */
  54. Point withY (ValueType newY) const noexcept { return Point (x, newY); }
  55. /** Changes the point's x and y coordinates. */
  56. void setXY (ValueType newX, ValueType newY) noexcept { x = newX; y = newY; }
  57. /** Adds a pair of coordinates to this value. */
  58. void addXY (ValueType xToAdd, ValueType yToAdd) noexcept { x += xToAdd; y += yToAdd; }
  59. //==============================================================================
  60. /** Returns a point with a given offset from this one. */
  61. Point translated (ValueType deltaX, ValueType deltaY) const noexcept { return Point (x + deltaX, y + deltaY); }
  62. /** Adds two points together */
  63. Point operator+ (Point other) const noexcept { return Point (x + other.x, y + other.y); }
  64. /** Adds another point's coordinates to this one */
  65. Point& operator+= (Point other) noexcept { x += other.x; y += other.y; return *this; }
  66. /** Subtracts one points from another */
  67. Point operator- (Point other) const noexcept { return Point (x - other.x, y - other.y); }
  68. /** Subtracts another point's coordinates to this one */
  69. Point& operator-= (Point other) noexcept { x -= other.x; y -= other.y; return *this; }
  70. /** Multiplies two points together */
  71. template <typename OtherType>
  72. Point operator* (Point<OtherType> other) const noexcept { return Point ((ValueType) (x * other.x), (ValueType) (y * other.y)); }
  73. /** Multiplies another point's coordinates to this one */
  74. template <typename OtherType>
  75. Point& operator*= (Point<OtherType> other) noexcept { *this = *this * other; return *this; }
  76. /** Divides one point by another */
  77. template <typename OtherType>
  78. Point operator/ (Point<OtherType> other) const noexcept { return Point ((ValueType) (x / other.x), (ValueType) (y / other.y)); }
  79. /** Divides this point's coordinates by another */
  80. template <typename OtherType>
  81. Point& operator/= (Point<OtherType> other) noexcept { *this = *this / other; return *this; }
  82. /** Returns a point whose coordinates are multiplied by a given scalar value. */
  83. template <typename FloatType>
  84. Point operator* (FloatType multiplier) const noexcept { return Point ((ValueType) (x * multiplier), (ValueType) (y * multiplier)); }
  85. /** Returns a point whose coordinates are divided by a given scalar value. */
  86. template <typename FloatType>
  87. Point operator/ (FloatType divisor) const noexcept { return Point ((ValueType) (x / divisor), (ValueType) (y / divisor)); }
  88. /** Multiplies the point's coordinates by a scalar value. */
  89. template <typename FloatType>
  90. Point& operator*= (FloatType multiplier) noexcept { x = (ValueType) (x * multiplier); y = (ValueType) (y * multiplier); return *this; }
  91. /** Divides the point's coordinates by a scalar value. */
  92. template <typename FloatType>
  93. Point& operator/= (FloatType divisor) noexcept { x = (ValueType) (x / divisor); y = (ValueType) (y / divisor); return *this; }
  94. /** Returns the inverse of this point. */
  95. Point operator-() const noexcept { return Point (-x, -y); }
  96. //==============================================================================
  97. /** This type will be double if the Point's type is double, otherwise it will be float. */
  98. typedef typename TypeHelpers::SmallestFloatType<ValueType>::type FloatType;
  99. //==============================================================================
  100. /** Returns the straight-line distance between this point and the origin. */
  101. ValueType getDistanceFromOrigin() const noexcept { return juce_hypot (x, y); }
  102. /** Returns the straight-line distance between this point and another one. */
  103. ValueType getDistanceFrom (Point other) const noexcept { return juce_hypot (x - other.x, y - other.y); }
  104. /** Returns the angle from this point to another one.
  105. The return value is the number of radians clockwise from the 12 o'clock direction,
  106. where this point is the centre and the other point is on the circumference.
  107. */
  108. FloatType getAngleToPoint (Point other) const noexcept
  109. {
  110. return static_cast<FloatType> (std::atan2 (static_cast<FloatType> (other.x - x),
  111. static_cast<FloatType> (y - other.y)));
  112. }
  113. /** Returns the point that would be reached by rotating this point clockwise
  114. about the origin by the specified angle.
  115. */
  116. Point rotatedAboutOrigin (ValueType angleRadians) const noexcept
  117. {
  118. return Point (x * std::cos (angleRadians) - y * std::sin (angleRadians),
  119. x * std::sin (angleRadians) + y * std::cos (angleRadians));
  120. }
  121. /** Taking this point to be the centre of a circle, this returns a point on its circumference.
  122. @param radius the radius of the circle.
  123. @param angle the angle of the point, in radians clockwise from the 12 o'clock position.
  124. */
  125. Point<FloatType> getPointOnCircumference (float radius, float angle) const noexcept
  126. {
  127. return Point<FloatType> (static_cast<FloatType> (x + radius * std::sin (angle)),
  128. static_cast<FloatType> (y - radius * std::cos (angle)));
  129. }
  130. /** Taking this point to be the centre of an ellipse, this returns a point on its circumference.
  131. @param radiusX the horizontal radius of the circle.
  132. @param radiusY the vertical 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 radiusX, float radiusY, float angle) const noexcept
  136. {
  137. return Point<FloatType> (static_cast<FloatType> (x + radiusX * std::sin (angle)),
  138. static_cast<FloatType> (y - radiusY * std::cos (angle)));
  139. }
  140. /** Returns the dot-product of two points (x1 * x2 + y1 * y2). */
  141. FloatType getDotProduct (Point other) const noexcept { return x * other.x + y * other.y; }
  142. //==============================================================================
  143. /** Uses a transform to change the point's coordinates.
  144. This will only compile if ValueType = float!
  145. @see AffineTransform::transformPoint
  146. */
  147. void applyTransform (const AffineTransform& transform) noexcept { transform.transformPoint (x, y); }
  148. /** Returns the position of this point, if it is transformed by a given AffineTransform. */
  149. Point transformedBy (const AffineTransform& transform) const noexcept
  150. {
  151. return Point (static_cast<ValueType> (transform.mat00 * x + transform.mat01 * y + transform.mat02),
  152. static_cast<ValueType> (transform.mat10 * x + transform.mat11 * y + transform.mat12));
  153. }
  154. //==============================================================================
  155. /** Casts this point to a Point<int> object. */
  156. Point<int> toInt() const noexcept { return Point<int> (static_cast<int> (x), static_cast<int> (y)); }
  157. /** Casts this point to a Point<float> object. */
  158. Point<float> toFloat() const noexcept { return Point<float> (static_cast<float> (x), static_cast<float> (y)); }
  159. /** Casts this point to a Point<double> object. */
  160. Point<double> toDouble() const noexcept { return Point<double> (static_cast<double> (x), static_cast<double> (y)); }
  161. /** Returns the point as a string in the form "x, y". */
  162. String toString() const { return String (x) + ", " + String (y); }
  163. //==============================================================================
  164. ValueType x; /**< The point's X coordinate. */
  165. ValueType y; /**< The point's Y coordinate. */
  166. };
  167. #endif // JUCE_POINT_H_INCLUDED