FairyGUI.Utils.ToolSet.IsPointInTriangle C# (CSharp) Method

IsPointInTriangle() public static method

public static IsPointInTriangle ( Vector2 &p, Vector2 &a, Vector2 &b, Vector2 &c ) : bool
p Vector2
a Vector2
b Vector2
c Vector2
return bool
        public static bool IsPointInTriangle(ref Vector2 p, ref Vector2 a, ref Vector2 b, ref Vector2 c)
        {
            // This algorithm is described well in this article:
            // http://www.blackpawn.com/texts/pointinpoly/default.html

            float v0x = c.x - a.x;
            float v0y = c.y - a.y;
            float v1x = b.x - a.x;
            float v1y = b.y - a.y;
            float v2x = p.x - a.x;
            float v2y = p.y - a.y;

            float dot00 = v0x * v0x + v0y * v0y;
            float dot01 = v0x * v1x + v0y * v1y;
            float dot02 = v0x * v2x + v0y * v2y;
            float dot11 = v1x * v1x + v1y * v1y;
            float dot12 = v1x * v2x + v1y * v2y;

            float invDen = 1.0f / (dot00 * dot11 - dot01 * dot01);
            float u = (dot11 * dot02 - dot01 * dot12) * invDen;
            float v = (dot00 * dot12 - dot01 * dot02) * invDen;

            return (u >= 0) && (v >= 0) && (u + v < 1);
        }