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.

juce_Point.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A pair of (x, y) coordinates.
  23. The ValueType template should be a primitive type such as int, float, double,
  24. rather than a class.
  25. @see Line, Path, AffineTransform
  26. @tags{Graphics}
  27. */
  28. template <typename ValueType>
  29. class Point
  30. {
  31. public:
  32. /** Creates a point at the origin */
  33. constexpr Point() = default;
  34. /** Creates a copy of another point. */
  35. constexpr Point (const Point&) = default;
  36. /** Creates a point from an (x, y) position. */
  37. constexpr Point (ValueType initialX, ValueType initialY) noexcept : x (initialX), y (initialY) {}
  38. //==============================================================================
  39. /** Copies this point from another one. */
  40. Point& operator= (const Point&) = default;
  41. constexpr inline bool operator== (Point other) const noexcept { return x == other.x && y == other.y; }
  42. 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. constexpr bool isOrigin() const noexcept { return x == ValueType() && y == ValueType(); }
  45. /** Returns true if the coordinates are finite values. */
  46. constexpr inline bool isFinite() const noexcept { return juce_isfinite(x) && juce_isfinite(y); }
  47. /** Returns the point's x coordinate. */
  48. constexpr inline ValueType getX() const noexcept { return x; }
  49. /** Returns the point's y coordinate. */
  50. 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. 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. 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. constexpr Point translated (ValueType deltaX, ValueType deltaY) const noexcept { return Point (x + deltaX, y + deltaY); }
  66. /** Adds two points together */
  67. 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. 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. 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. 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 OtherType>
  88. constexpr Point operator* (OtherType multiplier) const noexcept
  89. {
  90. using CommonType = typename std::common_type<ValueType, OtherType>::type;
  91. return Point ((ValueType) ((CommonType) x * (CommonType) multiplier),
  92. (ValueType) ((CommonType) y * (CommonType) multiplier));
  93. }
  94. /** Returns a point whose coordinates are divided by a given scalar value. */
  95. template <typename OtherType>
  96. constexpr Point operator/ (OtherType divisor) const noexcept
  97. {
  98. using CommonType = typename std::common_type<ValueType, OtherType>::type;
  99. return Point ((ValueType) ((CommonType) x / (CommonType) divisor),
  100. (ValueType) ((CommonType) y / (CommonType) divisor));
  101. }
  102. /** Multiplies the point's coordinates by a scalar value. */
  103. template <typename FloatType>
  104. Point& operator*= (FloatType multiplier) noexcept { x = (ValueType) (x * multiplier); y = (ValueType) (y * multiplier); return *this; }
  105. /** Divides the point's coordinates by a scalar value. */
  106. template <typename FloatType>
  107. Point& operator/= (FloatType divisor) noexcept { x = (ValueType) (x / divisor); y = (ValueType) (y / divisor); return *this; }
  108. /** Returns the inverse of this point. */
  109. constexpr Point operator-() const noexcept { return Point (-x, -y); }
  110. //==============================================================================
  111. /** This type will be double if the Point's type is double, otherwise it will be float. */
  112. using FloatType = typename TypeHelpers::SmallestFloatType<ValueType>::type;
  113. //==============================================================================
  114. /** Returns the straight-line distance between this point and the origin. */
  115. ValueType getDistanceFromOrigin() const noexcept { return juce_hypot (x, y); }
  116. /** Returns the straight-line distance between this point and another one. */
  117. ValueType getDistanceFrom (Point other) const noexcept { return juce_hypot (x - other.x, y - other.y); }
  118. /** Returns the square of the straight-line distance between this point and the origin. */
  119. constexpr ValueType getDistanceSquaredFromOrigin() const noexcept { return x * x + y * y; }
  120. /** Returns the square of the straight-line distance between this point and another one. */
  121. constexpr ValueType getDistanceSquaredFrom (Point other) const noexcept { return (*this - other).getDistanceSquaredFromOrigin(); }
  122. /** Returns the angle from this point to another one.
  123. Taking this point to be the centre of a circle, and the other point being a position on
  124. the circumference, the return value is the number of radians clockwise from the 12 o'clock
  125. direction.
  126. So 12 o'clock = 0, 3 o'clock = Pi/2, 6 o'clock = Pi, 9 o'clock = -Pi/2
  127. */
  128. FloatType getAngleToPoint (Point other) const noexcept
  129. {
  130. return static_cast<FloatType> (std::atan2 (static_cast<FloatType> (other.x - x),
  131. static_cast<FloatType> (y - other.y)));
  132. }
  133. /** Returns the point that would be reached by rotating this point clockwise
  134. about the origin by the specified angle.
  135. */
  136. Point rotatedAboutOrigin (ValueType angleRadians) const noexcept
  137. {
  138. return Point (x * std::cos (angleRadians) - y * std::sin (angleRadians),
  139. x * std::sin (angleRadians) + y * std::cos (angleRadians));
  140. }
  141. /** Taking this point to be the centre of a circle, this returns a point on its circumference.
  142. @param radius the 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 radius, float angle) const noexcept
  146. {
  147. return Point<FloatType> (static_cast<FloatType> (x + radius * std::sin (angle)),
  148. static_cast<FloatType> (y - radius * std::cos (angle)));
  149. }
  150. /** Taking this point to be the centre of an ellipse, this returns a point on its circumference.
  151. @param radiusX the horizontal radius of the circle.
  152. @param radiusY the vertical radius of the circle.
  153. @param angle the angle of the point, in radians clockwise from the 12 o'clock position.
  154. */
  155. Point<FloatType> getPointOnCircumference (float radiusX, float radiusY, float angle) const noexcept
  156. {
  157. return Point<FloatType> (static_cast<FloatType> (x + radiusX * std::sin (angle)),
  158. static_cast<FloatType> (y - radiusY * std::cos (angle)));
  159. }
  160. /** Returns the dot-product of two points (x1 * x2 + y1 * y2). */
  161. constexpr FloatType getDotProduct (Point other) const noexcept { return x * other.x + y * other.y; }
  162. //==============================================================================
  163. /** Uses a transform to change the point's coordinates.
  164. This will only compile if ValueType = float!
  165. @see AffineTransform::transformPoint
  166. */
  167. void applyTransform (const AffineTransform& transform) noexcept { transform.transformPoint (x, y); }
  168. /** Returns the position of this point, if it is transformed by a given AffineTransform. */
  169. Point transformedBy (const AffineTransform& transform) const noexcept
  170. {
  171. return Point (static_cast<ValueType> (transform.mat00 * (float) x + transform.mat01 * (float) y + transform.mat02),
  172. static_cast<ValueType> (transform.mat10 * (float) x + transform.mat11 * (float) y + transform.mat12));
  173. }
  174. //==============================================================================
  175. /** Casts this point to a Point<int> object. */
  176. constexpr Point<int> toInt() const noexcept { return Point<int> (static_cast<int> (x), static_cast<int> (y)); }
  177. /** Casts this point to a Point<float> object. */
  178. constexpr Point<float> toFloat() const noexcept { return Point<float> (static_cast<float> (x), static_cast<float> (y)); }
  179. /** Casts this point to a Point<double> object. */
  180. constexpr Point<double> toDouble() const noexcept { return Point<double> (static_cast<double> (x), static_cast<double> (y)); }
  181. /** Casts this point to a Point<int> object using roundToInt() to convert the values. */
  182. constexpr Point<int> roundToInt() const noexcept { return Point<int> (juce::roundToInt (x), juce::roundToInt (y)); }
  183. /** Returns the point as a string in the form "x, y". */
  184. String toString() const { return String (x) + ", " + String (y); }
  185. //==============================================================================
  186. ValueType x{}; /**< The point's X coordinate. */
  187. ValueType y{}; /**< The point's Y coordinate. */
  188. };
  189. /** Multiplies the point's coordinates by a scalar value. */
  190. template <typename ValueType>
  191. Point<ValueType> operator* (ValueType value, Point<ValueType> p) noexcept { return p * value; }
  192. } // namespace juce