Given three non-collinear integral points in XY plane, find the number of integral points inside the triangle formed by the three points. (A point in XY plane is said to be integral/lattice point if both its co-ordinates are integral).
Input: p = (0, 0), q = (0, 5) and r = (5,0) Output: 6 Explanation: The points (1,1) (1,2) (1,3) (2,1) (2,2) and (3,1) are the integral points inside the triangle.
We can use the Pick’s theorem, which states that the following equation holds true for a simple Polygon.
Pick's Theeorem: A = I + (B/2) -1 A ==> Area of Polygon B ==> Number of integral points on edges of polygon I ==> Number of integral points inside the polygon Using the above formula, we can deduce, I = (2A - B + 2) / 2
We can find A (area of triangle) using below Shoelace formula.
A = 1/2 * abs(x1(y2 - y3) + x2(y3 - y1) + x3(y1 - y2))
How to find B (number of integral points on edges of a triangle)?
We can find the number of integral points between any two vertex (V1, V2) of the triangle using the following algorithm.
1. If the edge formed by joining V1 and V2 is parallel to the X-axis, then the number of integral points between the vertices is : abs(V1.y - V2.y)-1 2. Similarly, if edge is parallel to the Y-axis, then the number of integral points in between is : abs(V1.x - V2.y)-1 3. Else, we can find the integral points between the vertices using below formula: GCD(abs(V1.x-V2.x), abs(V1.y-V2.y)) - 1 The above formula is a well known fact and can be verified using simple geometry. (Hint: Shift the edge such that one of the vertex lies at the Origin.) Please refer below link for detailed explanation. https://www.geeksforgeeks.org/number-integral-points-two-points/
Below is the implementation of the above algorithm.
Number of integral points inside given triangle is 6
This article is contributed by Ashutosh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Queries on count of points lie inside a circle
- Number of Integral Points between Two Points
- Coordinates of rectangle with given points lie inside
- Triangle with no point inside
- Area of a triangle inside a parallelogram
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Check whether a given point lies inside a triangle or not
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Count the number of rhombi possible inside a rectangle of given size
- Count maximum points on same line
- Count of triangles with total n points with m collinear
- Count of different straight lines with total n points with m collinear
- Count distinct points visited on the number line
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Count of distinct rectangles inscribed in an equilateral triangle