How many integer points within the three points forming a triangle?

后端 未结 13 1271
一个人的身影
一个人的身影 2020-12-12 21:04

Actually this is a classic problem as SO user Victor put it (in another SO question regarding which tasks to ask during an interview).

I couldn\'t do it in an hour

13条回答
  •  天涯浪人
    2020-12-12 21:43

    This is called the "Point in the Triangle" test.

    Here is an article with several solutions to this problem: Point in the Triangle Test.

    alt text

    A common way to check if a point is in a triangle is to find the vectors connecting the point to each of the triangle's three vertices and sum the angles between those vectors. If the sum of the angles is 2*pi (360-degrees) then the point is inside the triangle, otherwise it is not.

提交回复
热议问题