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

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