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_Line.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//==============================================================================
45template <typename ValueType>
46class Line
47{
48public:
49 //==============================================================================
51 Line() = default;
52
54 Line (const Line&) = default;
55
57 Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
58 : start (startX, startY), end (endX, endY)
59 {
60 }
61
64 : start (startPoint), end (endPoint)
65 {
66 }
67
69 Line& operator= (const Line&) = default;
70
72 ~Line() = default;
73
74 //==============================================================================
76 inline ValueType getStartX() const noexcept { return start.x; }
77
79 inline ValueType getStartY() const noexcept { return start.y; }
80
82 inline ValueType getEndX() const noexcept { return end.x; }
83
85 inline ValueType getEndY() const noexcept { return end.y; }
86
88 inline Point<ValueType> getStart() const noexcept { return start; }
89
91 inline Point<ValueType> getEnd() const noexcept { return end; }
92
94 void setStart (ValueType newStartX, ValueType newStartY) noexcept { start.setXY (newStartX, newStartY); }
95
97 void setEnd (ValueType newEndX, ValueType newEndY) noexcept { end.setXY (newEndX, newEndY); }
98
100 void setStart (const Point<ValueType> newStart) noexcept { start = newStart; }
101
103 void setEnd (const Point<ValueType> newEnd) noexcept { end = newEnd; }
104
106 Line reversed() const noexcept { return { end, start }; }
107
109 void applyTransform (const AffineTransform& transform) noexcept
110 {
111 start.applyTransform (transform);
112 end.applyTransform (transform);
113 }
114
115 //==============================================================================
117 ValueType getLength() const noexcept { return start.getDistanceFrom (end); }
118
120 ValueType getLengthSquared() const noexcept { return start.getDistanceSquaredFrom (end); }
121
123 bool isVertical() const noexcept { return start.x == end.x; }
124
126 bool isHorizontal() const noexcept { return start.y == end.y; }
127
133 typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); }
134
140 static Line fromStartAndAngle (Point<ValueType> startPoint, ValueType length, ValueType angle) noexcept
141 {
142 return { startPoint, startPoint.getPointOnCircumference (length, angle) };
143 }
144
145 //==============================================================================
147 Line<float> toFloat() const noexcept { return { start.toFloat(), end.toFloat() }; }
148
150 Line<double> toDouble() const noexcept { return { start.toDouble(), end.toDouble() }; }
151
152 //==============================================================================
154 bool operator== (Line other) const noexcept { return start == other.start && end == other.end; }
155
157 bool operator!= (Line other) const noexcept { return start != other.start || end != other.end; }
158
159 //==============================================================================
166 {
168 findIntersection (start, end, line.start, line.end, p);
169 return p;
170 }
171
184 bool intersects (Line line, Point<ValueType>& intersection) const noexcept
185 {
186 return findIntersection (start, end, line.start, line.end, intersection);
187 }
188
190 bool intersects (Line other) const noexcept
191 {
192 Point<ValueType> ignored;
193 return findIntersection (start, end, other.start, other.end, ignored);
194 }
195
196 //==============================================================================
205 {
206 const auto length = getLength();
207 return approximatelyEqual (length, (ValueType) 0) ? start : start + (end - start) * (distanceFromStart / length);
208 }
209
224 ValueType perpendicularDistance) const noexcept
225 {
226 auto delta = end - start;
227 auto length = juce_hypot ((double) delta.x,
228 (double) delta.y);
229 if (length <= 0)
230 return start;
231
232 return { start.x + static_cast<ValueType> ((delta.x * distanceFromStart - delta.y * perpendicularDistance) / length),
233 start.y + static_cast<ValueType> ((delta.y * distanceFromStart + delta.x * perpendicularDistance) / length) };
234 }
235
247 {
248 return start + (end - start) * proportionOfLength;
249 }
250
263 Point<ValueType>& pointOnLine) const noexcept
264 {
265 auto delta = end - start;
266 auto length = delta.x * delta.x + delta.y * delta.y;
267
268 if (length > 0)
269 {
270 auto prop = ((targetPoint.x - start.x) * delta.x
271 + (targetPoint.y - start.y) * delta.y) / (double) length;
272
273 if (prop >= 0 && prop <= 1.0)
274 {
275 pointOnLine = start + delta * prop;
276 return targetPoint.getDistanceFrom (pointOnLine);
277 }
278 }
279
280 auto fromStart = targetPoint.getDistanceFrom (start);
281 auto fromEnd = targetPoint.getDistanceFrom (end);
282
283 if (fromStart < fromEnd)
284 {
285 pointOnLine = start;
286 return fromStart;
287 }
288
289 pointOnLine = end;
290 return fromEnd;
291 }
292
302 {
303 auto delta = end - start;
304 auto length = delta.x * delta.x + delta.y * delta.y;
305
306 return length <= 0 ? 0
307 : jlimit (ValueType(), static_cast<ValueType> (1),
308 static_cast<ValueType> ((((point.x - start.x) * delta.x
309 + (point.y - start.y) * delta.y) / length)));
310 }
311
319
326 bool isPointAbove (Point<ValueType> point) const noexcept
327 {
328 return start.x != end.x
329 && point.y < ((end.y - start.y) * (point.x - start.x)) / (end.x - start.x) + start.y;
330 }
331
338 {
340 }
341
342 //==============================================================================
348 Line withShortenedStart (ValueType distanceToShortenBy) const noexcept
349 {
351 }
352
358 Line withLengthenedEnd (ValueType distanceToLengthenBy) const noexcept
359 {
361 }
362
368 Line withShortenedEnd (ValueType distanceToShortenBy) const noexcept
369 {
370 auto length = getLength();
371 return { start, getPointAlongLine (length - jmin (distanceToShortenBy, length)) };
372 }
373
374private:
375 //==============================================================================
376 Point<ValueType> start, end;
377
378 static bool isZeroToOne (ValueType v) noexcept { return v >= 0 && v <= static_cast<ValueType> (1); }
379
380 static bool findIntersection (const Point<ValueType> p1, const Point<ValueType> p2,
381 const Point<ValueType> p3, const Point<ValueType> p4,
382 Point<ValueType>& intersection) noexcept
383 {
384 if (p2 == p3)
385 {
387 return true;
388 }
389
390 auto d1 = p2 - p1;
391 auto d2 = p4 - p3;
392 auto divisor = d1.x * d2.y - d2.x * d1.y;
393
394 const auto zero = ValueType{};
395
396 if (approximatelyEqual (divisor, zero))
397 {
398 if (! (d1.isOrigin() || d2.isOrigin()))
399 {
400 if (approximatelyEqual (d1.y, zero) && ! approximatelyEqual (d2.y, zero))
401 {
402 auto along = (p1.y - p3.y) / d2.y;
403 intersection = p1.withX (p3.x + along * d2.x);
404 return isZeroToOne (along);
405 }
406
407 if (approximatelyEqual (d2.y, zero) && ! approximatelyEqual (d1.y, zero))
408 {
409 auto along = (p3.y - p1.y) / d1.y;
410 intersection = p3.withX (p1.x + along * d1.x);
411 return isZeroToOne (along);
412 }
413
414 if (approximatelyEqual (d1.x, zero) && ! approximatelyEqual (d2.x, zero))
415 {
416 auto along = (p1.x - p3.x) / d2.x;
417 intersection = p1.withY (p3.y + along * d2.y);
418 return isZeroToOne (along);
419 }
420
421 if (approximatelyEqual (d2.x, zero) && ! approximatelyEqual (d1.x, zero))
422 {
423 auto along = (p3.x - p1.x) / d1.x;
424 intersection = p3.withY (p1.y + along * d1.y);
425 return isZeroToOne (along);
426 }
427 }
428
429 intersection = (p2 + p3) / static_cast<ValueType> (2);
430 return false;
431 }
432
433 auto along1 = ((p1.y - p3.y) * d2.x - (p1.x - p3.x) * d2.y) / divisor;
434 intersection = p1 + d1 * along1;
435
436 if (! isZeroToOne (along1))
437 return false;
438
439 auto along2 = ((p1.y - p3.y) * d1.x - (p1.x - p3.x) * d1.y) / divisor;
440 return isZeroToOne (along2);
441 }
442};
443
444} // namespace juce
Represents a 2D affine-transformation matrix.
Represents a line.
Definition juce_Line.h:47
Line(Point< ValueType > startPoint, Point< ValueType > endPoint) noexcept
Creates a line from its start and end points.
Definition juce_Line.h:63
Line reversed() const noexcept
Returns a line that is the same as this one, but with the start and end reversed,.
Definition juce_Line.h:106
ValueType getStartY() const noexcept
Returns the y coordinate of the line's start point.
Definition juce_Line.h:79
static Line fromStartAndAngle(Point< ValueType > startPoint, ValueType length, ValueType angle) noexcept
Creates a line from a start point, length and angle.
Definition juce_Line.h:140
~Line()=default
Destructor.
bool isVertical() const noexcept
Returns true if the line's start and end x coordinates are the same.
Definition juce_Line.h:123
Line withLengthenedEnd(ValueType distanceToLengthenBy) const noexcept
Returns a lengthened copy of this line.
Definition juce_Line.h:358
void setStart(ValueType newStartX, ValueType newStartY) noexcept
Changes this line's start point.
Definition juce_Line.h:94
Point< ValueType >::FloatType getAngle() const noexcept
Returns the line's angle.
Definition juce_Line.h:133
ValueType getEndY() const noexcept
Returns the y coordinate of the line's end point.
Definition juce_Line.h:85
void setStart(const Point< ValueType > newStart) noexcept
Changes this line's start point.
Definition juce_Line.h:100
Point< ValueType > getIntersection(Line line) const noexcept
Finds the intersection between two lines.
Definition juce_Line.h:165
Point< ValueType > getPointAlongLineProportionally(typename Point< ValueType >::FloatType proportionOfLength) const noexcept
Returns the location of the point which is a given distance along this line proportional to the line'...
Definition juce_Line.h:246
Point< ValueType > getPointAlongLine(ValueType distanceFromStart) const noexcept
Returns the location of the point which is a given distance along this line.
Definition juce_Line.h:204
Line withLengthenedStart(ValueType distanceToLengthenBy) const noexcept
Returns a lengthened copy of this line.
Definition juce_Line.h:337
Line(ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
Creates a line based on the coordinates of its start and end points.
Definition juce_Line.h:57
ValueType getLengthSquared() const noexcept
Returns the length of the line.
Definition juce_Line.h:120
bool isPointAbove(Point< ValueType > point) const noexcept
Returns true if the given point lies above this line.
Definition juce_Line.h:326
Line withShortenedStart(ValueType distanceToShortenBy) const noexcept
Returns a shortened copy of this line.
Definition juce_Line.h:348
bool operator!=(Line other) const noexcept
Compares two lines.
Definition juce_Line.h:157
void setEnd(const Point< ValueType > newEnd) noexcept
Changes this line's end point.
Definition juce_Line.h:103
bool intersects(Line other) const noexcept
Returns true if this line intersects another.
Definition juce_Line.h:190
Line & operator=(const Line &)=default
Copies a line from another one.
ValueType getLength() const noexcept
Returns the length of the line.
Definition juce_Line.h:117
ValueType findNearestProportionalPositionTo(Point< ValueType > point) const noexcept
Finds the point on this line which is nearest to a given point, and returns its position as a proport...
Definition juce_Line.h:301
Line()=default
Creates a line, using (0, 0) as its start and end points.
Line< float > toFloat() const noexcept
Casts this line to float coordinates.
Definition juce_Line.h:147
ValueType getStartX() const noexcept
Returns the x coordinate of the line's start point.
Definition juce_Line.h:76
Point< ValueType > findNearestPointTo(Point< ValueType > point) const noexcept
Finds the point on this line which is nearest to a given point.
Definition juce_Line.h:315
ValueType getDistanceFromPoint(Point< ValueType > targetPoint, Point< ValueType > &pointOnLine) const noexcept
Returns the smallest distance between this line segment and a given point.
Definition juce_Line.h:262
bool operator==(Line other) const noexcept
Compares two lines.
Definition juce_Line.h:154
void setEnd(ValueType newEndX, ValueType newEndY) noexcept
Changes this line's end point.
Definition juce_Line.h:97
Line(const Line &)=default
Creates a copy of another line.
bool isHorizontal() const noexcept
Returns true if the line's start and end y coordinates are the same.
Definition juce_Line.h:126
Point< ValueType > getEnd() const noexcept
Returns the line's end point.
Definition juce_Line.h:91
ValueType getEndX() const noexcept
Returns the x coordinate of the line's end point.
Definition juce_Line.h:82
Line< double > toDouble() const noexcept
Casts this line to double coordinates.
Definition juce_Line.h:150
void applyTransform(const AffineTransform &transform) noexcept
Applies an affine transform to the line's start and end points.
Definition juce_Line.h:109
Line withShortenedEnd(ValueType distanceToShortenBy) const noexcept
Returns a shortened copy of this line.
Definition juce_Line.h:368
Point< ValueType > getPointAlongLine(ValueType distanceFromStart, ValueType perpendicularDistance) const noexcept
Returns a point which is a certain distance along and to the side of this line.
Definition juce_Line.h:223
bool intersects(Line line, Point< ValueType > &intersection) const noexcept
Finds the intersection between two lines.
Definition juce_Line.h:184
Point< ValueType > getStart() const noexcept
Returns the line's start point.
Definition juce_Line.h:88
A pair of (x, y) coordinates.
Definition juce_Point.h:42
JUCE Namespace.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
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 ...
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