Assume an imaginary line starting at ($x_1$,$y_1$) and ending at ($x_2$,$y_2$) having a fixed distance 5. (a,b) is a point which lies on the line. If ($x_1$,$y_1$) and (a,b) is given then how do I find all the whole number points that lie on the imaginary line?
For instance, if ($x_1$,$y_1$) is (0,1), (a,b) is (0,4) and length of the line is 5, then ($x_2$,$y_2$) is (0,6) and the whole number points lying on the line are (0,2) (0,3) (0,4) and (0,5). I want to find the points using a formula that I can implement in a C++ program.
I was trying to find the missing point ($x_2$,$y_2$) and use the Bresenham's line algorithm to find the co-ordinates that lie on the line, but is there any faster and simpler method to compute this?
The slope of the line is $\frac {b-y_1}{a-x_1}$ so the equation of the line is $y-y_1=\frac {b-y_1}{a-x_1}(x-x_1)$ You can find $(x_2,y_2)$ by plugging that into the line and solving simultaneously with $(x_2-x_1)^2+(y_2-y_1)^2=25$ Then you can check the whole numbers between $x_1$ and $x_2$, plug into the equation of the line, and see if they yield whole numbers in $y$. Note that if you are using floating point variables for this, detecting a whole number is hard because of numeric precision issues.