PHP查找两点之间的坐标 [英] PHP Find Coordinates between two points

查看:100
本文介绍了PHP查找两点之间的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个简单的问题.可以说我有两点:

simple question here. Lets say I have two points:

point 1

x = 0
y = 0


point 2

x = 10
y = 10

假设两个点之间有一条海峡线,我将如何以编程方式找出它们之间的所有坐标...因此,上面的示例将返回:

How would i find out all the coordinates inbetween that programmatically, assuming there is a strait line between two points... so the above example would return:

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

谢谢:)

推荐答案

感谢您的所有帮助,但没有发布的答案可以满足我的要求.例如,假设我的观点是:

thanks for all your help but non of the answers posted worked how i wanted it to. For example, lets say my points were:

0,0

0,10

只有一个起点和一个终点坐标...它之间找不到坐标.

There would only be a start and a finish coordinate... it wouldnt find the ones inbetween.

也许我做错了:S但我想出了自己的解决方案:

Maybe i did something wrong :S but i came up with my own 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);

这篇关于PHP查找两点之间的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆