I am trying to solve this, but not lucky:
$p1 = [0,0,0]; // x,y,z
$p2 = [20,10,1]; // x,y,z
$p3 = [30,15]; // x,y
$p3z = calc_z3($p1,$p2,$p3);
How to calculate the Z of P3?
CodePudding user response:
I see it like this:
l12 = sqrt( (x2-x1)^2 (y2-y1)^2 )
l13 = sqrt( (x3-x1)^2 (y3-y1)^2 )
z3 = z1 ((z2-z1)*l13/l12)
so:
| (x3-x1)^2 (y3-y1)^2 |^0.5
z3 = z1 (z2-z1)*|-----------------------|
| (x2-x1)^2 (y2-y1)^2 |
CodePudding user response:
I found this solution:
function calc_y3 ($x1,$y1,$x2,$y2,$x3) {
$m = ($y2 - $y1) / ($x2 - $x1);
$b = $y1 - $m * $x1;
$y3 = $m * $x3 $b;
return $y3;
}
function calc_z3 ($x1,$y1,$z1,$x2,$y2,$z2,$x3,$y3) {
$x = $x2-$x1;
$y = $y2-$y1;
$z = $z2-$z1;
if ($x == 0 and $y == 0) // it's a point
return $z1;
if ($x == 0) // it's a 2d line
return calc_y3($y1,$z1,$y2,$z2,$y3);
if ($y == 0) // it's a 2d line
return calc_y3($x1,$z1,$x2,$z2,$x3);
$t = ($y3-$y1)/$y;
$z3 = $z1 $t*$z;
return $z3;
}