public static bool IntersectionExists(FixedLine2 a, FixedLine2 b) { int sign1 = (a.start.y - (b.slope.Mul(a.start.x)) - b.yIntercept).Sign(); int sign2 = (a.end.y - (b.slope.Mul(a.end.x)) - b.yIntercept).Sign(); return(sign1 != sign2); }
public static bool LineIntersection(FixedLine2 left, FixedLine2 right, out FixedVector2 intersection) { intersection = FixedVector2.NAN; //check if we have parralel lines (even if we have two overlapping lines) if ((left.isHorizontal && right.isHorizontal) || ((left.isVertical && right.isVertical))) { return(false); } if (left.isHorizontal && right.isVertical) { horizontal = left; vertical = right; } else if (right.isHorizontal && left.isVertical) { horizontal = right; vertical = left; } intersection = new FixedVector2(vertical.start.x, horizontal.start.y); if ((FixedMath.Min(horizontal.start.x, horizontal.end.x) <= intersection.x) && intersection.x <= FixedMath.Max(horizontal.start.x, horizontal.end.x) && (FixedMath.Min(vertical.start.y, vertical.end.y) <= intersection.y) && intersection.y <= FixedMath.Max(vertical.start.y, vertical.end.y)) { return(true); } //check for sloped lines while accounting for vertical lines return(false); }
public static bool AAOrthogonalLineIntersection(FixedLine2 horizontal, FixedLine2 vertical, out FixedVector2 intersection) { intersection = new FixedVector2(vertical.start.x, horizontal.start.y); if ((FixedMath.Min(horizontal.start.x, horizontal.end.x) < intersection.x) && intersection.x < FixedMath.Max(horizontal.start.x, horizontal.end.x) && (FixedMath.Min(vertical.start.y, vertical.end.y) < intersection.y) && intersection.y < FixedMath.Max(vertical.start.y, vertical.end.y)) { return(true); } //check for sloped lines while accounting for vertical lines return(false); }
public static bool LineLineIntersection(FixedLine2 line1, FixedLine2 line2, out FixedVector2 intersection) { intersection = new FixedVector2(long.MinValue, long.MinValue); //standard form of line 1 long a1 = line1.end.y - line1.start.y; long b1 = line1.start.x - line1.end.x; long c1 = a1.Mul(line1.start.x) - b1.Mul(line1.start.y); //standard form of line 2 long a2 = line2.end.y - line2.start.y; long b2 = line2.start.x - line2.end.x; long c2 = a2.Mul(line2.start.x) - b2.Mul(line2.start.y); // float delta = A1*B2 - A2*B1; // if(delta == 0) // throw new ArgumentException("Lines are parallel"); // // float x = (B2*C1 - B1*C2)/delta; // float y = (A1*C2 - A2*C1)/delta; long determinant = a1.Mul(b2) - a2.Mul(b1); //line are parallel if (determinant.Abs() < FixedMath.Hundredth) { return(false); } long x = (b2.Mul(c1) - b1.Mul(c2)).Div(determinant); long y = (a1.Mul(c2) - a2.Mul(c1)).Div(determinant); intersection = new FixedVector2(x, y); // UnityEngine.Debug.DrawLine (new FixedVector2 (0, 0).ToVector3 (), intersection.ToVector3 ()); if ((FixedMath.Min(line1.start.x, line1.end.x) < x && x < FixedMath.Max(line1.start.x, line1.end.x)) && (FixedMath.Min(line1.start.y, line1.end.y) < y && y < FixedMath.Max(line1.start.y, line1.end.y))) { return(true); } return(false); }
public static bool AreParallel(FixedLine2 a, FixedLine2 b) { return(a.slope == b.slope); }