The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

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