Wednesday, 25 September 2019

A Simple 3D Vector Class in PHP

PHP 

3D vector calculation is quite common in computer applications such as games, simulators and scientific visualization. The following PHP class, SimpleVector3D, implements the basic functions of the 3D vector.

<?php

  namespace CodeBilby;
  class SimpleVector3DException extends \Exception {};

  class SimpleVector3D
  {
      private $a;

      public function __construct(Array $a)
      {
          if (count($a) !== 3) 
              throw new SimpleVector3DException('The vector must have three elements');

          $this->a = $a;
      }

      public function X()
      {
          return $this->a[0];
      }

      public function Y()
      {
          return $this->a[1];
      }

      public function Z()
      {
          return $this->a[2];
      }

      public function add($b)
      {

          $result = array_fill(0, 3, 0);

          $result[0] = $this->a[0] + $b->a[0];
          $result[1] = $this->a[1] + $b->a[1];        
          $result[2] = $this->a[2] + $b->a[2];

          return new SimpleVector3D($result);
      }

      public function toString()
      {
          return '[' . implode(', ', $this->a) . ']';
      }

      public function sub(SimpleVector3D $b)
      {        
          $result = array_fill(0, 3, 0);

          $result[0] = $this->a[0] - $b->a[0];
          $result[1] = $this->a[1] - $b->a[1];        
          $result[2] = $this->a[2] - $b->a[2];

          return new SimpleVector3D($result);
      }

      public function neg()
      {
          $result = array_fill(0, 3, 0);

          $result[0] = -$this->a[0];
          $result[1] = -$this->a[1];        
          $result[2] = -$this->a[2];

          return new SimpleVector3D($result);
      }

      public function scale($s)
      {
          $result = array_fill(0, 3, 0);

          $result[0] = $this->a[0] * $s;
          $result[1] = $this->a[1] * $s;        
          $result[2] = $this->a[2] * $s;

          return new SimpleVector3D($result);
      }    

      public function dot($b)
      {        
          $result = 0;

          for ($i = 0; $i < 3; $i++) {      
              $result += $this->a[$i] * $b->a[$i];
          }

          return $result;
      }

      public function cross($b)
      {         
          $result = array_fill(0, 3, 0);

          $result[0] = $this->a[1] * $b->a[2] - $this->a[2] * $b->a[1];
          $result[1] = $this->a[2] * $b->a[0] - $this->a[0] * $b->a[2];      
          $result[2] = $this->a[0] * $b->a[1] - $this->a[1] * $b->a[0];

          return new SimpleVector3D($result);    
      }

      public function len()
      {
          return sqrt($this->a[0] * $this->a[0] + $this->a[1] * $this->a[1] + $this->a[2] * $this->a[2]);
      }

      public function normalize()
      {
          $len = $this->len();

          if ($len <= 1e-7)
                  throw new SimpleVector3DException('The length of the vector is zero');

          $result = array_fill(0, 3, 0);

          $result[0] = $this->a[0] / $len;
          $result[1] = $this->a[1] / $len;
          $result[2] = $this->a[2] / $len;

          return new SimpleVector3D($result);
      }
  }
?>

It is very simple to use. Copy all the codes above and paste them into a new file: SimpleVector3D.php. We can call the functions as below:


require_once 'SimpleVector3D.php';

use CodeBilby\SimpleVector3D;

$p1 = new SimpleVector3D([10, 50, 23]);
$p2 = new SimpleVector3D([190, 18, 31]);

//addition
$v = $p1->add($p2);
echo $v->toString();


Search