private static Vector2 GetIntersectionVector2IfOneIsVertical(LineEquation line1, LineEquation line2) { LineEquation verticalLine = line2.IsVertical ? line2 : line1; LineEquation nonVerticalLine = line2.IsVertical ? line1 : line2; float y = (verticalLine.Start.x - nonVerticalLine.Start.x) * (nonVerticalLine.End.y - nonVerticalLine.Start.y) / ((nonVerticalLine.End.x - nonVerticalLine.Start.x)) + nonVerticalLine.Start.y; float x = line1.IsVertical ? line1.Start.x : line2.Start.x; return(new Vector2(x, y)); }
public Vector2 IntersectWithSegementOfLine(LineEquation otherLine) { Vector2 intersectionVector2 = new Vector2(); bool hasIntersection = IntersectsWithLine(otherLine, out intersectionVector2); if (hasIntersection) { return(intersectionVector2); } else { return(Vector2.zero); } }
public bool IntersectsWithLine(LineEquation otherLine, out Vector2 intersectionVector2) { intersectionVector2 = new Vector2(0, 0); if (IsVertical && otherLine.IsVertical) { return(false); } if (IsVertical || otherLine.IsVertical) { intersectionVector2 = GetIntersectionVector2IfOneIsVertical(otherLine, this); return(true); } float delta = A * otherLine.B - otherLine.A * B; bool hasIntersection = Mathf.Abs(delta - 0) > 0.0001f; if (hasIntersection) { float x = (otherLine.B * C - B * otherLine.C) / delta; float y = (A * otherLine.C - otherLine.A * C) / delta; intersectionVector2 = new Vector2(x, y); } return(hasIntersection); }