Simple PHP Vector (3D) class


3D vector operations are quite useful and common in the math world, especially when it involves the geometry. The following is a simple class made in PHP that encapsulates most vector operations such as additions, cross product. Feel free to use the code in your project. In fact, you can easily translate into other programming language.

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
126
127
128
129
130
131
132
133
134
135
136
137
<?php
  /*
    3D Vector Mathematics Library for PHP 
    https://HelloACM.com
  */
    
  public class Vector
  {
    private $x = 0;
    private $y = 0;
    private $z = 0;
    
    // Constructor
    public function Vector($x, $y, $z)
    {
      $this->x = $x;
      $this->y = $y;
      $this->z = $z;
    }
       
    // Set X
    public function setX($x)
    {
      $this->x = $x;
    }    
    
    // Set Y
    public function setY($y)
    {
      $this->y = $y;
    }
    
    // Set Z
    public function setZ($z)
    {
      $this->z = $z;
    }
    
    // Get X
    public function X()
    {
      return $this->x;
    }
    
    // Get Y
    public function Y()
    {
      return $this->y;
    }
    
    // Get Z
    public function Z()
    {
      return $this->z;
    }
    
    // Vector Add
    public function add($xx, $yy, $zz)
    {
      $this->x += $xx;
      $this->y += $yy;
      $this->z += $zz;
    }
     
    // Vector Sub
    public function sub($xx, $yy, $zz)
    {
      $this->x -= $xx;
      $this->y -= $yy;
      $this->z -= $zz;
    }
    
    // Vector Negative
    public function neg()
    {
      $this->x = -$this->x;
      $this->y = -$this->y;
      $this->z = -$this->z;
    }
    
    // Vector Scale
    public function scale($k)
    {
      $this->x *= $k;
      $this->y *= $k;
      $this->z *= $k;         
    }
    
    // Vector Dot Product
    public function dot($xx, $yy, $zz)
    {
      return ($this->x * $xx+
              $this->y * $yy+
              $this->z * $zz);
    }
    
    // Vector Length^2
    public function len2()
    {
      return ($this->x * $this->x +
              $this->y * $this->y +
              $this->z * $this->z);
    }
    
    // Vector Length
    public function len()
    {
      return (sqrt($this->len2()));
    }
    
    // Normalize Vector
    public function normalize()
    {
      $tmp = $this->len();
      if (abs($tmp) > 1e-7)
      {
        $this->x /= $tmp;
        $this->y /= $tmp;
        $this->z /= $tmp;
      }
      else
      {
        throw new Exception('len = 0');
      }
    }
    
    // Vector Cross Product
    public function cross($xx, $yy, $zz)
    {
      $cx = $this->y * $zz - $this->z * $yy;
      $cy = $this->z * $xx - $this->x * $zz;
      $cz = $this->x * $yy - $this->y * $xx;
      $this->x = $cx;
      $this->y = $cy;
      $this->z = $cz;
    }    
  } 
<?php
  /*
    3D Vector Mathematics Library for PHP 
    https://HelloACM.com
  */
	
  public class Vector
  {
    private $x = 0;
    private $y = 0;
    private $z = 0;
    
    // Constructor
    public function Vector($x, $y, $z)
    {
      $this->x = $x;
      $this->y = $y;
      $this->z = $z;
    }
       
    // Set X
    public function setX($x)
    {
      $this->x = $x;
    }    
    
    // Set Y
    public function setY($y)
    {
      $this->y = $y;
    }
    
    // Set Z
    public function setZ($z)
    {
      $this->z = $z;
    }
    
    // Get X
    public function X()
    {
      return $this->x;
    }
    
    // Get Y
    public function Y()
    {
      return $this->y;
    }
    
    // Get Z
    public function Z()
    {
      return $this->z;
    }
    
    // Vector Add
    public function add($xx, $yy, $zz)
    {
      $this->x += $xx;
      $this->y += $yy;
      $this->z += $zz;
    }
     
    // Vector Sub
    public function sub($xx, $yy, $zz)
    {
      $this->x -= $xx;
      $this->y -= $yy;
      $this->z -= $zz;
    }
    
    // Vector Negative
    public function neg()
    {
      $this->x = -$this->x;
      $this->y = -$this->y;
      $this->z = -$this->z;
    }
    
    // Vector Scale
    public function scale($k)
    {
      $this->x *= $k;
      $this->y *= $k;
      $this->z *= $k;         
    }
    
    // Vector Dot Product
    public function dot($xx, $yy, $zz)
    {
      return ($this->x * $xx+
              $this->y * $yy+
              $this->z * $zz);
    }
    
    // Vector Length^2
    public function len2()
    {
      return ($this->x * $this->x +
              $this->y * $this->y +
              $this->z * $this->z);
    }
    
    // Vector Length
    public function len()
    {
      return (sqrt($this->len2()));
    }
    
    // Normalize Vector
    public function normalize()
    {
      $tmp = $this->len();
      if (abs($tmp) > 1e-7)
      {
        $this->x /= $tmp;
        $this->y /= $tmp;
        $this->z /= $tmp;
      }
      else
      {
        throw new Exception('len = 0');
      }
    }
    
    // Vector Cross Product
    public function cross($xx, $yy, $zz)
    {
      $cx = $this->y * $zz - $this->z * $yy;
      $cy = $this->z * $xx - $this->x * $zz;
      $cz = $this->x * $yy - $this->y * $xx;
      $this->x = $cx;
      $this->y = $cy;
      $this->z = $cz;
    }    
  }	

To use this class, it is very handy:

1
2
3
$v1 = new Vector(1, 0, 1);
$v2 = new Vector(0, 0, 1);
echo $v1->add($v2->X(), $v2->Y(), $v2->Z()).len();
$v1 = new Vector(1, 0, 1);
$v2 = new Vector(0, 0, 1);
echo $v1->add($v2->X(), $v2->Y(), $v2->Z()).len();

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
454 words
Last Post: Simple Matrix Mathematics Library for PHP (Matrix Det)
Next Post: How to Convert GB2312 (or other Non-ANSI Characters) to UTF-8 encoding (Both MySQL and Files Charset)

The Permanent URL is: Simple PHP Vector (3D) class

Leave a Reply