public static bool DoesLineIntersectRectangle(RectangleF pobjRectangle, Line pobjLine, ref Line pobjIntersectingPortionOfLine)
{
RectangleF objLineAsRectangle = default(RectangleF);
objLineAsRectangle = pobjLine.ToRectangle();
if (!pobjRectangle.IntersectsWith(objLineAsRectangle))
{
return false;
}
else
{
bool blnIsPointAIn = false;
bool blnIsPointBIn = false;
blnIsPointAIn = pobjRectangle.Contains(pobjLine.PointA);
blnIsPointBIn = pobjRectangle.Contains(pobjLine.PointB);
if (blnIsPointAIn && blnIsPointBIn)
{
pobjIntersectingPortionOfLine = pobjLine;
}
else
{
PointF[] colIntersections = new PointF[2];
// Either one or two intersections.
List<Line> colLines = null;
colLines = GeometryHelper.RectangleTo4Lines(pobjRectangle);
foreach (Line objLine in colLines)
{
if (colIntersections[0].IsEmpty)
{
DoLinesIntersect(objLine, pobjLine, ref colIntersections[0]);
}
else if (colIntersections[1].IsEmpty)
{
DoLinesIntersect(objLine, pobjLine, ref colIntersections[1]);
}
}
if (!blnIsPointAIn && !blnIsPointBIn)
{
pobjIntersectingPortionOfLine = new Line(colIntersections[0], colIntersections[1]);
}
else
{
if (blnIsPointAIn)
{
pobjIntersectingPortionOfLine = new Line(colIntersections[0], pobjLine.PointA);
}
else
{
pobjIntersectingPortionOfLine = new Line(colIntersections[0], pobjLine.PointB);
}
}
}
return true;
}
}