Given two points p (x1, y1) and q (x2, y2), calculate the number of integral points lying on the line joining them.
Example : If points are (0, 2) and (4, 0), then the number of integral points lying on it is only one and that is (2, 1).
Similarly, if points are (1, 9) and (8, 16), the integral points lying on it are 6 and they are (2, 10), (3, 11), (4, 12), (5, 13), (6, 14) and (7, 15).
Start from any of the given points, reach the other end point by using loops. For every point inside the loop, check if it lies on the line that joins given two points. If yes, then increment the count by 1. Time Complexity for this approach will be O(min(x2-x1, y2-y1)).
1. If the edge formed by joining p and q is parallel to the X-axis, then the number of integral points between the vertices is : abs(p.y - q.y)-1 2. Similarly if edge is parallel to the Y-axis, then the number of integral points in between is : abs(p.x - q.x)-1 3. Else, we can find the integral points between the vertices using below formula: GCD(abs(p.x - q.x), abs(p.y - q.y)) - 1
How does the GCD formula work?
The idea is to find the equation of the line in simplest form, i.e., in equation ax + by +c, coefficients a, b and c become co-prime. We can do this by calculating the GCD (greatest common divisor) of a, b and c and convert a, b and c in the simplest form.
Then, the answer will be (difference of y coordinates) divided by (a) – 1. This is because after calculating ax + by + c = 0, for different y values, x will be number of y values which are exactly divisible by a.
Below is the implementation of above idea.
The number of integral points between (1, 9) and (8, 16) is 6
This article has been contributed by Paridhi Johari. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count Integral points inside a Triangle
- Generate all integral points lying inside a rectangle
- Find integral points with minimum distance from given set of integers using BFS
- Prime points (Points that split a number into two primes)
- Number of non-negative integral solutions of a + b + c = n
- Number of integral solutions of the equation x1 + x2 +.... + xN = k
- Number of non-negative integral solutions of sum equation
- Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c
- Trapezoidal Rule for Approximate Value of Definite Integral
- Maximum integral co-ordinates with non-integer distances
- Program for finding the Integral of a given function using Boole's Rule
- Count of integral coordinates that lies inside a Square
- Probability that an arbitrary positive divisor of 10^X is an integral multiple of 10^Y
- Find initial integral solution of Linear Diophantine equation if finite solution exists
- Find the integral roots of a given Cubic equation
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Ways to choose three points with distance between the most distant points <= L
- Minimum number of points to be removed to get remaining points on one side of axis
- Steps required to visit M points in order on a circular ring of N points
- Find the point on X-axis from given N points having least Sum of Distances from all other points