LMMS
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 { return x == other.x && y == other.y; }
58 constexpr inline bool operator!= (Point other) const noexcept { return x != other.x || y != other.y; }
59
61 constexpr bool isOrigin() const noexcept { return x == ValueType() && y == ValueType(); }
62
64 constexpr inline bool isFinite() const noexcept { return juce_isfinite(x) && juce_isfinite(y); }
65
67 constexpr inline ValueType getX() const noexcept { return x; }
68
70 constexpr inline ValueType getY() const noexcept { return y; }
71
73 inline void setX (ValueType newX) noexcept { x = newX; }
74
76 inline void setY (ValueType newY) noexcept { y = newY; }
77
79 constexpr Point withX (ValueType newX) const noexcept { return Point (newX, y); }
80
82 constexpr Point withY (ValueType newY) const noexcept { return Point (x, newY); }
83
85 void setXY (ValueType newX, ValueType newY) noexcept { x = newX; y = newY; }
86
88 void addXY (ValueType xToAdd, ValueType yToAdd) noexcept { x += xToAdd; y += yToAdd; }
89
90 //==============================================================================
92 constexpr Point translated (ValueType deltaX, ValueType deltaY) const noexcept { return Point (x + deltaX, y + deltaY); }
93
95 constexpr Point operator+ (Point other) const noexcept { return Point (x + other.x, y + other.y); }
96
98 Point& operator+= (Point other) noexcept { x += other.x; y += other.y; return *this; }
99
101 constexpr Point operator- (Point other) const noexcept { return Point (x - other.x, y - other.y); }
102
104 Point& operator-= (Point other) noexcept { x -= other.x; y -= other.y; return *this; }
105
107 template <typename OtherType>
108 constexpr Point operator* (Point<OtherType> other) const noexcept { return Point ((ValueType) (x * other.x), (ValueType) (y * other.y)); }
109
111 template <typename OtherType>
112 Point& operator*= (Point<OtherType> other) noexcept { *this = *this * other; return *this; }
113
115 template <typename OtherType>
116 constexpr Point operator/ (Point<OtherType> other) const noexcept { return Point ((ValueType) (x / other.x), (ValueType) (y / other.y)); }
117
119 template <typename OtherType>
120 Point& operator/= (Point<OtherType> other) noexcept { *this = *this / other; return *this; }
121
123 template <typename OtherType>
124 constexpr Point operator* (OtherType multiplier) const noexcept
125 {
126 using CommonType = typename std::common_type<ValueType, OtherType>::type;
127 return Point ((ValueType) ((CommonType) x * (CommonType) multiplier),
128 (ValueType) ((CommonType) y * (CommonType) multiplier));
129 }
130
132 template <typename OtherType>
133 constexpr Point operator/ (OtherType divisor) const noexcept
134 {
135 using CommonType = typename std::common_type<ValueType, OtherType>::type;
136 return Point ((ValueType) ((CommonType) x / (CommonType) divisor),
137 (ValueType) ((CommonType) y / (CommonType) divisor));
138 }
139
141 template <typename FloatType>
142 Point& operator*= (FloatType multiplier) noexcept { x = (ValueType) (x * multiplier); y = (ValueType) (y * multiplier); return *this; }
143
145 template <typename FloatType>
146 Point& operator/= (FloatType divisor) noexcept { x = (ValueType) (x / divisor); y = (ValueType) (y / divisor); return *this; }
147
149 constexpr Point operator-() const noexcept { return Point (-x, -y); }
150
151 //==============================================================================
154
155 //==============================================================================
158
160 ValueType getDistanceFrom (Point other) const noexcept { return juce_hypot (x - other.x, y - other.y); }
161
163 constexpr ValueType getDistanceSquaredFromOrigin() const noexcept { return x * x + y * y; }
164
166 constexpr ValueType getDistanceSquaredFrom (Point other) const noexcept { return (*this - other).getDistanceSquaredFromOrigin(); }
167
175 FloatType getAngleToPoint (Point other) const noexcept
176 {
177 return static_cast<FloatType> (std::atan2 (static_cast<FloatType> (other.x - x),
178 static_cast<FloatType> (y - other.y)));
179 }
180
184 Point rotatedAboutOrigin (ValueType angleRadians) const noexcept
185 {
186 return Point (x * std::cos (angleRadians) - y * std::sin (angleRadians),
187 x * std::sin (angleRadians) + y * std::cos (angleRadians));
188 }
189
194 Point<FloatType> getPointOnCircumference (float radius, float angle) const noexcept
195 {
196 return Point<FloatType> (static_cast<FloatType> (x + radius * std::sin (angle)),
197 static_cast<FloatType> (y - radius * std::cos (angle)));
198 }
199
205 Point<FloatType> getPointOnCircumference (float radiusX, float radiusY, float angle) const noexcept
206 {
207 return Point<FloatType> (static_cast<FloatType> (x + radiusX * std::sin (angle)),
208 static_cast<FloatType> (y - radiusY * std::cos (angle)));
209 }
210
212 constexpr FloatType getDotProduct (Point other) const noexcept { return x * other.x + y * other.y; }
213
214 //==============================================================================
220 void applyTransform (const AffineTransform& transform) noexcept { transform.transformPoint (x, y); }
221
223 Point transformedBy (const AffineTransform& transform) const noexcept
224 {
225 return Point (static_cast<ValueType> (transform.mat00 * (float) x + transform.mat01 * (float) y + transform.mat02),
226 static_cast<ValueType> (transform.mat10 * (float) x + transform.mat11 * (float) y + transform.mat12));
227 }
228
229 //==============================================================================
231 constexpr Point<int> toInt() const noexcept { return Point<int> (static_cast<int> (x), static_cast<int> (y)); }
232
234 constexpr Point<float> toFloat() const noexcept { return Point<float> (static_cast<float> (x), static_cast<float> (y)); }
235
237 constexpr Point<double> toDouble() const noexcept { return Point<double> (static_cast<double> (x), static_cast<double> (y)); }
238
241
243 String toString() const { return String (x) + ", " + String (y); }
244
245 //==============================================================================
246 ValueType x{};
247 ValueType y{};
248};
249
251template <typename ValueType>
252Point<ValueType> operator* (ValueType value, Point<ValueType> p) noexcept { return p * value; }
253
254} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
Definition juce_AffineTransform.h:43
Definition juce_Point.h:42
constexpr FloatType getDotProduct(Point other) const noexcept
Definition juce_Point.h:212
constexpr Point translated(ValueType deltaX, ValueType deltaY) const noexcept
Definition juce_Point.h:92
ValueType getDistanceFrom(Point other) const noexcept
Definition juce_Point.h:160
Point rotatedAboutOrigin(ValueType angleRadians) const noexcept
Definition juce_Point.h:184
FloatType getAngleToPoint(Point other) const noexcept
Definition juce_Point.h:175
constexpr Point< float > toFloat() const noexcept
Definition juce_Point.h:234
constexpr Point()=default
Definition Geometry.cpp:36
constexpr Point operator-() const noexcept
Definition juce_Point.h:149
constexpr Point withY(ValueType newY) const noexcept
Definition juce_Point.h:82
void setXY(ValueType newX, ValueType newY) noexcept
Definition juce_Point.h:85
void setX(ValueType newX) noexcept
Definition juce_Point.h:73
constexpr ValueType getDistanceSquaredFromOrigin() const noexcept
Definition juce_Point.h:163
void setY(ValueType newY) noexcept
Definition juce_Point.h:76
constexpr ValueType getX() const noexcept
Definition juce_Point.h:67
constexpr Point< int > roundToInt() const noexcept
Definition juce_Point.h:240
constexpr Point< int > toInt() const noexcept
Definition juce_Point.h:231
constexpr Point< double > toDouble() const noexcept
Definition juce_Point.h:237
String toString() const
Definition juce_Point.h:243
void applyTransform(const AffineTransform &transform) noexcept
Definition juce_Point.h:220
constexpr bool isOrigin() const noexcept
Definition juce_Point.h:61
constexpr ValueType getY() const noexcept
Definition juce_Point.h:70
ValueType y
Definition juce_Point.h:247
void addXY(ValueType xToAdd, ValueType yToAdd) noexcept
Definition juce_Point.h:88
Point transformedBy(const AffineTransform &transform) const noexcept
Definition juce_Point.h:223
typename TypeHelpers::SmallestFloatType< ValueType >::type FloatType
Definition juce_Point.h:153
constexpr Point(const Point &)=default
constexpr bool isFinite() const noexcept
Definition juce_Point.h:64
constexpr ValueType getDistanceSquaredFrom(Point other) const noexcept
Definition juce_Point.h:166
ValueType x
Definition juce_Point.h:246
constexpr Point withX(ValueType newX) const noexcept
Definition juce_Point.h:79
Point< FloatType > getPointOnCircumference(float radiusX, float radiusY, float angle) const noexcept
Definition juce_Point.h:205
Point< FloatType > getPointOnCircumference(float radius, float angle) const noexcept
Definition juce_Point.h:194
ValueType getDistanceFromOrigin() const noexcept
Definition juce_Point.h:157
constexpr Point(ValueType initialX, ValueType initialY) noexcept
Definition juce_Point.h:51
Definition juce_String.h:53
int y
Definition inflate.c:1588
unsigned x[BMAX+1]
Definition inflate.c:1586
static PuglViewHint int value
Definition pugl.h:1708
Definition carla_juce.cpp:31
bool juce_isfinite(NumericType) noexcept
Definition juce_MathsFunctions.h:421
Type juce_hypot(Type a, Type b) noexcept
Definition juce_MathsFunctions.h:352
int roundToInt(const FloatType value) noexcept
Definition juce_MathsFunctions.h:465
float type
Definition juce_MathsFunctions.h:657
uch * p
Definition crypt.c:594
#define const
Definition zconf.h:137