Sollicitatievraag bij Susquehanna International Group

Write a function to determine whether a point lies within a triangle (2D space).

Antwoorden op sollicitatievragen

Anoniem

2 dec 2010

I knew how to do this in 3D because I'd done it numerous times before, but my mind panicked and blanked and I couldn't figure out how to step back to 2D. I ended up doing it in 3D, which confused them but they let it slide. Basically, if you had triangle a,b,c and point p, you check to see if point p lies on the same side of each line (e.g. ab) as the third point (e.g. c). I did this by setting the z value of all vectors to 0 (since it was 2D space), taking the cross products abxap and abxac (for example) and seeing if the resulting vectors' z values had the same sign (both negative or both positive). There's a better way to do it in 2D though.

Anoniem

11 mei 2012

Let the triangle is abc and the point is p. Then calculate the area of the three triangles abp, acp and bcp. if the sum is not equal to the area of abc then the point is outside.

1

Anoniem

22 okt 2020

niubi answer