【问题标题】:Idenfitying all points at which gridlines are crossed by a straight line in a 3d cartesian graph识别 3d 笛卡尔图中网格线与直线相交的所有点
【发布时间】:2016-03-27 03:18:18
【问题描述】:

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

以下帖子很有帮助,但由于它处理的是 2D 系统,所以并不是我所需要的。

PHP Find Coordinates between two points

谢谢

杰森

【问题讨论】:

    标签: php 3d cartesian


    【解决方案1】:

    我不是 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);
    

    【讨论】:

      【解决方案2】:

      我不是专家,但是一种(愚蠢的?)方法是编写一个函数来找到两个坐标 ((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 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-23
        • 1970-01-01
        • 1970-01-01
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多