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 12KB

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