Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find out the number of integral coordinates on a straight line between two points in Python
When we have two points (p1, q1) and (p2, q2), we can find the number of integral coordinates (points where both x and y are integers) that lie on the straight line between them. This problem uses the mathematical concept that the number of lattice points on a line segment equals the GCD of the coordinate differences.
So, if the input is like p1 = 3, q1 = 3, p2 = 6, q2 = 6, then the output will be 2. The integral points (4, 4) and (5, 5) lie on the straight line between (3, 3) and (6, 6).
Algorithm
To solve this problem, we follow these steps:
- Calculate the absolute differences: |p2 - p1| and |q2 - q1|
- Find the GCD (Greatest Common Divisor) of these differences
- Return GCD - 1 (excluding the endpoints)
Why GCD Works
The mathematical reasoning is that if we have a line from (0, 0) to (a, b), the number of lattice points on this line is gcd(a, b) + 1. For a line segment between any two points, we subtract 1 to exclude the endpoints from the count.
Implementation
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
# Test the function
print(solve(3, 3, 6, 6))
print(solve(1, 1, 4, 4))
print(solve(0, 0, 6, 4))
2 2 1
Using Built-in Math Module
We can also use Python's built-in math.gcd() function for a cleaner solution:
import math
def solve_builtin(p1, q1, p2, q2):
return math.gcd(abs(p2 - p1), abs(q2 - q1)) - 1
# Test cases
test_cases = [(3, 3, 6, 6), (1, 1, 4, 4), (0, 0, 6, 4), (2, 3, 8, 9)]
for p1, q1, p2, q2 in test_cases:
result = solve_builtin(p1, q1, p2, q2)
print(f"Points ({p1}, {q1}) to ({p2}, {q2}): {result} integral points")
Points (3, 3) to (6, 6): 2 integral points Points (1, 1) to (4, 4): 2 integral points Points (0, 0) to (6, 4): 1 integral points Points (2, 3) to (8, 9): 5 integral points
Verification Example
Let's verify the result for points (3, 3) to (6, 6):
# For line from (3,3) to (6,6)
# The line equation is y = x
# Points on this line: (4,4), (5,5)
# So there are 2 integral points between the endpoints
def verify_points(p1, q1, p2, q2):
points = []
# Simple verification for diagonal lines
if abs(p2 - p1) == abs(q2 - q1):
step_x = 1 if p2 > p1 else -1
step_y = 1 if q2 > q1 else -1
x, y = p1 + step_x, q1 + step_y
while x != p2 and y != q2:
points.append((x, y))
x += step_x
y += step_y
return points
points = verify_points(3, 3, 6, 6)
print(f"Integral points: {points}")
print(f"Count: {len(points)}")
Integral points: [(4, 4), (5, 5)] Count: 2
Conclusion
The number of integral coordinates between two points on a line equals GCD(|x2-x1|, |y2-y1|) - 1. This elegant mathematical approach efficiently solves the problem using the Euclidean algorithm for GCD calculation.
