New file |
0,0 → 1,35 |
<?php |
class Line { |
public $p1; |
public $p2; |
|
public function __construct(Vector $p1,Vector $p2) { |
$this->p1 = $p1; |
$this->p2 = $p2; |
} |
|
public function LengthSquared() { |
$dx = $this->p1->x - $this->p2->x; |
$dy = $this->p1->y - $this->p2->y; |
return $dx*$dx + $dy*$dy; |
} |
|
public function DistanceToPointSquared(Vector $point) { |
$v = new Vector($point->x - $this->p1->x, $point->y - $this->p1->y); |
$l = new Vector($this->p2->x - $this->p1->x, $this->p2->y - $this->p1->y); |
$dot = $v->DotProduct($l->UnitVector()); |
if ($dot <= 0) {// Point nearest P1 |
$dl = new Line($this->p1, $point); |
return $dl->LengthSquared(); |
} |
if (($dot*$dot)>=$this->LengthSquared()) {// Point nearest P2 |
$dl = new Line($this->p2, $point); |
return $dl->LengthSquared(); |
} else {// Point within line |
$v2 = new Line($this->p1, $point); |
$h = $v2->LengthSquared(); |
return $h - $dot * $dot; |
} |
} |
} |
?> |