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

后端 未结 13 1341
一个人的身影
一个人的身影 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:31

    My knee-jerk reaction would be to brute-force it:

    • Find the maximum and minimum extent of the triangle in the x and y directions.
    • Loop over all combinations of integer points within those extents.
    • For each set of points, use one of the standard tests (Same side or Barycentric techniques, for example) to see if the point lies within the triangle. Since this sort of computation is a component of algorithms for detecting intersections between rays/line segments and triangles, you can also check this link for more info.

提交回复
热议问题