# Number of Integral Points between Two Points

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).

## We strongly recommend that you click here and practice it, before moving on to the solution.

**Simple Approach**

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)).

**Optimal Approach**

1. If the edge formed by joiningpandqis 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.

## C++

`// C++ code to find the number of integral points ` `// lying on the line joining the two given points ` `#include <iostream> ` `#include <cmath> ` `using` `namespace` `std; ` ` ` `// Class to represent an Integral point on XY plane. ` `class` `Point ` `{ ` `public` `: ` ` ` `int` `x, y; ` ` ` `Point(` `int` `a=0, ` `int` `b=0):x(a),y(b) {} ` `}; ` ` ` `// Utility function to find GCD of two numbers ` `// GCD of a and b ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `gcd(b, a%b); ` `} ` ` ` `// Finds the no. of Integral points between ` `// two given points. ` `int` `getCount(Point p, Point q) ` `{ ` ` ` `// If line joining p and q is parallel to ` ` ` `// x axis, then count is difference of y ` ` ` `// values ` ` ` `if` `(p.x==q.x) ` ` ` `return` `abs` `(p.y - q.y) - 1; ` ` ` ` ` `// If line joining p and q is parallel to ` ` ` `// y axis, then count is difference of x ` ` ` `// values ` ` ` `if` `(p.y == q.y) ` ` ` `return` `abs` `(p.x-q.x) - 1; ` ` ` ` ` `return` `gcd(` `abs` `(p.x-q.x), ` `abs` `(p.y-q.y))-1; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `Point p(1, 9); ` ` ` `Point q(8, 16); ` ` ` ` ` `cout << ` `"The number of integral points between "` ` ` `<< ` `"("` `<< p.x << ` `", "` `<< p.y << ` `") and ("` ` ` `<< q.x << ` `", "` `<< q.y << ` `") is "` ` ` `<< getCount(p, q); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to find the number of integral points ` `// lying on the line joining the two given points ` ` ` `class` `GFG ` `{ ` ` ` `// Class to represent an Integral point on XY plane. ` `static` `class` `Point ` `{ ` ` ` `int` `x, y; ` ` ` `Point(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `this` `.x = a; ` ` ` `this` `.y = b; ` ` ` `} ` `}; ` ` ` `// Utility function to find GCD of two numbers ` `// GCD of a and b ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` `return` `gcd(b, a % b); ` `} ` ` ` `// Finds the no. of Integral points between ` `// two given points. ` `static` `int` `getCount(Point p, Point q) ` `{ ` ` ` `// If line joining p and q is parallel to ` ` ` `// x axis, then count is difference of y ` ` ` `// values ` ` ` `if` `(p.x == q.x) ` ` ` `return` `Math.abs(p.y - q.y) - ` `1` `; ` ` ` ` ` `// If line joining p and q is parallel to ` ` ` `// y axis, then count is difference of x ` ` ` `// values ` ` ` `if` `(p.y == q.y) ` ` ` `return` `Math.abs(p.x - q.x) - ` `1` `; ` ` ` ` ` `return` `gcd(Math.abs(p.x - q.x), Math.abs(p.y - q.y)) - ` `1` `; ` `} ` ` ` `// Driver program to test above ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `Point p = ` `new` `Point(` `1` `, ` `9` `); ` ` ` `Point q = ` `new` `Point(` `8` `, ` `16` `); ` ` ` ` ` `System.out.println(` `"The number of integral points between "` ` ` `+ ` `"("` `+ p.x + ` `", "` `+ p.y + ` `") and ("` ` ` `+ q.x + ` `", "` `+ q.y + ` `") is "` ` ` `+ getCount(p, q)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find the number of ` `# integral points lying on the line ` `# joining the two given points ` ` ` `# Class to represent an Integral point ` `# on XY plane. ` `class` `Point: ` ` ` ` ` `def` `__init__(` `self` `, a, b): ` ` ` `self` `.x ` `=` `a ` ` ` `self` `.y ` `=` `b ` ` ` `# Utility function to find GCD ` `# of two numbers GCD of a and b ` `def` `gcd(a, b): ` ` ` ` ` `if` `b ` `=` `=` `0` `: ` ` ` `return` `a ` ` ` `return` `gcd(b, a ` `%` `b) ` ` ` `# Finds the no. of Integral points ` `# between two given points. ` `def` `getCount(p, q): ` ` ` ` ` `# If line joining p and q is parallel ` ` ` `# to x axis, then count is difference ` ` ` `# of y values ` ` ` `if` `p.x ` `=` `=` `q.x: ` ` ` `return` `abs` `(p.y ` `-` `q.y) ` `-` `1` ` ` ` ` `# If line joining p and q is parallel ` ` ` `# to y axis, then count is difference ` ` ` `# of x values ` ` ` `if` `p.y ` `=` `=` `q.y: ` ` ` `return` `abs` `(p.x ` `-` `q.x) ` `-` `1` ` ` ` ` `return` `gcd(` `abs` `(p.x ` `-` `q.x), ` ` ` `abs` `(p.y ` `-` `q.y)) ` `-` `1` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `p ` `=` `Point(` `1` `, ` `9` `) ` ` ` `q ` `=` `Point(` `8` `, ` `16` `) ` ` ` ` ` `print` `(` `"The number of integral points"` `, ` ` ` `"between ({}, {}) and ({}, {}) is {}"` `. ` ` ` `format` `(p.x, p.y, q.x, q.y, getCount(p, q))) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# code to find the number of integral points ` `// lying on the line joining the two given points ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Class to represent an Integral point on XY plane. ` `public` `class` `Point ` `{ ` ` ` `public` `int` `x, y; ` ` ` `public` `Point(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `this` `.x = a; ` ` ` `this` `.y = b; ` ` ` `} ` `}; ` ` ` `// Utility function to find GCD of two numbers ` `// GCD of a and b ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` `return` `gcd(b, a % b); ` `} ` ` ` `// Finds the no. of Integral points between ` `// two given points. ` `static` `int` `getCount(Point p, Point q) ` `{ ` ` ` `// If line joining p and q is parallel to ` ` ` `// x axis, then count is difference of y ` ` ` `// values ` ` ` `if` `(p.x == q.x) ` ` ` `return` `Math.Abs(p.y - q.y) - 1; ` ` ` ` ` `// If line joining p and q is parallel to ` ` ` `// y axis, then count is difference of x ` ` ` `// values ` ` ` `if` `(p.y == q.y) ` ` ` `return` `Math.Abs(p.x - q.x) - 1; ` ` ` ` ` `return` `gcd(Math.Abs(p.x - q.x), Math.Abs(p.y - q.y)) - 1; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `Point p = ` `new` `Point(1, 9); ` ` ` `Point q = ` `new` `Point(8, 16); ` ` ` ` ` `Console.WriteLine(` `"The number of integral points between "` ` ` `+ ` `"("` `+ p.x + ` `", "` `+ p.y + ` `") and ("` ` ` `+ q.x + ` `", "` `+ q.y + ` `") is "` ` ` `+ getCount(p, q)); ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

Output:

Output:

The number of integral points between (1, 9) and (8, 16) is 6

**Reference : **

https://www.geeksforgeeks.org/count-integral-points-inside-a-triangle/

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 contribute@geeksforgeeks.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

## Recommended Posts:

- Minimum number of points to be removed to get remaining points on one side of axis
- Prime points (Points that split a number into two primes)
- Count Integral points inside a Triangle
- Steps required to visit M points in order on a circular ring of N points
- 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
- Number of quadrilaterals possible from the given points
- Number of triangles that can be formed with given N points
- Maximum number of segments that can contain the given points
- Number of triangles formed from a set of points on three lines
- Print all combinations of points that can compose a given number
- Number of triangles in a plane if no more than two points are collinear
- Number of cells in matrix which are equidistant from given two points
- Number of pairs of lines having integer intersection points
- Find the number of points that have atleast 1 point above, below, left or right of it