JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
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) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29//==============================================================================
40template <typename ValueType>
41class Point
42{
43public:
45 constexpr Point() = default;
46
48 constexpr Point (const Point&) = default;
49
51 constexpr Point (ValueType initialX, ValueType initialY) noexcept : x (initialX), y (initialY) {}
52
53 //==============================================================================
55 Point& operator= (const Point&) = default;
56
57 constexpr inline bool operator== (Point other) const noexcept
58 {
59 const auto tie = [] (const Point& p) { return std::tie (p.x, p.y); };
60 return tie (*this) == tie (other);
61 }
62
63 constexpr inline bool operator!= (Point other) const noexcept { return ! operator== (other); }
64
66 constexpr bool isOrigin() const noexcept { return operator== (Point()); }
67
69 constexpr inline bool isFinite() const noexcept { return juce_isfinite (x) && juce_isfinite (y); }
70
72 constexpr inline ValueType getX() const noexcept { return x; }
73
75 constexpr inline ValueType getY() const noexcept { return y; }
76
78 inline void setX (ValueType newX) noexcept { x = newX; }
79
81 inline void setY (ValueType newY) noexcept { y = newY; }
82
84 constexpr Point withX (ValueType newX) const noexcept { return Point (newX, y); }
85
87 constexpr Point withY (ValueType newY) const noexcept { return Point (x, newY); }
88
90 void setXY (ValueType newX, ValueType newY) noexcept { x = newX; y = newY; }
91
93 void addXY (ValueType xToAdd, ValueType yToAdd) noexcept { x += xToAdd; y += yToAdd; }
94
95 //==============================================================================
97 constexpr Point translated (ValueType deltaX, ValueType deltaY) const noexcept { return Point (x + deltaX, y + deltaY); }
98
100 constexpr Point operator+ (Point other) const noexcept { return Point (x + other.x, y + other.y); }
101
103 Point& operator+= (Point other) noexcept { x += other.x; y += other.y; return *this; }
104
106 constexpr Point operator- (Point other) const noexcept { return Point (x - other.x, y - other.y); }
107
109 Point& operator-= (Point other) noexcept { x -= other.x; y -= other.y; return *this; }
110
112 template <typename OtherType>
113 constexpr Point operator* (Point<OtherType> other) const noexcept { return Point ((ValueType) (x * other.x), (ValueType) (y * other.y)); }
114
116 template <typename OtherType>
117 Point& operator*= (Point<OtherType> other) noexcept { *this = *this * other; return *this; }
118
120 template <typename OtherType>
121 constexpr Point operator/ (Point<OtherType> other) const noexcept { return Point ((ValueType) (x / other.x), (ValueType) (y / other.y)); }
122
124 template <typename OtherType>
125 Point& operator/= (Point<OtherType> other) noexcept { *this = *this / other; return *this; }
126
128 template <typename OtherType>
129 constexpr Point operator* (OtherType multiplier) const noexcept
130 {
132 return Point ((ValueType) ((CommonType) x * (CommonType) multiplier),
133 (ValueType) ((CommonType) y * (CommonType) multiplier));
134 }
135
137 template <typename OtherType>
138 constexpr Point operator/ (OtherType divisor) const noexcept
139 {
141 return Point ((ValueType) ((CommonType) x / (CommonType) divisor),
142 (ValueType) ((CommonType) y / (CommonType) divisor));
143 }
144
146 template <typename FloatType>
147 Point& operator*= (FloatType multiplier) noexcept { x = (ValueType) (x * multiplier); y = (ValueType) (y * multiplier); return *this; }
148
150 template <typename FloatType>
151 Point& operator/= (FloatType divisor) noexcept { x = (ValueType) (x / divisor); y = (ValueType) (y / divisor); return *this; }
152
154 constexpr Point operator-() const noexcept { return Point (-x, -y); }
155
156 //==============================================================================
159
160 //==============================================================================
162 ValueType getDistanceFromOrigin() const noexcept { return juce_hypot (x, y); }
163
165 ValueType getDistanceFrom (Point other) const noexcept { return juce_hypot (x - other.x, y - other.y); }
166
168 constexpr ValueType getDistanceSquaredFromOrigin() const noexcept { return x * x + y * y; }
169
171 constexpr ValueType getDistanceSquaredFrom (Point other) const noexcept { return (*this - other).getDistanceSquaredFromOrigin(); }
172
181 {
182 return static_cast<FloatType> (std::atan2 (static_cast<FloatType> (other.x - x),
183 static_cast<FloatType> (y - other.y)));
184 }
185
189 Point rotatedAboutOrigin (ValueType angleRadians) const noexcept
190 {
193 }
194
199 Point<FloatType> getPointOnCircumference (float radius, float angle) const noexcept
200 {
201 return Point<FloatType> (static_cast<FloatType> (x + radius * std::sin (angle)),
202 static_cast<FloatType> (y - radius * std::cos (angle)));
203 }
204
210 Point<FloatType> getPointOnCircumference (float radiusX, float radiusY, float angle) const noexcept
211 {
212 return Point<FloatType> (static_cast<FloatType> (x + radiusX * std::sin (angle)),
213 static_cast<FloatType> (y - radiusY * std::cos (angle)));
214 }
215
217 constexpr FloatType getDotProduct (Point other) const noexcept { return x * other.x + y * other.y; }
218
219 //==============================================================================
225 void applyTransform (const AffineTransform& transform) noexcept { transform.transformPoint (x, y); }
226
228 Point transformedBy (const AffineTransform& transform) const noexcept
229 {
230 return Point (static_cast<ValueType> (transform.mat00 * (float) x + transform.mat01 * (float) y + transform.mat02),
231 static_cast<ValueType> (transform.mat10 * (float) x + transform.mat11 * (float) y + transform.mat12));
232 }
233
234 //==============================================================================
236 constexpr Point<int> toInt() const noexcept { return Point<int> (static_cast<int> (x), static_cast<int> (y)); }
237
239 constexpr Point<float> toFloat() const noexcept { return Point<float> (static_cast<float> (x), static_cast<float> (y)); }
240
242 constexpr Point<double> toDouble() const noexcept { return Point<double> (static_cast<double> (x), static_cast<double> (y)); }
243
246
248 String toString() const { return String (x) + ", " + String (y); }
249
250 //==============================================================================
251 ValueType x{};
252 ValueType y{};
253};
254
256template <typename ValueType>
257Point<ValueType> operator* (ValueType value, Point<ValueType> p) noexcept { return p * value; }
258
259} // namespace juce
T atan2(T... args)
Represents a 2D affine-transformation matrix.
A pair of (x, y) coordinates.
Definition juce_Point.h:42
constexpr FloatType getDotProduct(Point other) const noexcept
Returns the dot-product of two points (x1 * x2 + y1 * y2).
Definition juce_Point.h:217
constexpr Point translated(ValueType deltaX, ValueType deltaY) const noexcept
Returns a point with a given offset from this one.
Definition juce_Point.h:97
ValueType getDistanceFrom(Point other) const noexcept
Returns the straight-line distance between this point and another one.
Definition juce_Point.h:165
Point rotatedAboutOrigin(ValueType angleRadians) const noexcept
Returns the point that would be reached by rotating this point clockwise about the origin by the spec...
Definition juce_Point.h:189
FloatType getAngleToPoint(Point other) const noexcept
Returns the angle from this point to another one.
Definition juce_Point.h:180
void setY(ValueType newY) noexcept
Sets the point's y coordinate.
Definition juce_Point.h:81
constexpr Point< float > toFloat() const noexcept
Casts this point to a Point<float> object.
Definition juce_Point.h:239
constexpr Point operator+(Point other) const noexcept
Adds two points together.
Definition juce_Point.h:100
constexpr Point operator-() const noexcept
Returns the inverse of this point.
Definition juce_Point.h:154
constexpr Point withY(ValueType newY) const noexcept
Returns a point which has the same X position as this one, but a new Y.
Definition juce_Point.h:87
void setXY(ValueType newX, ValueType newY) noexcept
Changes the point's x and y coordinates.
Definition juce_Point.h:90
constexpr ValueType getY() const noexcept
Returns the point's y coordinate.
Definition juce_Point.h:75
constexpr ValueType getDistanceSquaredFromOrigin() const noexcept
Returns the square of the straight-line distance between this point and the origin.
Definition juce_Point.h:168
void setX(ValueType newX) noexcept
Sets the point's x coordinate.
Definition juce_Point.h:78
constexpr Point()=default
Creates a point at the origin.
constexpr Point< int > roundToInt() const noexcept
Casts this point to a Point<int> object using roundToInt() to convert the values.
Definition juce_Point.h:245
constexpr ValueType getX() const noexcept
Returns the point's x coordinate.
Definition juce_Point.h:72
constexpr Point< int > toInt() const noexcept
Casts this point to a Point<int> object.
Definition juce_Point.h:236
constexpr Point< double > toDouble() const noexcept
Casts this point to a Point<double> object.
Definition juce_Point.h:242
String toString() const
Returns the point as a string in the form "x, y".
Definition juce_Point.h:248
void applyTransform(const AffineTransform &transform) noexcept
Uses a transform to change the point's coordinates.
Definition juce_Point.h:225
Point & operator+=(Point other) noexcept
Adds another point's coordinates to this one.
Definition juce_Point.h:103
Point & operator*=(Point< OtherType > other) noexcept
Multiplies another point's coordinates to this one.
Definition juce_Point.h:117
constexpr Point operator/(Point< OtherType > other) const noexcept
Divides one point by another.
Definition juce_Point.h:121
constexpr bool isOrigin() const noexcept
Returns true if the point is (0, 0).
Definition juce_Point.h:66
ValueType y
The point's Y coordinate.
Definition juce_Point.h:252
void addXY(ValueType xToAdd, ValueType yToAdd) noexcept
Adds a pair of coordinates to this value.
Definition juce_Point.h:93
Point transformedBy(const AffineTransform &transform) const noexcept
Returns the position of this point, if it is transformed by a given AffineTransform.
Definition juce_Point.h:228
constexpr Point(const Point &)=default
Creates a copy of another point.
constexpr bool isFinite() const noexcept
Returns true if the coordinates are finite values.
Definition juce_Point.h:69
Point & operator-=(Point other) noexcept
Subtracts another point's coordinates to this one.
Definition juce_Point.h:109
constexpr ValueType getDistanceSquaredFrom(Point other) const noexcept
Returns the square of the straight-line distance between this point and another one.
Definition juce_Point.h:171
constexpr Point operator*(Point< OtherType > other) const noexcept
Multiplies two points together.
Definition juce_Point.h:113
ValueType x
The point's X coordinate.
Definition juce_Point.h:251
constexpr Point withX(ValueType newX) const noexcept
Returns a point which has the same Y position as this one, but a new X.
Definition juce_Point.h:84
Point< FloatType > getPointOnCircumference(float radiusX, float radiusY, float angle) const noexcept
Taking this point to be the centre of an ellipse, this returns a point on its circumference.
Definition juce_Point.h:210
Point & operator=(const Point &)=default
Copies this point from another one.
Point & operator/=(Point< OtherType > other) noexcept
Divides this point's coordinates by another.
Definition juce_Point.h:125
Point< FloatType > getPointOnCircumference(float radius, float angle) const noexcept
Taking this point to be the centre of a circle, this returns a point on its circumference.
Definition juce_Point.h:199
ValueType getDistanceFromOrigin() const noexcept
Returns the straight-line distance between this point and the origin.
Definition juce_Point.h:162
constexpr Point(ValueType initialX, ValueType initialY) noexcept
Creates a point from an (x, y) position.
Definition juce_Point.h:51
The JUCE String class!
Definition juce_String.h:53
T cos(T... args)
JUCE Namespace.
const DirectoryEntry & operator*(const DirectoryEntry &e) noexcept
A convenience operator so that the expression *it++ works correctly when it is an instance of RangedD...
Type juce_hypot(Type a, Type b) noexcept
Using juce_hypot is easier than dealing with the different types of hypot function that are provided ...
bool juce_isfinite(NumericType value) noexcept
The isfinite() method seems to vary between platforms, so this is a platform-independent function for...
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.
T sin(T... args)
T tie(T... args)