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

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

    I have this idea -

    Let A(x1, y1), B(x2, y2) and C(x3, y3) be the vertices of the triangle. Let 'count' be the number of integer points forming the triangle.

    If we need the points on the triangle edges then using Euclidean Distance formula http://en.wikipedia.org/wiki/Euclidean_distance, the length of all three sides can be ascertained. The sum of length of all three sides - 3, would give that count.

    To find the number of points inside the triangle we need to use a triangle fill algorithm and instead of doing the actual rendering i.e. executing drawpixel(x,y), just go through the loops and keep updating the count as we loop though. A triangle fill algorithm from

    Fundamentals of Computer Graphics by Peter Shirley,Michael Ashikhmin

    should help. Its referred here http://www.gidforums.com/t-20838.html

    cheers

提交回复
热议问题