First you need to find the slope of the line:
m = (y1 - y2) / (x1 - x2)
Then you need to find the equation of the line:
y = mx + b
In your example, we get:
y = 1x + b 0 = 1(0) + b
or
y = x
To get all the coordinates, you just need to connect all the values โโx1 โ x2. In PHP, it all looks something like this:
// These are in the form array(x_cord, y_cord) $pt1 = array(0, 0); $pt2 = array(10, 10); $m = ($pt1[1] - $pt2[1]) / ($pt1[0] - $pt2[0]); $b = $pt1[1] - $m * $pt1[0]; for ($i = $pt1[0]; $i <= $pt2[0]; $i++) $points[] = array($i, $m * $i + $b);
This, of course, will give you the coordinates for all points that fall on integer values โโof X, and not "all coordinates" between two points.
source share