D
D
Daniel Demidko2018-12-01 14:03:10
Mathematics
Daniel Demidko, 2018-12-01 14:03:10

How to find all integer points of a segment?

5c026a6bef030077960037.png
Known segment given by two points (x1, y1), (x2, y2).
I need to find all integer points lying on this segment.
How to find all these points?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-12-01
@DanielDemidko

For arbitrary (x₁, y₁) and (x₂, y₂), IMHO, just take all integer values ​​between x₁ and x₂ and check the value of y at the resulting points.
For integers (x₁, y₁) and (x₂, y₂) you can do this:
n = gcd(|x₂ − x₁|, |y₂ − y₁|)
dx = (x₂ − x₁) / n
dy = (y₂ − y₁) / n
And take all pairs (x₁ + dx × i, y₁ + dy × i) for i = 1 .. n − 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question