private bool LineIntesect(Line line, Line rayCastingLine) { if (line.IsVertical && rayCastingLine.IsVertical) { return(false); } if (line.IsVertical || rayCastingLine.IsVertical) { int x; Line vLine; Line otherLine; if (line.IsVertical) { x = line.A.X; vLine = line; otherLine = rayCastingLine; } else if (rayCastingLine.IsVertical) { x = rayCastingLine.A.X; vLine = rayCastingLine; otherLine = line; } else { throw new NotImplementedException(); } float intersectY = otherLine.GetLinearEquation(x); return(otherLine.GetLeft().X < x && otherLine.GetRight().X > x && intersectY <= vLine.GetTop().Y&& intersectY >= vLine.GetBottom().Y); } LinearEquation lineEq = line.GetLinearEquation(); LinearEquation rayEq = rayCastingLine.GetLinearEquation(); if (Math.Abs(rayEq.K - lineEq.K) < 0.001) { return(false); } float crossX = (lineEq.M - rayEq.M) / (rayEq.K - lineEq.K); return(crossX >= rayCastingLine.GetLeft().X&& crossX >= line.GetLeft().X&& crossX <= rayCastingLine.GetRight().X&& crossX < line.GetRight().X); }
public float GetLinearEquation(float x) { LinearEquation linearEquation = GetLinearEquation(); return(linearEquation.K * x + linearEquation.M); }