1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Added methods for line/rectangle intersection.

This commit is contained in:
jules 2013-11-13 10:26:35 +00:00
parent 8f6105de9b
commit 41545be958
2 changed files with 26 additions and 9 deletions

View file

@ -153,6 +153,18 @@ public:
bool operator!= (const Line& other) const noexcept { return start != other.start || end != other.end; }
//==============================================================================
/** Finds the intersection between two lines.
@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> p;
findIntersection (start, end, line.start, line.end, p);
return p;
}
/** Finds the intersection between two lines.
@param line the other line
@ -170,16 +182,11 @@ public:
return findIntersection (start, end, line.start, line.end, intersection);
}
/** Finds the intersection between two lines.
@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
/** Returns true if this line intersects another. */
bool intersects (const Line& other) const noexcept
{
Point<ValueType> p;
findIntersection (start, end, line.start, line.end, p);
return p;
Point<ValueType> ignored;
return findIntersection (start, end, other.start, other.end, ignored);
}
//==============================================================================

View file

@ -563,6 +563,16 @@ public:
&& other.w > ValueType() && other.h > ValueType();
}
/** Returns true if any part of the given line lies inside this rectangle. */
bool intersects (const Line<ValueType>& line) const noexcept
{
return contains (line.getStart()) || contains (line.getEnd())
|| line.intersects (Line<ValueType> (getTopLeft(), getTopRight()))
|| line.intersects (Line<ValueType> (getTopRight(), getBottomRight()))
|| line.intersects (Line<ValueType> (getBottomRight(), getBottomLeft()))
|| line.intersects (Line<ValueType> (getBottomLeft(), getTopLeft()));
}
/** Returns the region that is the overlap between this and another rectangle.
If the two rectangles don't overlap, the rectangle returned will be empty.
*/