-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.php
125 lines (109 loc) · 2.3 KB
/
point.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* libvector3d
*
* Git: <https://git.eliteheberg.fr/younishd/libvector3d>
* Author: YouniS Bensalah <younis.bensalah@riseup.net> <http://younishd.fr>
* License: This program is under a MIT License.
*/
/**
* A 3D Point.
*/
class Point {
public function __construct($x, $y, $z)
{
$this->_x = $x;
$this->_y = $y;
$this->_z = $z;
$this->_id = '';
}
/**
* construct point from coordinates.
*/
static public function fromCoordinates($x, $y, $z)
{
$point = new Point($x, $y, $z);
return $point;
}
/**
* construct point from vector (pointing towards the point).
*/
static public function fromVector(Vector $vector)
{
$point = new Point();
$point->x($vector->x);
$point->y($vector->y);
$point->z($vector->z);
return $point;
}
/**
* construct a vector from this point instance.
*/
public function getVector()
{
$vector = new Vector();
$vector->x($this->x());
$vector->y($this->y());
$vector->z($this->z());
return $vector;
}
/**
* construct a vector from a point instance.
*/
static public function vectorFromPoint(Point $point)
{
return $point->getVector();
}
/**
* get/set X-coordinate.
*/
public function x($x = null)
{
if ($x === null) {
return $this->_x;
}
$this->_x = $x;
}
/**
* get/set Y-coordinate.
*/
public function y($y = null)
{
if ($y === null) {
return $this->_y;
}
$this->_y = $y;
}
/**
* get/set Z-coordinate.
*/
public function z($z = null)
{
if ($z === null) {
return $this->_z;
}
$this->_z = $z;
}
/**
* get/set ID.
*/
public function id($id = null)
{
if ($id === null) {
return $this->_id;
}
$this->_id = (string)$id;
}
public function display()
{
echo $this->_id . '('.$this->_x.';'.$this->_y.';'.$this->_z.')';
}
/** X-coordinate. */
protected $_x;
/** Y-coordinate. */
protected $_y;
/** Z-coordinate. */
protected $_z;
/** ID. */
protected $_id;
}