public static bool IsPointOnLineSegment(Vector2 p, Vector2 v0, Vector2 v1) { var vp = p - v0; var l = v1 - v0; if (Vector2Ext.Equality(vp, Vector2Ext.Zero)) { return(true); //p == v0 } var vpNormal = vp.Normal(); var lNormal = l.Normal(); if ((float)Math.Abs((double)Vector2Ext.Dot(vpNormal, lNormal)) < OneMinusEps) { return(false); } //Lines are the same, are they within the length if (vp.Length() <= l.Length()) { return(true); } return(false); }
public static LineIntersectionResult RobustLineIntersectAndResult(Vector2 a0, Vector2 a1, Vector2 b0, Vector2 b1, bool fireOnOverlap = true) { //Fail on zero length lines if (Vector2Ext.Equality(a0, a1) || Vector2Ext.Equality(b0, b1)) { return new LineIntersectionResult() { Intersecting = false, CoLinear = false, IntersectPoint = Vector2.Zero } } ; var r = a1 - a0; var s = b1 - b0; var qMp = b0 - a0; float rXs = Vector2Ext.Cross(r, s); float qMpXr = Vector2Ext.Cross(qMp, r); //1. Colinear Check if (IsFuzzyZero(rXs) && IsFuzzyZero(qMpXr)) { //They are colinear if (fireOnOverlap) { float qMpDOTr = Vector2Ext.Dot(qMp, r); float rDOTr = Vector2Ext.Dot(r, r); var pMq = a0 - b0; float pMqDOTs = Vector2Ext.Dot(pMq, s); float sDOTs = Vector2Ext.Dot(s, s); if ((qMpDOTr >= 0.0f && qMpDOTr <= rDOTr) || (pMqDOTs >= 0.0f && pMqDOTs <= sDOTs)) { //You do not get provided a valid intersect point as the lines lie on top of each other return(new LineIntersectionResult() { Intersecting = true, CoLinear = true, IntersectPoint = Vector2.Zero }); } } //Not classifying overlap as intersection return(new LineIntersectionResult() { Intersecting = false, CoLinear = true, IntersectPoint = Vector2.Zero }); } //2. Paralell - Do not intersect if (IsFuzzyZero(rXs) && !IsFuzzyZero(qMpXr)) { return(new LineIntersectionResult() { Intersecting = false, CoLinear = false, IntersectPoint = Vector2.Zero }); } //3.Intersect at Single Point float t = Vector2Ext.Cross(qMp, s) / rXs; float u = Vector2Ext.Cross(qMp, r) / rXs; if (!IsFuzzyZero(rXs) && t >= 0.0f && t <= 1.0f && u >= 0.0f && u <= 1.0f) { return(new LineIntersectionResult() { Intersecting = true, CoLinear = false, IntersectPoint = a0 + (r * t) }); } //4. Do not intersect return(new LineIntersectionResult() { Intersecting = false, CoLinear = false, IntersectPoint = Vector2.Zero }); }
public static float DotWith(this Vector2 v0, Vector2 v1) { return(Vector2Ext.Dot(v0, v1)); }