Subversion Repositories eFlore/Projets.communes

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 jpm 1
<?php
2
class Line {
3
	public $p1;
4
	public $p2;
5
 
6
	public function __construct(Vector $p1,Vector $p2) {
7
		$this->p1 = $p1;
8
		$this->p2 = $p2;
9
	}
10
 
11
	public function LengthSquared() {
12
		$dx = $this->p1->x - $this->p2->x;
13
		$dy = $this->p1->y - $this->p2->y;
14
		return $dx*$dx + $dy*$dy;
15
	}
16
 
17
	public function DistanceToPointSquared(Vector $point) {
18
		$v = new Vector($point->x - $this->p1->x, $point->y - $this->p1->y);
19
		$l = new Vector($this->p2->x - $this->p1->x, $this->p2->y - $this->p1->y);
20
		$dot = $v->DotProduct($l->UnitVector());
21
		if ($dot <= 0) {// Point nearest P1
22
			$dl = new Line($this->p1, $point);
23
			return $dl->LengthSquared();
24
		}
25
		if (($dot*$dot)>=$this->LengthSquared()) {// Point nearest P2
26
			$dl = new Line($this->p2, $point);
27
			return $dl->LengthSquared();
28
		} else {// Point within line
29
			$v2 = new Line($this->p1, $point);
30
			$h = $v2->LengthSquared();
31
			return $h - $dot * $dot;
32
		}
33
	}
34
}
35
?>