2
$\begingroup$

I have two lattice (grid) end points of the line, how can I generate intermediate points of the line segment between them?
One way is iterate through all the x points or y points and check (solve line equation) if the coordinates we get each time are integral solution or not.

$\endgroup$
1
  • 3
    $\begingroup$ I suggest rephrasing the question to include all necessary details, as well as to clearly indicate what the desired output is like. Please spend a few minutes making your question as clear as possible, using as many words as necessary. $\endgroup$ Feb 9, 2018 at 15:37

2 Answers 2

3
$\begingroup$

For a line segment defined by two integral lattice points $(x_0,y_0), (x_1,y_1)$ there will be $gcd(y_1-y_0,x_1-x_0)-1$ additional integral points along the line segment. To find them move along line by largest value in the reduced slope.

$\endgroup$
2
  • $\begingroup$ Let the slope be in p/q (reduced form) .Now how do I move? $\endgroup$
    – Sathyaram
    Feb 10, 2018 at 4:46
  • 1
    $\begingroup$ Add q,p to x,y until you reach end of line segment. $\endgroup$
    – Tomoki
    Feb 10, 2018 at 6:37
2
$\begingroup$

It seems that you are interested in line rasterization or digitization algorithm, meaning that you want efficiently find continous representation of line in discrete grid.
Very efficient algorithm was made by Bresenham. There are other algorithms like DDA, or anialiased Wu line algorithm.

The core idea is to calculate gradient of the line, iterate by discrete step (here exactly pixel) and determine the exact position (one of two adjacent pixels).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.