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.

<?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:

$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) —

437 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 (AMP Version)

Leave a Reply