openshot-audio  0.1.6
juce_Point.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission is granted to use this software under the terms of either:
8  a) the GPL v2 (or any later version)
9  b) the Affero GPL v3
10 
11  Details of these licenses can be found at: www.gnu.org/licenses
12 
13  JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17  ------------------------------------------------------------------------------
18 
19  To release a closed-source product which uses JUCE, commercial licenses are
20  available: visit www.juce.com for more information.
21 
22  ==============================================================================
23 */
24 
25 #ifndef JUCE_POINT_H_INCLUDED
26 #define JUCE_POINT_H_INCLUDED
27 
28 
29 //==============================================================================
38 template <typename ValueType>
39 class Point
40 {
41 public:
43  Point() noexcept : x(), y() {}
44 
46  Point (const Point& other) noexcept : x (other.x), y (other.y) {}
47 
49  Point (ValueType initialX, ValueType initialY) noexcept : x (initialX), y (initialY) {}
50 
51  //==============================================================================
53  Point& operator= (const Point& other) noexcept { x = other.x; y = other.y; return *this; }
54 
55  inline bool operator== (Point other) const noexcept { return x == other.x && y == other.y; }
56  inline bool operator!= (Point other) const noexcept { return x != other.x || y != other.y; }
57 
59  bool isOrigin() const noexcept { return x == ValueType() && y == ValueType(); }
60 
62  inline bool isFinite() const noexcept { return juce_isfinite(x) && juce_isfinite(y); }
63 
65  inline ValueType getX() const noexcept { return x; }
66 
68  inline ValueType getY() const noexcept { return y; }
69 
71  inline void setX (ValueType newX) noexcept { x = newX; }
72 
74  inline void setY (ValueType newY) noexcept { y = newY; }
75 
77  Point withX (ValueType newX) const noexcept { return Point (newX, y); }
78 
80  Point withY (ValueType newY) const noexcept { return Point (x, newY); }
81 
83  void setXY (ValueType newX, ValueType newY) noexcept { x = newX; y = newY; }
84 
86  void addXY (ValueType xToAdd, ValueType yToAdd) noexcept { x += xToAdd; y += yToAdd; }
87 
88  //==============================================================================
90  Point translated (ValueType deltaX, ValueType deltaY) const noexcept { return Point (x + deltaX, y + deltaY); }
91 
93  Point operator+ (Point other) const noexcept { return Point (x + other.x, y + other.y); }
94 
96  Point& operator+= (Point other) noexcept { x += other.x; y += other.y; return *this; }
97 
99  Point operator- (Point other) const noexcept { return Point (x - other.x, y - other.y); }
100 
102  Point& operator-= (Point other) noexcept { x -= other.x; y -= other.y; return *this; }
103 
105  template <typename OtherType>
106  Point operator* (Point<OtherType> other) const noexcept { return Point ((ValueType) (x * other.x), (ValueType) (y * other.y)); }
107 
109  template <typename OtherType>
110  Point& operator*= (Point<OtherType> other) noexcept { *this = *this * other; return *this; }
111 
113  template <typename OtherType>
114  Point operator/ (Point<OtherType> other) const noexcept { return Point ((ValueType) (x / other.x), (ValueType) (y / other.y)); }
115 
117  template <typename OtherType>
118  Point& operator/= (Point<OtherType> other) noexcept { *this = *this / other; return *this; }
119 
121  template <typename FloatType>
122  Point operator* (FloatType multiplier) const noexcept { return Point ((ValueType) (x * multiplier), (ValueType) (y * multiplier)); }
123 
125  template <typename FloatType>
126  Point operator/ (FloatType divisor) const noexcept { return Point ((ValueType) (x / divisor), (ValueType) (y / divisor)); }
127 
129  template <typename FloatType>
130  Point& operator*= (FloatType multiplier) noexcept { x = (ValueType) (x * multiplier); y = (ValueType) (y * multiplier); return *this; }
131 
133  template <typename FloatType>
134  Point& operator/= (FloatType divisor) noexcept { x = (ValueType) (x / divisor); y = (ValueType) (y / divisor); return *this; }
135 
137  Point operator-() const noexcept { return Point (-x, -y); }
138 
139  //==============================================================================
142 
143  //==============================================================================
145  ValueType getDistanceFromOrigin() const noexcept { return juce_hypot (x, y); }
146 
148  ValueType getDistanceFrom (Point other) const noexcept { return juce_hypot (x - other.x, y - other.y); }
149 
155  FloatType getAngleToPoint (Point other) const noexcept
156  {
157  return static_cast<FloatType> (std::atan2 (static_cast<FloatType> (other.x - x),
158  static_cast<FloatType> (y - other.y)));
159  }
160 
164  Point rotatedAboutOrigin (ValueType angleRadians) const noexcept
165  {
166  return Point (x * std::cos (angleRadians) - y * std::sin (angleRadians),
167  x * std::sin (angleRadians) + y * std::cos (angleRadians));
168  }
169 
174  Point<FloatType> getPointOnCircumference (float radius, float angle) const noexcept
175  {
176  return Point<FloatType> (static_cast<FloatType> (x + radius * std::sin (angle)),
177  static_cast<FloatType> (y - radius * std::cos (angle)));
178  }
179 
185  Point<FloatType> getPointOnCircumference (float radiusX, float radiusY, float angle) const noexcept
186  {
187  return Point<FloatType> (static_cast<FloatType> (x + radiusX * std::sin (angle)),
188  static_cast<FloatType> (y - radiusY * std::cos (angle)));
189  }
190 
192  FloatType getDotProduct (Point other) const noexcept { return x * other.x + y * other.y; }
193 
194  //==============================================================================
200  void applyTransform (const AffineTransform& transform) noexcept { transform.transformPoint (x, y); }
201 
203  Point transformedBy (const AffineTransform& transform) const noexcept
204  {
205  return Point (static_cast<ValueType> (transform.mat00 * x + transform.mat01 * y + transform.mat02),
206  static_cast<ValueType> (transform.mat10 * x + transform.mat11 * y + transform.mat12));
207  }
208 
209  //==============================================================================
211  Point<int> toInt() const noexcept { return Point<int> (static_cast<int> (x), static_cast<int> (y)); }
212 
214  Point<float> toFloat() const noexcept { return Point<float> (static_cast<float> (x), static_cast<float> (y)); }
215 
217  Point<double> toDouble() const noexcept { return Point<double> (static_cast<double> (x), static_cast<double> (y)); }
218 
221 
223  String toString() const { return String (x) + ", " + String (y); }
224 
225  //==============================================================================
226  ValueType x;
227  ValueType y;
228 };
229 
230 
231 #endif // JUCE_POINT_H_INCLUDED
Point & operator=(const Point &other) noexcept
Definition: juce_Point.h:53
ValueType getY() const noexcept
Definition: juce_Point.h:68
#define noexcept
Definition: juce_CompilerSupport.h:141
Point(ValueType initialX, ValueType initialY) noexcept
Definition: juce_Point.h:49
ValueType getDistanceFrom(Point other) const noexcept
Definition: juce_Point.h:148
Point translated(ValueType deltaX, ValueType deltaY) const noexcept
Definition: juce_Point.h:90
int roundToInt(const FloatType value) noexcept
Definition: juce_core.h:418
Point< double > toDouble() const noexcept
Definition: juce_Point.h:217
ValueType getDistanceFromOrigin() const noexcept
Definition: juce_Point.h:145
Point< FloatType > getPointOnCircumference(float radius, float angle) const noexcept
Definition: juce_Point.h:174
Definition: juce_Point.h:39
Definition: juce_String.h:43
FloatType getAngleToPoint(Point other) const noexcept
Definition: juce_Point.h:155
Point(const Point &other) noexcept
Definition: juce_Point.h:46
#define const
Point withX(ValueType newX) const noexcept
Definition: juce_Point.h:77
bool operator==(Point other) const noexcept
Definition: juce_Point.h:55
void setX(ValueType newX) noexcept
Definition: juce_Point.h:71
Point transformedBy(const AffineTransform &transform) const noexcept
Definition: juce_Point.h:203
Point< float > toFloat() const noexcept
Definition: juce_Point.h:214
FloatType getDotProduct(Point other) const noexcept
Definition: juce_Point.h:192
Point & operator+=(Point other) noexcept
Definition: juce_Point.h:96
bool isOrigin() const noexcept
Definition: juce_Point.h:59
Point< int > roundToInt() const noexcept
Definition: juce_Point.h:220
Point & operator/=(Point< OtherType > other) noexcept
Definition: juce_Point.h:118
Point operator-() const noexcept
Definition: juce_Point.h:137
void setXY(ValueType newX, ValueType newY) noexcept
Definition: juce_Point.h:83
void setY(ValueType newY) noexcept
Definition: juce_Point.h:74
Point operator/(Point< OtherType > other) const noexcept
Definition: juce_Point.h:114
void applyTransform(const AffineTransform &transform) noexcept
Definition: juce_Point.h:200
Type juce_hypot(Type a, Type b) noexcept
Definition: juce_core.h:313
Point & operator*=(Point< OtherType > other) noexcept
Definition: juce_Point.h:110
Point & operator-=(Point other) noexcept
Definition: juce_Point.h:102
float type
Definition: juce_MathsFunctions.h:607
Point< int > toInt() const noexcept
Definition: juce_Point.h:211
bool operator!=(Point other) const noexcept
Definition: juce_Point.h:56
Point operator+(Point other) const noexcept
Definition: juce_Point.h:93
ValueType getX() const noexcept
Definition: juce_Point.h:65
bool isFinite() const noexcept
Definition: juce_Point.h:62
Definition: juce_AffineTransform.h:40
bool juce_isfinite(NumericType) noexcept
Definition: juce_core.h:374
Point rotatedAboutOrigin(ValueType angleRadians) const noexcept
Definition: juce_Point.h:164
TypeHelpers::SmallestFloatType< int >::type FloatType
Definition: juce_Point.h:141
Point withY(ValueType newY) const noexcept
Definition: juce_Point.h:80
Point() noexcept
Definition: juce_Point.h:43
Point operator*(Point< OtherType > other) const noexcept
Definition: juce_Point.h:106
ValueType x
Definition: juce_Point.h:226
Point< FloatType > getPointOnCircumference(float radiusX, float radiusY, float angle) const noexcept
Definition: juce_Point.h:185
void addXY(ValueType xToAdd, ValueType yToAdd) noexcept
Definition: juce_Point.h:86
ValueType y
Definition: juce_Point.h:227
String toString() const
Definition: juce_Point.h:223