public static int findClosestPointOnTriangle(float v0X, float v0Y, float v1X, float v1Y, float v2X, float v2Y, float pX, float pY, Vector2f result) {
        float abX = v1X - v0X, abY = v1Y - v0Y;
        float acX = v2X - v0X, acY = v2Y - v0Y;
        float apX = pX - v0X, apY = pY - v0Y;
        float d1 = abX * apX + abY * apY;
        float d2 = acX * apX + acY * apY;
        if (d1 <= 0.0f && d2 <= 0.0f) {
            result.x = v0X;
            result.y = v0Y;
            return POINT_ON_TRIANGLE_VERTEX_0;
        }
        float bpX = pX - v1X, bpY = pY - v1Y;
        float d3 = abX * bpX + abY * bpY;
        float d4 = acX * bpX + acY * bpY;
        if (d3 >= 0.0f && d4 <= d3) {
            result.x = v1X;
            result.y = v1Y;
            return POINT_ON_TRIANGLE_VERTEX_1;
        }
        float vc = d1 * d4 - d3 * d2;
        if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f) {
            float v = d1 / (d1 - d3);
            result.x = v0X + v * abX;
            result.y = v0Y + v * abY;
            return POINT_ON_TRIANGLE_EDGE_01;
        }
        float cpX = pX - v2X, cpY = pY - v2Y;
        float d5 = abX * cpX + abY * cpY;
        float d6 = acX * cpX + acY * cpY;
        if (d6 >= 0.0f && d5 <= d6) {
            result.x = v2X;
            result.y = v2Y;
            return POINT_ON_TRIANGLE_VERTEX_2;
        }
        float vb = d5 * d2 - d1 * d6;
        if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f) {
            float w = d2 / (d2 - d6);
            result.x = v0X + w * acX;
            result.y = v0Y + w * acY;
            return POINT_ON_TRIANGLE_EDGE_20;
        }
        float va = d3 * d6 - d5 * d4;
        if (va <= 0.0f && d4 - d3 >= 0.0f && d5 - d6 >= 0.0f) {
            float w = (d4 - d3) / (d4 - d3 + d5 - d6);
            result.x = v1X + w * (v2X - v1X);
            result.y = v1Y + w * (v2Y - v1Y);
            return POINT_ON_TRIANGLE_EDGE_12;
        }
        float denom = 1.0f / (va + vb + vc);
        float v = vb * denom;
        float w = vc * denom;
        result.x = v0X + abX * v + acX * w;
        result.y = v0Y + abY * v + acY * w;
        return POINT_ON_TRIANGLE_FACE;
    }
public static int findClosestPointOnTriangle ( float v0X , float v0Y , float v1X , float v1Y , float v2X , float v2Y , float pX , float pY , Vector2f result ) { float abX = v1X - v0X , abY = v1Y - v0Y ; float acX = v2X - v0X , acY = v2Y - v0Y ; float apX = pX - v0X , apY = pY - v0Y ; float d1 = abX * apX + abY * apY ; float d2 = acX * apX + acY * apY ; if ( d1 <= 0.0f && d2 <= 0.0f ) { result . x = v0X ; result . y = v0Y ; return POINT_ON_TRIANGLE_VERTEX_0 ; } float bpX = pX - v1X , bpY = pY - v1Y ; float d3 = abX * bpX + abY * bpY ; float d4 = acX * bpX + acY * bpY ; if ( d3 >= 0.0f && d4 <= d3 ) { result . x = v1X ; result . y = v1Y ; return POINT_ON_TRIANGLE_VERTEX_1 ; } float vc = d1 * d4 - d3 * d2 ; if ( vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f ) { float v = d1 / ( d1 - d3 ) ; result . x = v0X + v * abX ; result . y = v0Y + v * abY ; return POINT_ON_TRIANGLE_EDGE_01 ; } float cpX = pX - v2X , cpY = pY - v2Y ; float d5 = abX * cpX + abY * cpY ; float d6 = acX * cpX + acY * cpY ; if ( d6 >= 0.0f && d5 <= d6 ) { result . x = v2X ; result . y = v2Y ; return POINT_ON_TRIANGLE_VERTEX_2 ; } float vb = d5 * d2 - d1 * d6 ; if ( vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f ) { float w = d2 / ( d2 - d6 ) ; result . x = v0X + w * acX ; result . y = v0Y + w * acY ; return POINT_ON_TRIANGLE_EDGE_20 ; } float va = d3 * d6 - d5 * d4 ; if ( va <= 0.0f && d4 - d3 >= 0.0f && d5 - d6 >= 0.0f ) { float w = ( d4 - d3 ) / ( d4 - d3 + d5 - d6 ) ; result . x = v1X + w * ( v2X - v1X ) ; result . y = v1Y + w * ( v2Y - v1Y ) ; return POINT_ON_TRIANGLE_EDGE_12 ; } float denom = 1.0f / ( va + vb + vc ) ; float v = vb * denom ; float w = vc * denom ; result . x = v0X + abX * v + acX * w ; result . y = v0Y + abY * v + acY * w ; return POINT_ON_TRIANGLE_FACE ; }
Determine the closest point on the triangle with the given vertices <code>(v0X, v0Y)</code>, <code>(v1X, v1Y)</code>, <code>(v2X, v2Y)</code>
between that triangle and the given point <code>(pX, pY)</code> and store that point into the given <code>result</code>.
<p>
Additionally, this method returns whether the closest point is a vertex ({@link #POINT_ON_TRIANGLE_VERTEX_0}, {@link #POINT_ON_TRIANGLE_VERTEX_1}, {@link #POINT_ON_TRIANGLE_VERTEX_2})
of the triangle, lies on an edge ({@link #POINT_ON_TRIANGLE_EDGE_01}, {@link #POINT_ON_TRIANGLE_EDGE_12}, {@link #POINT_ON_TRIANGLE_EDGE_20})
or on the {@link #POINT_ON_TRIANGLE_FACE face} of the triangle.
<p>
Reference: Book "Real-Time Collision Detection" chapter 5.1.5 "Closest Point on Triangle to Point"

@param v0X
the x coordinate of the first vertex of the triangle
@param v0Y
the y coordinate of the first vertex of the triangle
@param v1X
the x coordinate of the second vertex of the triangle
@param v1Y
the y coordinate of the second vertex of the triangle
@param v2X
the x coordinate of the third vertex of the triangle
@param v2Y
the y coordinate of the third vertex of the triangle
@param pX
the x coordinate of the point
@param pY
the y coordinate of the point
@param result
will hold the closest point
@return one of {@link #POINT_ON_TRIANGLE_VERTEX_0}, {@link #POINT_ON_TRIANGLE_VERTEX_1}, {@link #POINT_ON_TRIANGLE_VERTEX_2},
{@link #POINT_ON_TRIANGLE_EDGE_01}, {@link #POINT_ON_TRIANGLE_EDGE_12}, {@link #POINT_ON_TRIANGLE_EDGE_20} or
{@link #POINT_ON_TRIANGLE_FACE}