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

后端 未结 13 1299
一个人的身影
一个人的身影 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 found a quite useful link which clearly explains the solution to this problem. I am weak in coordinate geometry so I used this solution and coded it in Java which works (at least for the test cases I tried..)

    http://mathforum.org/library/drmath/view/55169.html

    public int points(int[][] vertices){
          int interiorPoints = 0;
          double triangleArea = 0;
          int x1 = vertices[0][0], x2 = vertices[1][0], x3 = vertices[2][0];
          int y1 = vertices[0][1], y2 = vertices[1][1], y3 = vertices[2][1];
    
          triangleArea = Math.abs(((x1-x2)*(y1+y2))                             
                    + ((x2-x3)*(y2+y3))
                    + ((x3-x1)*(y3+y1)));
    
          triangleArea /=2;
          triangleArea++;
    
          interiorPoints = Math.abs(gcd(x1-x2,y1-y2))
                    + Math.abs(gcd(x2-x3, y2-y3))
                    + Math.abs(gcd(x3-x1, y3-y1));
    
          interiorPoints /=2;
    
          return  (int)(triangleArea - interiorPoints);
     }
    

提交回复
热议问题