识别 3d 笛卡尔图中网格线与直线相交的所有点

发布于 2024-12-01 22:46:07 字数 292 浏览 0 评论 0原文

我正在使用 PHP 并生成了一个 3D 笛卡尔坐标系 (x,y,z)。我想在两点之间走一条直线。我将每个 1x1x1 的正方形称为由最接近原点的相邻格点标识的扇区。我试图识别线段经过的每个扇区。

以下帖子很有帮助,但由于它涉及 2D 系统,因此并不完全是我所需要的。

PHP查找两点之间的坐标

谢谢杰森

I'm using PHP and have generated a 3D cartesian coordinate system (x,y,z). I want to travel a straight line between two points. I call each 1x1x1 square a sector identified by the adjacent lattice point closest to the origin. I'm trying to identify each sector that the line segment passes through.

The following post was helpful, but since it deals with 2D systems, wasn't exactly what I needed.

PHP Find Coordinates between two points

Thanks

Jason

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉墨 2024-12-08 22:46:07

我不是 PHP 专家,但这对于 3d 系统来说是相同的解决方案:

// Points
$p1 = array(
    'x' => 50,
    'y' => 50,
    'z' => 20,

);

$p2 = array(
    'x' => 234,
    'y' => 177,
    'z' => 100
);

// Work out distances
$pxd = $p2['x'] - $p1['x'];
$pyd = $p2['y'] - $p1['y'];
$pzd = $p2['z'] - $p1['z'];
// Find out steps
$steps = max(abs($pxd), abs($pyd), abs($pzd));

$coords = array();

for ($i = 0; $i < $steps; ++ $i) {
    $coords[] = array(
        'x' => round($p1['x'] += $pxd / $steps),
        'y' => round($p1['y'] += $pyd / $steps),
        'z' => round($p1['z'] += $pzd / $steps)
    );
}

print_r($coords);

I am not an expert for the PHP, but this is the same solution for the 3d system:

// Points
$p1 = array(
    'x' => 50,
    'y' => 50,
    'z' => 20,

);

$p2 = array(
    'x' => 234,
    'y' => 177,
    'z' => 100
);

// Work out distances
$pxd = $p2['x'] - $p1['x'];
$pyd = $p2['y'] - $p1['y'];
$pzd = $p2['z'] - $p1['z'];
// Find out steps
$steps = max(abs($pxd), abs($pyd), abs($pzd));

$coords = array();

for ($i = 0; $i < $steps; ++ $i) {
    $coords[] = array(
        'x' => round($p1['x'] += $pxd / $steps),
        'y' => round($p1['y'] += $pyd / $steps),
        'z' => round($p1['z'] += $pzd / $steps)
    );
}

print_r($coords);
橘亓 2024-12-08 22:46:07

我不是专家,但一种(愚蠢的?)方法是编写一个函数来查找两个坐标的“中点”((x1 + x2)/2,(y1 + y2)/2,(z1 + z2) )/2),然后将它们舍入以获得真正的坐标。现在,您只需递归地将函数应用于所有腿,直到没有新的中间点,就可以找到整条“线”。

func findMiddlePoint(a, b)
  return new Point(
        ((a.x + b.x).abs / 2).round, 
        ((a.y + b.y).abs / 2).round, 
        ((a.z + b.z).abs / 2).round)
func findLine(points, a, b)
  if a == b # no new points
    return 
  middle = findMiddlePoint(a, b)
  points.add(middle)
  findLine(points, a, middle)
  findLine(points, middle, b)

func findFullLine(a, b)
  points = []
  points.add(a)
  findLine(points, a, b)
  opints.add(b)
  return points 

I'm no expert, but one (stupid?) approach would be to write a function which finds the 'middle point' of two coordinates ((x1 + x2)/2, (y1 + y2)/2, (z1 + z2)/2), then round them to get a real coordinate. Now you could find the whole 'line' by just recursively apply the function to all legs until there's no new middle point.

func findMiddlePoint(a, b)
  return new Point(
        ((a.x + b.x).abs / 2).round, 
        ((a.y + b.y).abs / 2).round, 
        ((a.z + b.z).abs / 2).round)
func findLine(points, a, b)
  if a == b # no new points
    return 
  middle = findMiddlePoint(a, b)
  points.add(middle)
  findLine(points, a, middle)
  findLine(points, middle, b)

func findFullLine(a, b)
  points = []
  points.add(a)
  findLine(points, a, b)
  opints.add(b)
  return points 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文