这个问题已经有了答案:
Shortest distance between a point and a line segment
50答
我有一个点c(cx,cy),然后是一条线,由两点a(ax,ay)和b(bx,by)表示。
我需要找到C点和AB线之间的垂直距离。
如何在php中实现这一点?

最佳答案

答案是直截了当的。它的数学而不是php

<?php
    //Coordinates are (a,b) and (c,d)
    //the point (x,y) is the required point.
    $a=1;
    $b=2;
    $c=3;
    $d=4;

    $m=($d-$b)/($c-$a);
    //echo $m."\n";

    $x=10;
    $y=20;
    //echo $y-($m*$x)-$b+($m*$a)."\n";
    $distance=abs($y-($m*$x)-$b+($m*$a))/sqrt(1+($m*$m));
    echo $distance;
   ?>

10-08 08:31
查看更多