从点到由两点表示的直线的垂直距离

时间:2013-04-08 14:26:52

标签: php geometry cartesian cartesian-coordinates

我有一个点C(Cx,Cy),然后是一个由两个点A(Ax,Ay)和B(Bx,By)表示的线。 我需要找到C点和AB代表的线之间的垂直距离。 我如何在PHP中执行此操作?

1 个答案:

答案 0 :(得分:3)

答案很简单。它是数学而不是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;
   ?>