문제

I have a point C(Cx,Cy) and then a line represented by two points A(Ax,Ay) and B(Bx,By). I need to find the perpendicular distance between the point C and the line represented by AB. How do I do this in PHP?

도움이 되었습니까?

해결책

The answer is straight forward.Its mathematics rather than 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;
   ?>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top