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.

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