public static List <Vector2> GetIntersectionPoint(this Rect rect, Line2 line) { List <Vector2> p = new List <Vector2>(); // Top line Line2 lineTop = new Line2(new Vector2(rect.xMin, rect.yMax), rect.max); Vector2 topPoint = line.GetIntersectionPoint(lineTop); if (lineTop.ContainPointInSegment(topPoint)) { p.Add(topPoint); } // Bottom line Line2 lineBottom = new Line2(rect.min, new Vector2(rect.xMax, rect.yMin)); Vector2 bottomPoint = line.GetIntersectionPoint(lineBottom); if (lineBottom.ContainPointInSegment(bottomPoint)) { p.Add(bottomPoint); } // Left side Line2 lineLeft = new Line2(rect.min, new Vector2(rect.xMin, rect.yMax)); Vector2 leftPoint = line.GetIntersectionPoint(lineLeft); if (lineLeft.ContainPointInSegment(leftPoint)) { p.Add(leftPoint); } // Right side Line2 lineRight = new Line2(new Vector2(rect.xMax, rect.yMin), rect.max); Vector2 rightPoint = line.GetIntersectionPoint(lineRight); if (lineRight.ContainPointInSegment(rightPoint)) { p.Add(rightPoint); } return(p); }