I've points for a big triangle lets call it a, b, c. (a = (x, y) and so on).
Now I want to count the number of integral points inside the area enclosed by this triangle, so I first looked at Pick's theorem. The second approach that I considered was generating a list of points bounded by max, min of the triangle, and then checking if each point lies inside the triangle.
I used the Barycentric coordinates method to do this. It works however my triangle is pretty huge and my program is basically a brute force across points. How do I improve on this algorithm?
My code can be found here: https://bpaste.net/show/58433b6e389c
This problem can and should be solved using Pick's theorem. Read the article to have a full understanding on how it works and it will work for any polygon you can think of. So, "Pick says" that if you want to compute the area of a polygon you have the formula
area = noOfInsidePoints + noOfBoundaryPoints /2 - 1
. To compute the area of any polygon you can use the following code, wherepc
is an array of structures representing the vertices of the polygon.Having the area computed, we have to count the number of point from all of the polygon edges. This can be done using the following:
Having this computed too, we can find the number of point inside
Final time complexity: O(N) Final memory complexity: O(N)