25 #ifndef JUCE_LINE_H_INCLUDED 26 #define JUCE_LINE_H_INCLUDED 43 template <
typename ValueType>
53 : start (other.start),
59 Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY)
noexcept 60 : start (startX, startY),
104 void setStart (ValueType newStartX, ValueType newStartY)
noexcept { start.setXY (newStartX, newStartY); }
107 void setEnd (ValueType newEndX, ValueType newEndY)
noexcept { end.setXY (newEndX, newEndY); }
121 start.applyTransform (transform);
122 end.applyTransform (transform);
164 findIntersection (start, end, line.start, line.end, p);
182 return findIntersection (start, end, line.start, line.end, intersection);
189 return findIntersection (start, end, other.start, other.end, ignored);
202 return start + (end - start) * (distanceFromStart /
getLength());
219 ValueType perpendicularDistance)
const noexcept 222 const double length =
juce_hypot ((
double) delta.
x,
227 return Point<ValueType> (start.x +
static_cast<ValueType
> ((delta.
x * distanceFromStart - delta.
y * perpendicularDistance) / length),
228 start.y +
static_cast<ValueType
> ((delta.
y * distanceFromStart + delta.
x * perpendicularDistance) / length));
243 return start + (end - start) * proportionOfLength;
261 const double length = delta.
x * delta.
x + delta.
y * delta.
y;
265 const double prop = ((targetPoint.x - start.x) * delta.
x 266 + (targetPoint.y - start.y) * delta.
y) / length;
268 if (prop >= 0 && prop <= 1.0)
270 pointOnLine = start + delta *
static_cast<ValueType
> (prop);
275 const float fromStart = targetPoint.getDistanceFrom (start);
276 const float fromEnd = targetPoint.getDistanceFrom (end);
278 if (fromStart < fromEnd)
301 const double length = delta.
x * delta.
x + delta.
y * delta.
y;
303 return length <= 0 ? 0
304 :
jlimit (ValueType(), static_cast<ValueType> (1),
305 static_cast<ValueType> ((((point.x - start.x) * delta.
x 306 + (point.y - start.y) * delta.
y) / length)));
325 return start.x != end.x
326 && point.y < ((end.y - start.y)
327 * (point.x - start.x)) / (end.x - start.x) + start.y;
368 const ValueType divisor = d1.
x * d2.
y - d2.
x * d1.
y;
374 if (d1.
y == 0 && d2.
y != 0)
376 const ValueType along = (p1.
y - p3.
y) / d2.
y;
377 intersection = p1.
withX (p3.
x + along * d2.
x);
378 return along >= 0 && along <= static_cast<ValueType> (1);
380 else if (d2.
y == 0 && d1.
y != 0)
382 const ValueType along = (p3.
y - p1.
y) / d1.
y;
383 intersection = p3.
withX (p1.
x + along * d1.
x);
384 return along >= 0 && along <= static_cast<ValueType> (1);
386 else if (d1.
x == 0 && d2.
x != 0)
388 const ValueType along = (p1.
x - p3.
x) / d2.
x;
389 intersection = p1.
withY (p3.
y + along * d2.
y);
390 return along >= 0 && along <= static_cast<ValueType> (1);
392 else if (d2.
x == 0 && d1.
x != 0)
394 const ValueType along = (p3.
x - p1.
x) / d1.
x;
395 intersection = p3.
withY (p1.
y + along * d1.
y);
396 return along >= 0 && along <= static_cast<ValueType> (1);
400 intersection = (p2 + p3) / static_cast<ValueType> (2);
404 const ValueType along1 = ((p1.
y - p3.
y) * d2.
x - (p1.
x - p3.
x) * d2.
y) / divisor;
405 intersection = p1 + d1 * along1;
407 if (along1 < 0 || along1 > static_cast<ValueType> (1))
410 const ValueType along2 = ((p1.
y - p3.
y) * d1.
x - (p1.
x - p3.
x) * d1.
y) / divisor;
411 return along2 >= 0 && along2 <= static_cast<ValueType> (1);
416 #endif // JUCE_LINE_H_INCLUDED ValueType getDistanceFromPoint(const Point< ValueType > targetPoint, Point< ValueType > &pointOnLine) const noexcept
Definition: juce_Line.h:257
ValueType getStartY() const noexcept
Definition: juce_Line.h:89
bool intersects(const Line &line, Point< ValueType > &intersection) const noexcept
Definition: juce_Line.h:180
bool operator==(const Line &other) const noexcept
Definition: juce_Line.h:150
Definition: juce_Line.h:44
#define noexcept
Definition: juce_CompilerSupport.h:141
bool intersects(const Line &other) const noexcept
Definition: juce_Line.h:186
Type jmin(const Type a, const Type b)
Definition: juce_core.h:113
bool operator!=(const Line &other) const noexcept
Definition: juce_Line.h:153
ValueType getDistanceFrom(Point other) const noexcept
Definition: juce_Point.h:148
Point< ValueType > getEnd() const noexcept
Definition: juce_Line.h:101
ValueType getEndY() const noexcept
Definition: juce_Line.h:95
Point< ValueType > getStart() const noexcept
Definition: juce_Line.h:98
const Line reversed() const noexcept
Definition: juce_Line.h:116
Definition: juce_Point.h:39
Point withX(ValueType newX) const noexcept
Definition: juce_Point.h:77
Line(const Point< ValueType > startPoint, const Point< ValueType > endPoint) noexcept
Definition: juce_Line.h:66
Point< ValueType > getPointAlongLine(ValueType distanceFromStart, ValueType perpendicularDistance) const noexcept
Definition: juce_Line.h:218
Line< double > toDouble() const noexcept
Definition: juce_Line.h:146
void setStart(ValueType newStartX, ValueType newStartY) noexcept
Definition: juce_Line.h:104
bool isPointAbove(const Point< ValueType > point) const noexcept
Definition: juce_Line.h:323
Line(ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
Definition: juce_Line.h:59
Line withShortenedEnd(ValueType distanceToShortenBy) const noexcept
Definition: juce_Line.h:346
Point< ValueType > getIntersection(const Line &line) const noexcept
Definition: juce_Line.h:161
Line & operator=(const Line &other) noexcept
Definition: juce_Line.h:74
bool isOrigin() const noexcept
Definition: juce_Point.h:59
void setStart(const Point< ValueType > newStart) noexcept
Definition: juce_Line.h:110
Line withShortenedStart(ValueType distanceToShortenBy) const noexcept
Definition: juce_Line.h:336
void setEnd(const Point< ValueType > newEnd) noexcept
Definition: juce_Line.h:113
bool isHorizontal() const noexcept
Definition: juce_Line.h:133
void applyTransform(const AffineTransform &transform) noexcept
Definition: juce_Line.h:119
Point< ValueType > getPointAlongLine(ValueType distanceFromStart) const noexcept
Definition: juce_Line.h:200
Type jlimit(const Type lowerLimit, const Type upperLimit, const Type valueToConstrain) noexcept
Definition: juce_MathsFunctions.h:220
ValueType getStartX() const noexcept
Definition: juce_Line.h:86
Type juce_hypot(Type a, Type b) noexcept
Definition: juce_core.h:313
ValueType getLength() const noexcept
Definition: juce_Line.h:127
Line(const Line &other) noexcept
Definition: juce_Line.h:52
Point< ValueType > findNearestPointTo(const Point< ValueType > point) const noexcept
Definition: juce_Line.h:312
ValueType findNearestProportionalPositionTo(const Point< ValueType > point) const noexcept
Definition: juce_Line.h:298
void setEnd(ValueType newEndX, ValueType newEndY) noexcept
Definition: juce_Line.h:107
Point< ValueType > getPointAlongLineProportionally(ValueType proportionOfLength) const noexcept
Definition: juce_Line.h:241
~Line() noexcept
Definition: juce_Line.h:82
bool isVertical() const noexcept
Definition: juce_Line.h:130
Point< ValueType >::FloatType getAngle() const noexcept
Definition: juce_Line.h:140
Line() noexcept
Definition: juce_Line.h:49
TypeHelpers::SmallestFloatType< ValueType >::type FloatType
Definition: juce_Point.h:141
Point withY(ValueType newY) const noexcept
Definition: juce_Point.h:80
Line< float > toFloat() const noexcept
Definition: juce_Line.h:143
ValueType x
Definition: juce_Point.h:226
ValueType y
Definition: juce_Point.h:227
ValueType getEndX() const noexcept
Definition: juce_Line.h:92