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.

189 lines
9.5KB

  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_JUCEHEADER__
  18. #define __JUCE_POINT_JUCEHEADER__
  19. #include "juce_AffineTransform.h"
  20. //==============================================================================
  21. /**
  22. A pair of (x, y) co-ordinates.
  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. //==============================================================================
  32. /** Creates a point with co-ordinates (0, 0). */
  33. Point() noexcept : x(), y() {}
  34. /** Creates a copy of another point. */
  35. Point (const Point& other) noexcept : x (other.x), y (other.y) {}
  36. /** Creates a point from an (x, y) position. */
  37. Point (const ValueType initialX, const ValueType initialY) noexcept : x (initialX), y (initialY) {}
  38. //==============================================================================
  39. /** Copies this point from another one. */
  40. Point& operator= (const Point& other) noexcept { x = other.x; y = other.y; return *this; }
  41. inline bool operator== (Point other) const noexcept { return x == other.x && y == other.y; }
  42. inline bool operator!= (Point other) const noexcept { return x != other.x || y != other.y; }
  43. /** Returns true if the point is (0, 0). */
  44. bool isOrigin() const noexcept { return x == ValueType() && y == ValueType(); }
  45. /** Returns the point's x co-ordinate. */
  46. inline ValueType getX() const noexcept { return x; }
  47. /** Returns the point's y co-ordinate. */
  48. inline ValueType getY() const noexcept { return y; }
  49. /** Sets the point's x co-ordinate. */
  50. inline void setX (const ValueType newX) noexcept { x = newX; }
  51. /** Sets the point's y co-ordinate. */
  52. inline void setY (const 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 (const 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 (const ValueType newY) const noexcept { return Point (x, newY); }
  57. /** Changes the point's x and y co-ordinates. */
  58. void setXY (const ValueType newX, const ValueType newY) noexcept { x = newX; y = newY; }
  59. /** Adds a pair of co-ordinates to this value. */
  60. void addXY (const ValueType xToAdd, const ValueType yToAdd) noexcept { x += xToAdd; y += yToAdd; }
  61. /** Returns a point with a given offset from this one. */
  62. Point translated (const ValueType xDelta, const ValueType yDelta) const noexcept { return Point (x + xDelta, y + yDelta); }
  63. /** Adds two points together. */
  64. Point operator+ (Point other) const noexcept { return Point (x + other.x, y + other.y); }
  65. /** Adds another point's co-ordinates to this one. */
  66. Point& operator+= (Point other) noexcept { x += other.x; y += other.y; return *this; }
  67. /** Subtracts one points from another. */
  68. Point operator- (Point other) const noexcept { return Point (x - other.x, y - other.y); }
  69. /** Subtracts another point's co-ordinates to this one. */
  70. Point& operator-= (Point other) noexcept { x -= other.x; y -= other.y; return *this; }
  71. /** Returns a point whose coordinates are multiplied by a given value. */
  72. template <typename FloatType>
  73. Point operator* (const FloatType multiplier) const noexcept { return Point ((ValueType) (x * multiplier), (ValueType) (y * multiplier)); }
  74. /** Returns a point whose coordinates are divided by a given value. */
  75. template <typename FloatType>
  76. Point operator/ (const FloatType divisor) const noexcept { return Point ((ValueType) (x / divisor), (ValueType) (y / divisor)); }
  77. /** Multiplies the point's co-ordinates by a value. */
  78. template <typename FloatType>
  79. Point& operator*= (const FloatType multiplier) noexcept { x = (ValueType) (x * multiplier); y = (ValueType) (y * multiplier); return *this; }
  80. /** Divides the point's co-ordinates by a value. */
  81. template <typename FloatType>
  82. Point& operator/= (const FloatType divisor) noexcept { x = (ValueType) (x / divisor); y = (ValueType) (y / divisor); return *this; }
  83. /** Returns the inverse of this point. */
  84. Point operator-() const noexcept { return Point (-x, -y); }
  85. /** Returns the straight-line distance between this point and the origin. */
  86. ValueType getDistanceFromOrigin() const noexcept { return juce_hypot (x, y); }
  87. /** Returns the straight-line distance between this point and another one. */
  88. ValueType getDistanceFrom (Point other) const noexcept { return juce_hypot (x - other.x, y - other.y); }
  89. /** This type will be double if the Point's type is double, otherwise it will be float. */
  90. typedef typename TypeHelpers::SmallestFloatType<ValueType>::type FloatType;
  91. /** Returns the angle from this point to another one.
  92. The return value is the number of radians clockwise from the 12 o'clock direction,
  93. where this point is the centre and the other point is on the circumference.
  94. */
  95. FloatType getAngleToPoint (Point other) const noexcept
  96. { return static_cast<FloatType> (std::atan2 (other.x - x, y - other.y)); }
  97. /** Taking this point to be the centre of a circle, this returns a point on its circumference.
  98. @param radius the radius of the circle.
  99. @param angle the angle of the point, in radians clockwise from the 12 o'clock position.
  100. */
  101. Point<FloatType> getPointOnCircumference (const float radius, const float angle) const noexcept
  102. { return Point<FloatType> (static_cast <FloatType> (x + radius * std::sin (angle)),
  103. static_cast <FloatType> (y - radius * std::cos (angle))); }
  104. /** Taking this point to be the centre of an ellipse, this returns a point on its circumference.
  105. @param radiusX the horizontal radius of the circle.
  106. @param radiusY the vertical radius of the circle.
  107. @param angle the angle of the point, in radians clockwise from the 12 o'clock position.
  108. */
  109. Point<FloatType> getPointOnCircumference (const float radiusX, const float radiusY, const float angle) const noexcept
  110. { return Point<FloatType> (static_cast <FloatType> (x + radiusX * std::sin (angle)),
  111. static_cast <FloatType> (y - radiusY * std::cos (angle))); }
  112. /** Uses a transform to change the point's co-ordinates.
  113. This will only compile if ValueType = float!
  114. @see AffineTransform::transformPoint
  115. */
  116. void applyTransform (const AffineTransform& transform) noexcept { transform.transformPoint (x, y); }
  117. /** Returns the position of this point, if it is transformed by a given AffineTransform. */
  118. Point transformedBy (const AffineTransform& transform) const noexcept
  119. { return Point (transform.mat00 * x + transform.mat01 * y + transform.mat02,
  120. transform.mat10 * x + transform.mat11 * y + transform.mat12); }
  121. /** Returns the dot-product of two points (x1 * x2 + y1 * y2). */
  122. FloatType getDotProduct (Point other) const noexcept { return x * other.x + y * other.y; }
  123. /** Casts this point to a Point<int> object. */
  124. Point<int> toInt() const noexcept { return Point<int> (static_cast <int> (x), static_cast<int> (y)); }
  125. /** Casts this point to a Point<float> object. */
  126. Point<float> toFloat() const noexcept { return Point<float> (static_cast <float> (x), static_cast<float> (y)); }
  127. /** Casts this point to a Point<double> object. */
  128. Point<double> toDouble() const noexcept { return Point<double> (static_cast <double> (x), static_cast<double> (y)); }
  129. /** Returns the point as a string in the form "x, y". */
  130. String toString() const { return String (x) + ", " + String (y); }
  131. //==============================================================================
  132. ValueType x; /**< The point's X coordinate. */
  133. ValueType y; /**< The point's Y coordinate. */
  134. };
  135. #endif // __JUCE_POINT_JUCEHEADER__