【问题标题】:Plotting points on a line, between points in array在一条线上绘制点,在数组中的点之间
【发布时间】:2020-04-06 01:55:32
【问题描述】:

我确定这是一个简单的问题,但我的大脑现在拒绝工作......我有一系列点,看起来像:

$points = [
    ["x" => 12, "z" => 23],
    ["x" => 42, "z" => 64] etc...
]

现在,我需要在所有点之间画一条“虚线”,基本上形成一个正方形或封闭路径,仅包含 90 度转弯。我想出了这段代码:

$x = 0;
$y = $this->getYCord(); 
$z = 0;

for($pi = 1; $pi >= count($points); $pi++){
    for($x = $points[$pi - 1]["x"]; $x <= $points[$pi]["x"]; $x += 0.5){
        $this->addDot(new Dot($x, $y, $z));
    }
    for($z = $points[$pi - 1]["z"]; $z <= $points[$pi]["z"]; $z += 0.5){
        $this->addDot(new Dot($x, $y, $z));
    }
}

但是如你所见,这样会遇到很多或者错误,On 是:如果第二个点坐标较小怎么办?然后我们需要在循环中-=。任何帮助表示赞赏!谢谢!

【问题讨论】:

    标签: php math plot coordinate-systems


    【解决方案1】:

    可能的方法:

    $left = min($points[$pi]["x"], $points[$pi - 1]["x"]);
    $right = max($points[$pi]["x"], $points[$pi - 1]["x"]);
    for($x = $left; $x <= $right; $x += 0.5) ...
    

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 2019-08-15
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 2016-12-20
      相关资源
      最近更新 更多