php find distance of a point and a line segment not a line in 2D
问题 I have two point of a line like p1(a,b) and p2(c,d) my point is X(x,y) I've searched and find like here but it isn't php can anyone help me 回答1: DISCLAIMER: I assumed the JS code from the linked answer works. Below is my attempt to convert the javascript code from here to PHP. function sqr($x) { return $x * $x; } function dist2($v, $w) { return sqr($v->x - $w->x) + sqr($v->y - $w->y); } function distToSegmentSquared($p, $v, $w) { $l2 = dist2($v, $w); if ($l2 == 0) return dist2($p, $v); $t = (