Browse Source

Tidied up a few things in the Line class, and added a Line::fromStartAndAngle method

tags/2021-05-28
jules 9 years ago
parent
commit
6b8c7208b3
1 changed files with 32 additions and 21 deletions
  1. +32
    -21
      modules/juce_graphics/geometry/juce_Line.h

+ 32
- 21
modules/juce_graphics/geometry/juce_Line.h View File

@@ -50,23 +50,20 @@ public:
/** Creates a copy of another line. */
Line (const Line& other) noexcept
: start (other.start),
end (other.end)
: start (other.start), end (other.end)
{
}
/** Creates a line based on the coordinates of its start and end points. */
Line (ValueType startX, ValueType startY, ValueType endX, ValueType endY) noexcept
: start (startX, startY),
end (endX, endY)
: start (startX, startY), end (endX, endY)
{
}
/** Creates a line from its start and end points. */
Line (const Point<ValueType> startPoint,
const Point<ValueType> endPoint) noexcept
: start (startPoint),
end (endPoint)
Line (Point<ValueType> startPoint,
Point<ValueType> endPoint) noexcept
: start (startPoint), end (endPoint)
{
}
@@ -113,7 +110,7 @@ public:
void setEnd (const Point<ValueType> newEnd) noexcept { end = newEnd; }
/** Returns a line that is the same as this one, but with the start and end reversed, */
const Line reversed() const noexcept { return Line (end, start); }
Line reversed() const noexcept { return Line (end, start); }
/** Applies an affine transform to the line's start and end points. */
void applyTransform (const AffineTransform& transform) noexcept
@@ -142,6 +139,17 @@ public:
*/
typename Point<ValueType>::FloatType getAngle() const noexcept { return start.getAngleToPoint (end); }
/** Creates a line from a start point, length and angle.
This angle is the number of radians clockwise from the 12 o'clock direction,
where the line's start point is considered to be at the centre.
*/
static Line fromStartAndAngle (Point<ValueType> startPoint, ValueType length, ValueType angle) noexcept
{
return Line (startPoint, startPoint.getPointOnCircumference (length, angle));
}
//==============================================================================
/** Casts this line to float coordinates. */
Line<float> toFloat() const noexcept { return Line<float> (start.toFloat(), end.toFloat()); }
@@ -150,10 +158,10 @@ public:
//==============================================================================
/** Compares two lines. */
bool operator== (const Line& other) const noexcept { return start == other.start && end == other.end; }
bool operator== (Line other) const noexcept { return start == other.start && end == other.end; }
/** Compares two lines. */
bool operator!= (const Line& other) const noexcept { return start != other.start || end != other.end; }
bool operator!= (Line other) const noexcept { return start != other.start || end != other.end; }
//==============================================================================
/** Finds the intersection between two lines.
@@ -161,7 +169,7 @@ public:
@param line the line to intersect with
@returns the point at which the lines intersect, even if this lies beyond the end of the lines
*/
Point<ValueType> getIntersection (const Line& line) const noexcept
Point<ValueType> getIntersection (Line line) const noexcept
{
Point<ValueType> p;
findIntersection (start, end, line.start, line.end, p);
@@ -180,13 +188,13 @@ public:
don't intersect, the intersection coordinates returned will still
be valid
*/
bool intersects (const Line& line, Point<ValueType>& intersection) const noexcept
bool intersects (Line line, Point<ValueType>& intersection) const noexcept
{
return findIntersection (start, end, line.start, line.end, intersection);
}
/** Returns true if this line intersects another. */
bool intersects (const Line& other) const noexcept
bool intersects (Line other) const noexcept
{
Point<ValueType> ignored;
return findIntersection (start, end, other.start, other.end, ignored);
@@ -257,7 +265,7 @@ public:
@returns the point's distance from the line
@see getPositionAlongLineOfNearestPoint
*/
ValueType getDistanceFromPoint (const Point<ValueType> targetPoint,
ValueType getDistanceFromPoint (Point<ValueType> targetPoint,
Point<ValueType>& pointOnLine) const noexcept
{
const Point<ValueType> delta (end - start);
@@ -298,7 +306,7 @@ public:
turn this number into a position, use getPointAlongLineProportionally().
@see getDistanceFromPoint, getPointAlongLineProportionally
*/
ValueType findNearestProportionalPositionTo (const Point<ValueType> point) const noexcept
ValueType findNearestProportionalPositionTo (Point<ValueType> point) const noexcept
{
const Point<ValueType> delta (end - start);
const double length = delta.x * delta.x + delta.y * delta.y;
@@ -312,7 +320,7 @@ public:
/** Finds the point on this line which is nearest to a given point.
@see getDistanceFromPoint, findNearestProportionalPositionTo
*/
Point<ValueType> findNearestPointTo (const Point<ValueType> point) const noexcept
Point<ValueType> findNearestPointTo (Point<ValueType> point) const noexcept
{
return getPointAlongLineProportionally (findNearestProportionalPositionTo (point));
}
@@ -323,7 +331,7 @@ public:
coordinate of this line at the given x (assuming the line extends infinitely
in both directions).
*/
bool isPointAbove (const Point<ValueType> point) const noexcept
bool isPointAbove (Point<ValueType> point) const noexcept
{
return start.x != end.x
&& point.y < ((end.y - start.y)
@@ -380,19 +388,22 @@ private:
intersection = p1.withX (p3.x + along * d2.x);
return along >= 0 && along <= static_cast<ValueType> (1);
}
else if (d2.y == 0 && d1.y != 0)
if (d2.y == 0 && d1.y != 0)
{
const ValueType along = (p3.y - p1.y) / d1.y;
intersection = p3.withX (p1.x + along * d1.x);
return along >= 0 && along <= static_cast<ValueType> (1);
}
else if (d1.x == 0 && d2.x != 0)
if (d1.x == 0 && d2.x != 0)
{
const ValueType along = (p1.x - p3.x) / d2.x;
intersection = p1.withY (p3.y + along * d2.y);
return along >= 0 && along <= static_cast<ValueType> (1);
}
else if (d2.x == 0 && d1.x != 0)
if (d2.x == 0 && d1.x != 0)
{
const ValueType along = (p3.x - p1.x) / d1.x;
intersection = p3.withY (p1.y + along * d1.y);


Loading…
Cancel
Save