0

Triangle:

A: (-538.10,-41.64)

B: (-544.67,-57.29)

C: (-553.75,-35.07)

Points to Check:

P1: (-548.20,-45.77)

P2: (-553.00, -30.60)

I already know P1 is inside and P2 is outside, but I'm trying to figure out a formula to solve this by providing the coordinates of △ABC and a point and returning true/false.

By digging around a lil I found this(not lua):

function ptInTriangle(p, p0, p1, p2) {
    var A = 1/2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
    var sign = A < 0 ? -1 : 1;
    var s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
    var t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;

    return s > 0 && t > 0 && (s + t) < 2 * A * sign;
}

However, I can't wrap my head around what this means at all...

1

1 Answer 1

2

You know the value of just x or just y has to be < two of the items or > two of the items but not > than three items or < three items.

If you are > than two items but less than 1 of x for the three coordinates, you know you are between the x values. If this holds true for x and y, then you are within the triangle.

Not the answer you're looking for? Browse other questions tagged or ask your own question.