PHP Find coordinates between two points

simple question here. Suppose I have two points:

point 1 x = 0 y = 0 point 2 x = 10 y = 10 

How would I know all the coordinates between them programmatically, assuming there is a strait line between two points ... so the above example will return:

 0,0 1,1 2,2 3,3 ... 8,8 9,9 10,10 

Thanks:)

+3
source share
5 answers

Thank you for your help, but not the answers did not work as I wanted it. For example, let's say my points were:

0, 0

0, 10

There will be only the starting and ending coordinates ... he will not find those that are between them.

Maybe I did something wrong: S, but I came up with my solution:

 // Points $p1 = array( 'x' => 50, 'y' => 50 ); $p2 = array( 'x' => 234, 'y' => 177 ); // Work out distances $pxd = $p2['x'] - $p1['x']; $pyd = $p2['y'] - $p1['y']; // Find out steps $steps = max($p1['x'], $p1['y'], $p2['x'], $p2['y']); $coords = array(); for ($i = 0; $i < $steps; ++ $i) { $coords[] = array( 'x' => round($p1['x'] += $pxd / $steps), 'y' => round($p1['y'] += $pyd / $steps) ); } print_r($coords); 
+3
source

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.

+2
source
  • Use the linear equation, y = mx + c
  • We set (0,0) and (10,10) to get two equations and decide to get the values โ€‹โ€‹of m and c. (you can find direct equations to get m and c somewhere).
  • Then create a loop to start with x = x1 (0) to x = x2 (10)
  • Using y = mx + c, we get the value of y (now you know m and c)
+1
source

A simple algorithm would be to find the midpoint by averaging the coordinates, repeat until you finish. Just wanted to point out because no one did.

+1
source

To generate all lattice points (points with integer coordinates) on the segment between (x1, y1) and (x2, y2), where x1, x2, y1 and y2 are integers:

 function gcd($a,$b) { // implement the Euclidean algorithm for finding the greatest common divisor of two integers, always returning a non-negative value $a = abs($a); $b = abs($b); if ($a == 0) { return $b; } else if ($b == 0) { return $a; } else { return gcd(min($a,$b),max($a,$b) % min($a,$b)); } } function lattice_points($x1, $y1, $x2, $y2) { $delta_x = $x2 - $x1; $delta_y = $y2 - $y1; $steps = gcd($delta_x, $delta_y); $points = array(); for ($i = 0; $i <= $steps; $i++) { $x = $x1 + $i * $delta_x / $steps; $y = $y1 + $i * $delta_y / $steps; $points[] = "({$x},{$y})"; } return $points; } 
0
source

All Articles