- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find out the number of integral coordinates on a straight line between two points in Python

Suppose, we have been provided with two points (p1, q1) and (p2, q2). We have to find out the number of integral coordinates (both the x and y values are integers) if a straight line is drawn between the two given points. The number of points is returned.

So, if the input is like p1 = 3, q1 = 3, p2 = 6, q2 = 6, then the output will be 2 If we draw the straight line, we will see that the points (5,5) and (6,6) are situated on the straight line.

To solve this, we will follow these steps −

- Define a function gcd_find() . This will take x,y
- if y is same as 0, then
- return x

- return gcd_find(y, x mod y)

- if y is same as 0, then

From the main method/function, do the following −

- return gcd_find(|p2 - p1| , |q2 - q1|) - 1

## Example

Let us see the following implementation to get better understanding −

def gcd_find(x,y): if y == 0: return x return gcd_find(y,x % y) def solve(p1,q1,p2,q2): return gcd_find(abs(p2 - p1),abs(q2 - q1)) - 1 print(solve(3,3,6,6))

## Input

3,3,6,6

## Output

2

- Related Questions & Answers
- Number of Integral Points between Two Points in C++
- Draw a curve connecting two points instead of a straight line in matplotlib
- Program to count number of points that lie on a line in Python
- Program to check whether list of points form a straight line or not in Python
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- Program to Find Out the Points Achievable in a Contest in Python
- C++ program to find out the center coordinates and the height of a building
- Represent a Given Set of Points by the Best Possible Straight Line in C++
- Program to Find Out the Maximum Points From Removals in Python
- Program to find out the maximum points collectable in a game in Python
- Program to find out the vertical area between two points where no point lies and is the widest in Python
- Program to find out the length between two cities in shortcuts in Python
- Program to Find the Shortest Distance Between Two Points in C++
- Program to find out the number of accepted invitations in Python
- Program to Find Out the Number of Squares in a Grid in Python

Advertisements