Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ex02:
- class Vector
- {
- private $_x;
- private $_y;
- private $_z;
- private $_w = 0;
- static $verbose = false;
- public function __construct( array $kwargs)
- {
- if (isset($kwargs['dest']) && is_a($kwargs['dest'], "Vertex"))
- {
- if (isset($kwargs['orig']) && is_a($kwargs['orig'], "Vertex"))
- $orig = new Vertex(array('x' => $kwargs['orig']->getX(), 'y' => $kwargs['orig']->getY(), 'z' => $kwargs['orig']->getZ()));
- else
- $orig = new Vertex(array('x' => 0, 'y' => 0, 'z' => 0));
- $this->_x = $kwargs['dest']->getX() - $orig->getX();
- $this->_y = $kwargs['dest']->getY() - $orig->getY();
- $this->_z = $kwargs['dest']->getZ() - $orig->getZ();
- $this->_w = 0;
- }
- if (self::$verbose)
- printf("Vector( x:%0.2f, y:%0.2f, z:%0.2f, w:%0.2f ) constructed\n", $this->_x, $this->_y, $this->_z, $this->_w);
- }
- public function magnitude()
- {
- return (float)sqrt(($this->_x * $this->_x) + ($this->_y * $this->_y) + ($this->_z * $this->_z));
- }
- public function normalize()
- {
- $long = $this->magnitude();
- if ($long == 1) {
- return clone $this;
- }
- return new Vector(array('dest' => new Vertex(array('x' => $this->_x / $long, 'y' => $this->_y / $long, 'z' => $this->_z / $long))));
- }
- public function add(Vector $point)
- {
- return new Vector(array('dest' => new Vertex(array('x' => $this->_x + $point->_x, 'y' => $this->_y + $point->_y, 'z' => $this->_z + $point->_z))));
- }
- public function sub(Vector $point)
- {
- return new Vector(array('dest' => new Vertex(array('x' => $this->_x - $point->_x, 'y' => $this->_y - $point->_y, 'z' => $this->_z - $point->_z))));
- }
- public function opposite()
- {
- return new Vector(array('dest' => new Vertex(array('x' => $this->_x * -1, 'y' => $this->_y * -1, 'z' => $this->_z * -1))));
- }
- public function scalarProduct($p)
- {
- return new Vector(array('dest' => new Vertex(array('x' => $this->_x * $p, 'y' => $this->_y * $p, 'z' => $this->_z * $p))));
- }
- public function dotProduct(Vector $point)
- {
- return (float)(($this->_x * $point->_x) + ($this->_y * $point->_y) + ($this->_z * $point->_z));
- }
- public function crossProduct(Vector $point)
- {
- return new Vector(array('dest' => new Vertex(array(
- 'x' => $this->_y * $point->getZ() - $this->_z * $point->getY(),
- 'y' => $this->_z * $point->getX() - $this->_x * $point->getZ(),
- 'z' => $this->_x * $point->getY() - $this->_y * $point->getX()
- ))));
- }
- public function cos(Vector $point)
- {
- return ((($this->_x * $point->_x) + ($this->_y * $point->_y) + ($this->_z * $point->_z)) / sqrt((($this->_x * $this->_x) + ($this->_y * $this->_y) + ($this->_z * $this->_z)) * (($point->_x * $point->_x) + ($point->_y * $point->_y) + ($point->_z * $point->_z))));
- }
- function __destruct()
- {
- if (Self::$verbose)
- printf("Vector( x:%0.2f, y:%0.2f, z:%0.2f, w:%0.2f ) destructed\n", $this->_x, $this->_y, $this->_z, $this->_w);
- }
- function __toString()
- {
- return (vsprintf("Vector( x:%0.2f, y:%0.2f, z:%0.2f, w:%0.2f )", array($this->_x, $this->_y, $this->_z, $this->_w)));
- }
- public static function doc()
- {
- print(file_get_contents("Vector.doc.txt"));
- }
- public function getX()
- {
- return $this->_x;
- }
- public function getY()
- {
- return $this->_y;
- }
- public function getZ()
- {
- return $this->_z;
- }
- public function getW()
- {
- return $this->_w;
- }
- }
- <- Vector ----------------------------------------------------------------------
- The Vector class handles space 3D Vectors.
- More than three coordinates, it has got an homogenous parameter (always=0)
- An instance is contructed from two Vertex orig and dest.
- It represents then the vector between thos two points.
- new Vector( array( 'dest' => #instanceOfVertex, 'orig' => #instanceOfVertex );
- Ff the vertex 'orig' is not precised, it will then be (0,0,0) as default.
- All Attributes are private, you may want to use getters.
- When a Vector is created, you wont be able to change its values after.
- aka-> no setters;
- The following method are providing for this Class:
- - magnitude() : give the norme of the instanced Vector.
- - normalize() : give the normalized Vector correspondig (a fresh copy
- if the instance was already normalized)
- - add( Vector $v ) : give the result of the addition between
- the current instance and $.
- - sub( Vector $v ) : give the result of the substraction between
- the current instance and $.
- - opposite() : return the opposite Vector;
- - scalarProduct( $k ) : return the product of the constant $k
- and the current instance;
- - dotProduct( Vector $v ) : return the scalar product of the current
- instance and $v.
- - cos( Vector $v ) : return the cosinus between the current instance and $v
- Vector crossProduct( Vector $rhs ) : return the cross product
- (right handed set) of the current instance and $v
- ---------------------------------------------------------------------- Vector ->
- ex03:
- class Matrix
- {
- const IDENTITY = "IDENTITY";
- const SCALE = "SCALE";
- const RX = "Ox ROTATION";
- const RY = "Oy ROTATION";
- const RZ = "Oz ROTATION";
- const TRANSLATION = "TRANSLATION";
- const PROJECTION = "PROJECTION";
- protected $matrix = array();
- private $_preset;
- private $_scale;
- private $_angle;
- private $_vtc;
- private $_fov;
- private $_ratio;
- private $_near;
- private $_far;
- static $verbose = false;
- public function __construct( array $kwargs = null)
- {
- if (isset($kwargs))
- {
- if (isset($kwargs['preset']))
- $this->_preset = $kwargs['preset'];
- if (isset($kwargs['scale']))
- $this->_scale = $kwargs['scale'];
- if (isset($kwargs['angle']))
- $this->_angle = $kwargs['angle'];
- if (isset($kwargs['vtc']))
- $this->_vtc = $kwargs['vtc'];
- if (isset($kwargs['fov']))
- $this->_fov = $kwargs['fov'];
- if (isset($kwargs['ratio']))
- $this->_ratio = $kwargs['ratio'];
- if (isset($kwargs['near']))
- $this->_near = $kwargs['near'];
- if (isset($kwargs['far']))
- $this->_far = $kwargs['far'];
- $this->check();
- $this->createMatrix();
- if (self::$verbose) {
- if ($this->_preset == self::IDENTITY)
- echo "Matrix " . $this->_preset . " instance constructed\n";
- else
- echo "Matrix " . $this->_preset . " preset instance constructed\n";
- }
- $this->dispatch();
- }
- }
- private function dispatch()
- {
- switch ($this->_preset) {
- case (self::IDENTITY) :
- $this->identity(1);
- break;
- case (self::TRANSLATION) :
- $this->translation();
- break;
- case (self::SCALE) :
- $this->identity($this->_scale);
- break;
- case (self::RX) :
- $this->rotation_x();
- break;
- case (self::RY) :
- $this->rotation_y();
- break;
- case (self::RZ) :
- $this->rotation_z();
- break;
- case (self::PROJECTION) :
- $this->projection();
- break;
- }
- }
- private function createMatrix()
- {
- for ($i = 0; $i < 16; $i++) {
- $this->matrix[$i] = 0;
- }
- }
- private function identity($scale)
- {
- $this->matrix[0] = $scale;
- $this->matrix[5] = $scale;
- $this->matrix[10] = $scale;
- $this->matrix[15] = 1;
- }
- private function translation()
- {
- $this->identity(1);
- $this->matrix[3] = $this->_vtc->getX();
- $this->matrix[7] = $this->_vtc->getY();
- $this->matrix[11] = $this->_vtc->getZ();
- }
- private function rotation_x()
- {
- $this->identity(1);
- $this->matrix[0] = 1;
- $this->matrix[5] = cos($this->_angle);
- $this->matrix[6] = -sin($this->_angle);
- $this->matrix[9] = sin($this->_angle);
- $this->matrix[10] = cos($this->_angle);
- }
- private function rotation_y()
- {
- $this->identity(1);
- $this->matrix[0] = cos($this->_angle);
- $this->matrix[2] = sin($this->_angle);
- $this->matrix[5] = 1;
- $this->matrix[8] = -sin($this->_angle);
- $this->matrix[10] = cos($this->_angle);
- }
- private function rotation_z()
- {
- $this->identity(1);
- $this->matrix[0] = cos($this->_angle);
- $this->matrix[1] = -sin($this->_angle);
- $this->matrix[4] = sin($this->_angle);
- $this->matrix[5] = cos($this->_angle);
- $this->matrix[10] = 1;
- }
- private function projection()
- {
- $this->identity(1);
- $this->matrix[5] = 1 / tan(0.5 * deg2rad($this->_fov));
- $this->matrix[0] = $this->matrix[5] / $this->_ratio;
- $this->matrix[10] = -1 * (-$this->_near - $this->_far) / ($this->_near - $this->_far);
- $this->matrix[14] = -1;
- $this->matrix[11] = (2 * $this->_near * $this->_far) / ($this->_near - $this->_far);
- $this->matrix[15] = 0;
- }
- private function check()
- {
- if (empty($this->_preset))
- return "error";
- if ($this->_preset == self::SCALE && empty($this->_scale))
- return "error";
- if (($this->_preset == self::RX || $this->_preset == self::RY || $this->_preset == self::RZ) && empty($this->_angle))
- return "error";
- if ($this->_preset == self::TRANSLATION && empty($this->_vtc))
- return "error";
- if ($this->_preset == self::PROJECTION && (empty($this->_fov) || empty($this->_radio) || empty($this->_near) || empty($this->_far)))
- return "error";
- }
- public function mult(Matrix $mat)
- {
- $tmp = array();
- for ($i = 0; $i < 16; $i += 4) {
- for ($j = 0; $j < 4; $j++) {
- $tmp[$i + $j] = 0;
- $tmp[$i + $j] += $this->matrix[$i + 0] * $mat->matrix[$j + 0];
- $tmp[$i + $j] += $this->matrix[$i + 1] * $mat->matrix[$j + 4];
- $tmp[$i + $j] += $this->matrix[$i + 2] * $mat->matrix[$j + 8];
- $tmp[$i + $j] += $this->matrix[$i + 3] * $mat->matrix[$j + 12];
- }
- }
- $matrice = new Matrix();
- $matrice->matrix = $tmp;
- return $matrice;
- }
- public function transformVertex(Vertex $vec)
- {
- $tmp = array();
- $tmp['x'] = ($vec->getX() * $this->matrix[0]) + ($vec->getY() * $this->matrix[1]) + ($vec->getZ() * $this->matrix[2]) + ($vec->getW() * $this->matrix[3]);
- $tmp['y'] = ($vec->getX() * $this->matrix[4]) + ($vec->getY() * $this->matrix[5]) + ($vec->getZ() * $this->matrix[6]) + ($vec->getW() * $this->matrix[7]);
- $tmp['z'] = ($vec->getX() * $this->matrix[8]) + ($vec->getY() * $this->matrix[9]) + ($vec->getZ() * $this->matrix[10]) + ($vec->getW() * $this->matrix[11]);
- $tmp['w'] = ($vec->getX() * $this->matrix[11]) + ($vec->getY() * $this->matrix[13]) + ($vec->getZ() * $this->matrix[14]) + ($vec->getW() * $this->matrix[15]);
- $tmp['color'] = $vec->getColor();
- $vertex = new Vertex($tmp);
- return $vertex;
- }
- function __destruct()
- {
- if (self::$verbose)
- printf("Matrix instance destructed\n");
- }
- function __toString()
- {
- $tmp = "M | vtcX | vtcY | vtcZ | vtxO\n";
- $tmp .= "-----------------------------\n";
- $tmp .= "x | %0.2f | %0.2f | %0.2f | %0.2f\n";
- $tmp .= "y | %0.2f | %0.2f | %0.2f | %0.2f\n";
- $tmp .= "z | %0.2f | %0.2f | %0.2f | %0.2f\n";
- $tmp .= "w | %0.2f | %0.2f | %0.2f | %0.2f";
- return (vsprintf($tmp, array($this->matrix[0], $this->matrix[1], $this->matrix[2], $this->matrix[3], $this->matrix[4], $this->matrix[5], $this->matrix[6], $this->matrix[7], $this->matrix[8], $this->matrix[9], $this->matrix[10], $this->matrix[11], $this->matrix[12], $this->matrix[13], $this->matrix[14], $this->matrix[15])));
- }
- public static function doc()
- {
- print(file_get_contents("Matrix.doc.txt"));
- }
- }
- <- Matrix ----------------------------------------------------------------------
- The Matrix class handles 4x4 Matrixs.
- it's got a finite constante values for the preset :
- IDENTITY = 'IDENTITY',
- SCALE = 'SCALE',
- RX = 'Ox ROTATION',
- RY = 'Oy ROTATION',
- RZ = 'Oz ROTATION',
- TRANSLATION = 'TRANSLATION',
- PROJECTION = 'PROJECTION';
- On wich depends the following arguments respectivly :
- IDENTITY ->nothing
- SCALE -> the scale
- RX -> angle of rotation
- RY -> angle of rotation
- RZ -> angle of rotation
- TRANSLATION -> vector ( class Vector ) of the translation
- PROJECTION -> Field of view ( in degrees ), Ratio (width/height of screen), near and far z distances
- When a Matrix is created, you wont be able to change its values after.
- aka-> no setters;
- The following method are providing for this Class:
- - mult() : return a new matrix corresponding to the multiplication of the instance by the argument
- - transformVertex() : return a new Vertex corresponding to the matrix apply to the one sent in argument
- ---------------------------------------------------------------------- Matrix ->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement