public static bool SegmentTest(JellyVector2 a, JellyVector2 b, JellyVector2 c, JellyVector2 d) { float triaa = SignedTriangleArea(a, b, d); float triab = SignedTriangleArea(a, b, c); float triac = 0; float triad = 0; if (triaa * triab < 0.0) { triac = SignedTriangleArea(c, d, a); triad = triac + triab - triaa; if (triac * triad < 0.0) { return(true); } } if (triaa + triab + triac + triad == 0.0) { JellyVector2 ab = b - a; float abdab = JellyVector2.Dot(ab, ab); JellyVector2 ac = c - a; float abdac = JellyVector2.Dot(ab, ac); JellyVector2 ad = d - a; float abdad = JellyVector2.Dot(ab, ad); float min = System.Math.Min(abdac, abdad); float max = System.Math.Max(abdac, abdad); return(min < abdab && max > 0); } return(false); }
public static void Barycentric(JellyVector2 a, JellyVector2 b, JellyVector2 c, JellyVector2 p, out float u, out float v, out float w) { JellyVector2 v0 = b - a, v1 = c - a, v2 = p - a; float d00 = JellyVector2.Dot(v0, v0); float d01 = JellyVector2.Dot(v0, v1); float d11 = JellyVector2.Dot(v1, v1); float d20 = JellyVector2.Dot(v2, v0); float d21 = JellyVector2.Dot(v2, v1); float denom = d00 * d11 - d01 * d01; v = (d11 * d20 - d01 * d21) / denom; w = (d00 * d21 - d01 * d20) / denom; u = 1 - v - w; }
public static JellyVector2 ClosestPointOnSegment(JellyVector2 a, JellyVector2 b, JellyVector2 c) { JellyVector2 ab = b - a; float t = JellyVector2.Dot(c - a, ab) / JellyVector2.Dot(ab, ab); if (t < 0) { return(a); } if (t > 1) { return(b); } return(a + ab * t); }
public static bool SegmentTest(JellyVector2 a, JellyVector2 b, JellyVector2 c, JellyVector2 d, out JellyVector2 i) { float triaa = SignedTriangleArea(a, b, d); float triab = SignedTriangleArea(a, b, c); float triac = 0; float triad = 0; if (triaa * triab < 0.0) { triac = SignedTriangleArea(c, d, a); triad = triac + triab - triaa; if (triac * triad < 0.0) { float t = triac / (triac - triad); i = a + (b - a) * t; return(true); } } if (triaa + triab + triac + triad == 0.0) { JellyVector2 ab = b - a; float abdab = JellyVector2.Dot(ab, ab); JellyVector2 ac = c - a; float abdac = JellyVector2.Dot(ab, ac); JellyVector2 ad = d - a; float abdad = JellyVector2.Dot(ab, ad); float min = System.Math.Min(abdac, abdad); float max = System.Math.Max(abdac, abdad); if (min < abdab && max > 0) { i = (a + b + c + d) * 0.25f; return(true); } } i = JellyVector2.Zero;//it should be null,change in XNA return(false); }