|
Key
This line was removed.
This word was removed. This word was added.
This line was added.
|
Comment:
Changes (2)
View Page History{zone-data:component-name}
Zend_Math
{zone-data}
{zone-data:proposer-list}
[~walter.tamboer]
{zone-data}
{zone-data:liaison}
TBD
[~freak]
{zone-data}
1.0 - 29 May 2011: Initial Draft.
{zone-data}
{zone-data:overview}
Zend_Math is a component which contains math related functions and classes. The purpose of this component is to make life easier for people who are doing vector and matrix math for example.
{zone-data}
{zone-data:references}
* http://en.wikipedia.org/wiki/Mathematics
* http://en.wikipedia.org/wiki/Euclidean_vector
* http://en.wikipedia.org/wiki/Matrix_(mathematics)
* http://www.math.com
{zone-data}
{zone-data:requirements}
* This component *will* support helper methods such as "clamp" and "lerp".
* This component *will* support vector classes to do vector math.
* This component *will* support matrix classes to do matrix math.
* This component *will* support functionality to detect intersections.
{zone-data}
{zone-data:dependencies}
* Zend_Exception
* SPL Iterator
* SPL Exceptions
{zone-data}
{zone-data:operation}
This component can be used as is. It can be used to do calculations.
{zone-data}
{zone-data:milestones}
* Milestone 1: Setup the proposal and consider feedback.
* Milestone 2: Write and finish component source code.
* Milestone 3: Write unit tests
* Milestone 4: Write documentation
{zone-data}
{zone-data:class-list}
* Zend_Math
* Zend_Math_BoundingBox
* Zend_Math_BoundingFrustum
* Zend_Math_BoundingSphere
* Zend_Math_Exception
* Zend_Math_Exception_DivideByZero
* Zend_Math_Interface
* Zend_Math_Matrix
* Zend_Math_Matrix_Matrix44
* Zend_Math_Plane
* Zend_Math_Point
* Zend_Math_Quaternion
* Zend_Math_Ray
* Zend_Math_Rectangle
* Zend_Math_Size
* Zend_Math_Vector
* Zend_Math_Vector_Vector2
* Zend_Math_Vector_Vector3
* Zend_Math_Vector_Vector4
{zone-data}
{zone-data:use-cases}
||UC-01||
{code}
<?php
// Calculate the cross product of two vectors:
$locationOne = Zend_Math_Vector_Vector3::getUnitY();
$locationTwo = new Zend_Math_Vector_Vector3( -1.0, 0.0, 0.0 );
$crossProduct = $locationOne->cross( $locationTwo );
{code}
||UC-02||
{code}
<?php
// Create a translation, rotation and scaling matrix:
$translation = Zend_Math_Matrix_Matrix44::createTranslation( 12.34, 56.78, 90 );
$rotation = Zend_Math_Matrix_Matrix44::createRotationX( Zend_Math::degToRad( 90 ) );
$scale = Zend_Math_Matrix_Matrix44::createScale( 1, 1, 1 );
// Create a transformation matrix:
$transform = $scale->mul( $rotation )->mul( $translation );
{code}
{zone-data}
{zone-data:skeletons}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math
{
/**
* The value PI.
* @const
*/
const PI = 3.1415926535897932384626433832795028841971693993751;
/**
* Two times the value of PI.
* @const
*/
const PI2 = 6.283185307179586476925286766559;
/**
* The half of PI.
* @const
*/
const HALF_PI = 1.5707963267948966192313216916398;
/**
* The reciprocal value of PI.
* @const
*/
const RECIPROCAL_PI = 0.31830988618379067153776752674503;
/**
* The factor to convert degrees to radians.
* @const
*/
const DEG_TO_RAD = 0.0174532925;
/**
* The factor to convert radians to degrees.
* @const
*/
const RAD_TO_DEG = 57.2957795;
/**
* Converts the given radians to degrees.
* @param float $value The value to convert.
* @return float Returns the converted value.
*/
public static function degToRad( $value )
{
return $value * self::DEG_TO_RAD;
}
/**
* Converts the given degrees to radians.
* @param float $value The value to convert.
* @return float Returns the converted value.
*/
public static function radToDeg( $value )
{
return $value * self::RAD_TO_DEG;
}
/**
* Clamps the given value between the given minimum and maximum.
* @param mixed $value The value that should be clamped.
* @param mixed $min The minimum value.
* @param mixed $max The maximum value.
* @return mixed Returns the clamped value.
*/
public static function clamp( $value, $min, $max )
{
return max( $min, min( $value, $max ) );
}
/**
* Lineair interpolates the two values.
* @param mixed $from The value to start from.
* @param mixed $to The value to interpolate to.
* @param mixed $amount The amount of interpolation to apply.
* @return mixed Returns the interpolated value.
*/
public static function lerp( $from, $to, $amount )
{
return $from + ( $to - $from ) * $amount;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_BoundingBox
{
/**
* Checks if this bounding box contains the given value.
* @param mixed $value The value to check for.
* @return bool Returns true when this bounding box contains the given value; false otherwise.
*/
public function contains( $value )
{
$result = false;
if ( $value instanceof Zend_Math_BoundingBox )
{
}
else if ( $value instanceof Zend_Math_BoundingFrustum )
{
}
else if ( $value instanceof Zend_Math_BoundingSphere )
{
}
else if ( $value instanceof Zend_Math_Vector_Vector3 )
{
}
return $result;
}
/**
* Checks if this bounding box intersects with the given value.
* @param mixed $value The value to check for.
* @return bool Returns true when this bounding box intersects with the given value; false otherwise.
*/
public function intersects( $value )
{
$result = false;
if ( $value instanceof Zend_Math_BoundingBox )
{
}
else if ( $value instanceof Zend_Math_BoundingFrustum )
{
}
else if ( $value instanceof Zend_Math_BoundingSphere )
{
}
else if ( $value instanceof Zend_Math_Vector_Vector3 )
{
}
return $result;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_BoundingFrustum
{
/**
* Checks if this bounding frustum contains the given value.
* @param mixed $value The value to check for.
* @return bool Returns true when this bounding frustum contains the given value; false otherwise.
*/
public function contains( $value )
{
$result = false;
if ( $value instanceof Zend_Math_BoundingBox )
{
}
else if ( $value instanceof Zend_Math_BoundingFrustum )
{
}
else if ( $value instanceof Zend_Math_BoundingSphere )
{
}
else if ( $value instanceof Zend_Math_Vector_Vector3 )
{
}
return $result;
}
/**
* Checks if this bounding frustum intersects with the given value.
* @param mixed $value The value to check for.
* @return bool Returns true when this bounding frustum intersects with the given value; false otherwise.
*/
public function intersects( $value )
{
$result = false;
if ( $value instanceof Zend_Math_BoundingBox )
{
}
else if ( $value instanceof Zend_Math_BoundingFrustum )
{
}
else if ( $value instanceof Zend_Math_BoundingSphere )
{
}
else if ( $value instanceof Zend_Math_Vector_Vector3 )
{
}
return $result;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_BoundingSphere
{
/**
* Checks if this bounding sphere contains the given value.
* @param mixed $value The value to check for.
* @return bool Returns true when this bounding sphere contains the given value; false otherwise.
*/
public function contains( $value )
{
$result = false;
if ( $value instanceof Zend_Math_BoundingBox )
{
}
else if ( $value instanceof Zend_Math_BoundingFrustum )
{
}
else if ( $value instanceof Zend_Math_BoundingSphere )
{
}
else if ( $value instanceof Zend_Math_Vector_Vector3 )
{
}
return $result;
}
/**
* Checks if this bounding sphere intersects with the given value.
* @param mixed $value The value to check for.
* @return bool Returns true when this bounding sphere intersects with the given value; false otherwise.
*/
public function intersects( $value )
{
$result = false;
if ( $value instanceof Zend_Math_BoundingBox )
{
}
else if ( $value instanceof Zend_Math_BoundingFrustum )
{
}
else if ( $value instanceof Zend_Math_BoundingSphere )
{
}
else if ( $value instanceof Zend_Math_Vector_Vector3 )
{
}
return $result;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Exception extends Zend_Exception
{
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Exception_DivideByZero extends Zend_Math_Exception
{
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface Zend_Math_Interface
{
/**
* Adds the given value to this value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function add( $value );
/**
* Subtracts the given value from this value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function sub( $value );
/**
* Multiplies this value with the given value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function mul( $value );
/**
* Divides this value by the given value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function div( $value );
/**
* Converts this value to a readable string.
* @return string
*/
public function __toString();
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Matrix implements Zend_Math_Interface, Iterator
{
/**
* The position of the iterator.
* @var int
*/
private $_position = 0;
/**
* The amount of components that this matrix has.
* @var array
*/
protected $_components = array();
/**
* The amount of rows that this matrix has.
* @var int
*/
protected $_rows = 0;
/**
* The amount of columns that this matrix has.
* @var int
*/
protected $_columns = 0;
/**
* Initializes a new instance of this class.
* @param int $rows The amount of rows that this matrix has.
* @param int $columns The amount of columns that this matrix has.
*/
public function __construct( $rows, $columns )
{
$this->_rows = $rows;
$this->_columns = $columns;
$this->makeIdentity();
}
/**
* Gets the amount of rows that this matrix has.
* @return int
*/
public function getRows()
{
return $this->_rows;
}
/**
* Gets the amount of columns that this matrix has.
* @return int
*/
public function getColumns()
{
return $this->_columns;
}
/**
* Gets the given component.
* @return float
*/
public function get( $row, $column )
{
if ( $row >= $this->_rows )
{
throw new OutOfBoundsException( 'Trying to get row ' . ( $row + 1 ) . ' while this matrix only has ' . $this->_rows . ' rows.' );
}
if ( $column >= $this->_columns )
{
throw new OutOfBoundsException( 'Trying to get column ' . ( $column + 1 ) . ' while this matrix only has ' . $this->column . ' columns.' );
}
return $this->_components[ $row * $this->_rows + $column ];
}
/**
* Sets the value to the given component.
* @param int $row The row to set.
* @param int $column The column to set.
* @param float $value The value to set.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function set( $row, $column, $value )
{
if ( $row >= $this->_rows )
{
throw new OutOfBoundsException( 'Trying to set row ' . ( $row + 1 ) . ' while this matrix only has ' . $this->_rows . ' rows.' );
}
if ( $column >= $this->_columns )
{
throw new OutOfBoundsException( 'Trying to set column ' . ( $column + 1 ) . ' while this matrix only has ' . $this->_columns . ' columns.' );
}
$this->_components[ $row * $this->_rows + $column ] = $value;
return $this;
}
/**
* Adds the given value to this value.
* @param $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function add( $value )
{
if ( $value instanceof Zend_Math_Matrix )
{
if ( $value->getRows() >= $this->_rows || $value->getColumns() >= $this->_columns )
{
throw new OutOfBoundsException( 'Matrices only support the addition of other matrices with the same rows and columns.' );
}
}
else if ( is_int( $value ) || is_float( $value ) )
{
foreach ( $this->_components as $component => $componentValue )
{
$this->_components[ $component ] += $value;
}
}
else
{
throw new InvalidArgumentException( 'Matrices only supports the addition of matrices, floats and integers.' );
}
return $this;
}
/**
* Subtracts the given value from this value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function sub( $value )
{
if ( $value instanceof Zend_Math_Matrix )
{
if ( $value->getRows() >= $this->_rows || $value->getColumns() >= $this->_columns )
{
throw new OutOfBoundsException( 'Matrices only support the subtraction of other matrices with the same rows and columns.' );
}
}
else if ( is_int( $value ) || is_float( $value ) )
{
foreach ( $this->_components as $component => $componentValue )
{
$this->_components[ $component ] -= $value;
}
}
else
{
throw new InvalidArgumentException( 'Matrices only supports the subtraction of matrices, floats and integers.' );
}
return $this;
}
/**
* Multiplies this value with the given value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function mul( $value )
{
if ( $value instanceof Zend_Math_Matrix )
{
}
else if ( is_int( $value ) || is_float( $value ) )
{
foreach ( $this->_components as $component => $componentValue )
{
$this->_components[ $component ] *= $value;
}
}
else
{
throw new InvalidArgumentException( 'Matrices only supports the multiplication of matrices, floats and integers.' );
}
return $this;
}
/**
* Divides this value by the given value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function div( $value )
{
if ( $value instanceof Zend_Math_Matrix )
{
}
else if ( is_int( $value ) || is_float( $value ) )
{
if ( !$value )
{
throw new Zend_Math_Exception_DivideByZero( 'A value cannot be divided by zero.' );
}
foreach ( $this->_components as $component => $componentValue )
{
$this->_components[ $component ] /= $value;
}
}
else
{
throw new InvalidArgumentException( 'Matrices only supports the division of matrices, floats and integers.' );
}
return $this;
}
/**
* Makes an identity matrix of this matrix.
* @return Zend_Math_Matrix Returns the instance of this class.
*/
public function makeIdentity()
{
$this->_components = array_fill( 0, $this->_rows * $this->_columns, 0.0 );
for ( $r = 0; $r < $this->_rows; ++$r )
{
$index = $r * $this->_rows + $r;
$this->_components[ $index ] = 1;
}
return $this;
}
/**
* Calculates the determinant of this matrix.
* @return float Returns the determinant of this matrix.
*/
public function determinant()
{
if ( $this->_rows != $this->_columns )
{
throw new RuntimeException( 'The determinant of a matrix can only be calculated when the matrix is a square matrix.' );
}
$result = 0.0;
if ( $this->_rows == 1 )
{
$result = $this->_components[ 0 ];
}
else
{
for ( $i = 0; $i < $this->_columns; $i++ )
{
$minor = $this->calculateMinor( $this, 0, $i );
if ( $minor )
{
$result += ( pow( -1, $i ) * $this->_components[ $i ] * $minor->determinant() );
}
}
}
return $result;
}
/**
* Calculate the 'minor' of the given matrix at given position. The minor is the matrix formed by deleting the specified row and column from the matrix.
* @param Zend_Math_Matrix $matrix The matrix to work with.
* @param int $row The row to remove.
* @param int $column The column to remove.
* @return Zend_Math_Matrix Returns the matrix with the given row and column removed or null when no minor could be calculated.
*/
public function calculateMinor( Zend_Math_Matrix $matrix, $row, $column )
{
$result = null;
if ( $matrix->getRows() > 0 && $row < $matrix->getRows() && $column < $matrix->getColumns() )
{
$result = new Zend_Math_Matrix( $matrix->getRows() - 1, $matrix->getColumns() - 1 );
$a = $b = 0;
for ( $i = 0; $i < $matrix->getRows(); $i++ )
{
if( $i != $row )
{
$b = 0;
for ( $j = 0; $j < $matrix->getColumns(); $j++ )
{
if( $j != $column )
{
$result->set( $a, $b, $matrix->get( $i, $j ) );
$b++; // Increase column-count of minor
}
}
$a++; // Increase row-count of minor
}
}
}
return $result;
}
/**
* Transposes the rows and columns of this matrix and returns a new matrix.
* @return Zend_Math_Matrix Returns a new instance of this class containing the transposed matrix.
*/
public function transpose()
{
$result = clone $this;
for ( $r = 0; $r < $this->_rows; ++$r )
{
for ( $c = 0; $c < $this->_columns; ++$c )
{
$result->set( $c, $r, $this->get( $r, $c ) );
}
}
return $result;
}
/**
* Gets the current value from the iterator.
* @return mixed Returns the current value of the iterator.
*/
public function current()
{
return $this->_components[ $this->_position ];
}
/**
* Gets the current key from the iterator.
* @return int Returns the current key of the iterator.
*/
public function key()
{
return $this->_position;
}
/**
* Increases the iterator's position.
* @return void
*/
public function next()
{
++$this->_position;
}
/**
* Rewinds the iterator's position.
* @return void
*/
public function rewind()
{
$this->_position = 0;
}
/**
* Checks if the iterator is still valid.
* @return boolean Returns true when the iterator is valid; false otherwise.
*/
public function valid()
{
return $this->_position < $this->getDimension();
}
/**
* Converts this value to a readable string.
* @return string
*/
public function __toString()
{
return implode( ',', $this->_components );
}
}
{code}
{code}
<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Matrix_Matrix44 extends Zend_Math_Matrix
{
/**
* Initializes a new instance of this class.
*/
public function __construct()
{
parent::__construct( 4, 4 );
}
/**
* Creates a scale matrix.
*
* @param mixed $X The X-position to set. This can be a number or a vector.
* @param mixed $Y The Y-position to set. This should be a number or empty when $X is a vector.
* @param mixed $Z The Z-position to set. This should be a number or empty when $X is a vector.
* @return Zend_Math_Matrix Returns a new instance of this class.
*/
public static function createScale( $X = 0.0, $Y = 0.0, $Z = 0.0 )
{
$result = new self();
if ( is_float( $X ) || is_int( $X ) )
{
$result->set( 0, 0, $X );
$result->set( 1, 1, $Y );
$result->set( 2, 2, $Z );
}
else if ( $X instanceof Zend_Math_Vector_Vector3 || $X instanceof Zend_Math_Vector_Vector4 )
{
$result->set( 0, 0, $X->getX() );
$result->set( 1, 1, $X->getY() );
$result->set( 2, 2, $X->getZ() );
}
return $result;
}
/**
* Creates a translation matrix.
*
* @param mixed $X The X-position to set. This can be a number or a vector.
* @param mixed $Y The Y-position to set. This should be a number or empty when $X is a vector.
* @param mixed $Z The Z-position to set. This should be a number or empty when $X is a vector.
* @return Zend_Math_Matrix Returns a new instance of this class.
*/
public static function createTranslation( $X = 0.0, $Y = 0.0, $Z = 0.0 )
{
$result = new self();
if ( is_float( $X ) || is_int( $X ) )
{
$result->set( 0, 3, $X );
$result->set( 1, 3, $Y );
$result->set( 2, 3, $Z );
}
else if ( $X instanceof Zend_Math_Vector_Vector3 || $X instanceof Zend_Math_Vector_Vector4 )
{
$result->set( 0, 3, $X->getX() );
$result->set( 1, 3, $X->getY() );
$result->set( 2, 3, $X->getZ() );
}
return $result;
}
/**
* Creates a rotation matrix around the X-axis.
*
* @param float $radians The radians to set.
* @return Zend_Math_Matrix Returns a new instance of this class.
*/
public static function createRotationX( $radians = 0.0 )
{
$result = new self();
// @todo
return $result;
}
/**
* Creates a rotation matrix around the Y-axis.
*
* @param float $radians The radians to set.
* @return Zend_Math_Matrix Returns a new instance of this class.
*/
public static function createRotationY( $radians = 0.0 )
{
$result = new self();
// @todo
return $result;
}
/**
* Creates a rotation matrix around the Z-axis.
*
* @param float $radians The radians to set.
* @return Zend_Math_Matrix Returns a new instance of this class.
*/
public static function createRotationZ( $radians = 0.0 )
{
$result = new self();
// @todo
return $result;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Plane
{
/**
* The distance of the plane.
* @var float
*/
private $_distance;
/**
* The normal vector of the plane.
* @var Zend_Math_Vector_Vector3
*/
private $_normal;
/**
* Initializes a new instance of this class.
* @param $distance The distance of this plane.
* @param $normal The normal of this plane.
*/
public function __construct( $distance, Zend_Math_Vector_Vector3 $normal )
{
$this->_distance = (float)$distance;
$this->_normal = $normal;
}
/**
* Gets the distance of this plane related to the math origin.
* @return float Returns the distance of this plane from the math origin.
*/
public function getDistance()
{
return $this->_distance;
}
/**
* Gets the normal of this plane.
* @return Zend_Math_Vector_Vector3 Returns a Zend_Math_Vector_Vector3 which represents the normal of this plane.
*/
public function getNormal()
{
return $this->_normal;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Point
{
/**
* The X-position of the point.
* @var float
*/
private $_x;
/**
* The Y-position of the point.
* @var float
*/
private $_y;
/**
* Initializes a new instance of this class.
* @param $x The X-position of this point.
* @param $y The Y-position of this point.
*/
public function __construct( $x, $y )
{
$this->_x = $x;
$this->_y = $y;
}
/**
* Gets the X-position of this point.
* @return float Returns a float representing the X-position of this point.
*/
public function getX()
{
return $this->_x;
}
/**
* Gets the Y-position of this point.
* @return float Returns a float representing the Y-position of this point.
*/
public function getY()
{
return $this->_y;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Quaternion
{
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Ray
{
/**
* The direction of this ray.
* @var Zend_Math_Vector_Vector3
*/
private $_direction;
/**
* The position of this ray.
* @var Zend_Math_Vector_Vector3
*/
private $_position;
/**
* Initializes a new instance of this class.
* @param Zend_Math_Vector_Vector3 $position The position of the ray.
* @param Zend_Math_Vector_Vector3 $direction The direction of the ray.
*/
public function __construct( Zend_Math_Vector_Vector3 $position, Zend_Math_Vector_Vector3 $direction )
{
$this->_position = $position;
$this->_direction = $direction;
}
/**
* Gets the direction of this ray.
* @return Zend_Math_Vector_Vector3 Returns a Zend_Math_Vector_Vector3 which represents the direction.
*/
public function getDirection()
{
return $this->_direction;
}
/**
* Gets the position of this ray.
* @return Zend_Math_Vector_Vector3 Returns a Zend_Math_Vector_Vector3 which represents the position.
*/
public function getPosition()
{
return $this->_position;
}
/**
* Checks if this ray intersects with the given value.
* @param mixed $value The value to calculate with.
* @return bool Returns true when there is an intersection; false otherwise.
*/
public function intersects( $value )
{
$result = false;
if ( $value instanceof Zend_Math_BoundingBox )
{
// @todo
}
else if ( $value instanceof Zend_Math_BoundingFrustum )
{
// @todo
}
else if ( $value instanceof Zend_Math_BoundingSphere )
{
// @todo
}
else if ( $value instanceof Zend_Math_Plane )
{
// @todo
}
else
{
throw new InvalidArgumentException( 'Invalid argument provided for intersects method.' );
}
return $result;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Rectangle
{
/**
* The X-position of the rectangle.
* @var float
*/
private $_x;
/**
* The Y-position of the rectangle.
* @var float
*/
private $_y;
/**
* The width of the rectangle.
* @var float
*/
private $_width;
/**
* The height of the rectangle.
* @var float
*/
private $_height;
/**
* Initializes a new instance of this class.
* @param $x The X-position of this rectangle.
* @param $y The Y-position of this rectangle.
* @param $width The width of this rectangle.
* @param $height The height of this rectangle.
*/
public function __construct( $x, $y, $width, $height )
{
$this->_x = $x;
$this->_y = $y;
$this->_width = $width;
$this->_height = $height;
}
/**
* Gets the X-position of this rectangle.
* @return float Returns a float representing the X-position of this rectangle.
*/
public function getX()
{
return $this->_x;
}
/**
* Gets the Y-position of this rectangle.
* @return float Returns a float representing the Y-position of this rectangle.
*/
public function getY()
{
return $this->_y;
}
/**
* Gets the width of this rectangle.
* @return float Returns the width of this rectangle.
*/
public function getWidth()
{
return $this->_width;
}
/**
* Gets the height of this rectangle.
* @return float Returns the height of this rectangle.
*/
public function getHeight()
{
return $this->_height;
}
/**
* Gets the X-position of the left of this rectangle.
* @return float Returns a float representing the X-position of the left of this rectangle.
*/
public function getLeft()
{
return $this->_x;
}
/**
* Gets the Y-position of the top of this rectangle.
* @return float Returns a float representing the Y-position of the top of this rectangle.
*/
public function getTop()
{
return $this->_y;
}
/**
* Gets the X-position of the right of this rectangle.
* @return float Returns a float representing the X-position of the right of this rectangle.
*/
public function getRight()
{
return $this->_x + $this->_width;
}
/**
* Gets the Y-position of the bottom of this rectangle.
* @return float Returns a float representing the Y-position of the bottom of this rectangle.
*/
public function getBottom()
{
return $this->_y + $this->_height;
}
/**
* Gets the location of this rectangle.
* @return Point Returns a Point object which represents the location of this rectangle.
*/
public function getLocation()
{
return new Point( $this->_x, $this->_y );
}
/**
* Gets the size of this rectangle.
* @return Size Returns a Size object which represents the size of this rectangle.
*/
public function getSize()
{
return new Size( $this->_width, $this->_height );
}
/**
* Checks if the given value is inside the rectangle.
* @param mixed $x The X-position, point or rectangle value to check for.
* @param mixed $y The Y-position to check for.
* @return bool Returns true when the given value is inside this rectangle; false otherwise.
*/
public function contains( $x = 0.0, $y = 0.0 )
{
$result = false;
if ( $x instanceof Zend_Math_Point )
{
$x = $x->getX();
$y = $x->getY();
$result = ( ( $x >= $this->_x && $x <= $this->_x + $this->_width ) &&
( $y >= $this->_y && $y <= $this->_y + $this->_height ) );
}
else if ( $x instanceof Zend_Math_Rectangle )
{
// The complete rectangle must be present in this rectangle for this
// check to complete.
$posBegX = $x->getX();
$posBegY = $x->getY();
$posEndX = $x->getX() + $x->getWidth();
$posEndY = $x->getY() + $x->getHeight();
$result = ( ( $x >= $this->_x && $x <= $this->_x + $this->_width ) &&
( $y >= $this->_y && $y <= $this->_y + $this->_height ) );
}
else if ( is_float( $x ) || is_int( $x ) )
{
$result = ( ( $x >= $this->_x && $x <= $this->_x + $this->_width ) &&
( $y >= $this->_y && $y <= $this->_y + $this->_height ) );
}
else
{
throw new InvalidArgumentException( 'Can only offset a rectangle by supplying numbers or a Zend_Math_Point.' );
}
return $result;
}
/**
* Pushes the edges of this rectangle out by the given amount.
* @param mixed $horizontalAmount The value to push the sides out.
* @param mixed $verticalAmount The value to push the top and bottom out.
* @return Zend_Math_Rectangle Returns the updated instance of this class.
*/
public function inflate( $horizontalAmount, $verticalAmount )
{
$hSpace = $horizontalAmount / 2;
$vSpace = $verticalAmount / 2;
$this->_x -= $hSpace;
$this->_y -= $vSpace;
$this->_width += $hSpace;
$this->_height += $vSpace;
return false;
}
/**
* Checks if the given rectangle intersects with this rectangle.
* @param Zend_Math_Rectangle $value The value to check for.
* @return bool Returns true if the rectangle intersect; false otherwise.
*/
public function intersects( Zend_Math_Rectangle $value )
{
return (( $value->getLeft() > $this->getRight() ) ||
( $value->getRight() < $this->getLeft() ) ||
( $value->getTop() > $this->getBottom() ) ||
( $value->getBottom() < $this->getTop() ));
}
/**
* Changes the location of this rectangle.
* @param mixed $X The change in the X-position.
* @param mixed $y The change in the Y-position.
* @return Zend_Math_Rectangle Returns the updated instance of this class.
*/
public function offset( $x = 0.0, $y = 0.0 )
{
if ( $x instanceof Zend_Math_Point )
{
$this->_x += $x->getX();
$this->_y += $x->getY();
}
else if ( is_float( $x ) || is_int( $x ) )
{
$this->_x += $x;
$this->_y += $y;
}
else
{
throw new InvalidArgumentException( 'Can only offset a rectangle by supplying numbers or a Zend_Math_Point.' );
}
return $this;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Size
{
/**
* The width of the size.
* @var float
*/
private $_width;
/**
* The height of the size.
* @var float
*/
private $_height;
/**
* Initializes a new instance of this class.
* @param $width The width of this size.
* @param $height The height of this size.
*/
public function __construct( $width, $height )
{
$this->_width = $width;
$this->_height = $height;
}
/**
* Gets the width of this size.
* @return float Returns the width of this size.
*/
public function getWidth()
{
return $this->_width;
}
/**
* Gets the height of this size.
* @return float Returns the height of this size.
*/
public function getHeight()
{
return $this->_height;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Vector implements Zend_Math_Interface, Iterator
{
/**
* The position of the iterator.
* @var int
*/
private $_position = 0;
/**
* The amount of components that this vector has.
* @var array
*/
protected $_components = array();
/**
* Initializes a new instance of this class.
* @param int $components The amount of components that this vector has.
*/
public function __construct( $components )
{
$this->_components = array_fill( 0, $components, 0.0 );
}
/**
* Gets the dimension of this vector.
* @return int Returns an integer which represents the dimension of this vector.
*/
public function getDimension()
{
return count( $this->_components );
}
/**
* Gets the given component.
* @return float
*/
public function get( $component )
{
return $this->_components[ $component ];
}
/**
* Sets the value to the given component.
* @param int $component The component to set.
* @param float $value The value to set.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function set( $component, $value )
{
if ( $component < $this->getDimension() )
{
$this->_components[ $component ] = $value;
}
else
{
throw new OutOfBoundsException( 'Trying to set component index ' . $component . ' while this vector only has ' . $this->getDimension() . ' components.' );
}
return $this;
}
/**
* Adds the given value to this value.
* @param $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function add( $value )
{
if ( $value instanceof Zend_Math_Vector )
{
foreach ( $value as $component => $componentValue )
{
$this->_components[ $component ] += $componentValue;
}
}
else if ( is_int( $value ) || is_float( $value ) )
{
foreach ( $this->_components as $component => $componentValue )
{
$this->_components[ $component ] += $value;
}
}
return $this;
}
/**
* Subtracts the given value from this value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function sub( $value )
{
if ( $value instanceof Zend_Math_Vector )
{
foreach ( $value as $component => $componentValue )
{
$this->_components[ $component ] -= $componentValue;
}
}
else if ( is_int( $value ) || is_float( $value ) )
{
foreach ( $this->_components as $component => $componentValue )
{
$this->_components[ $component ] -= $value;
}
}
return $this;
}
/**
* Multiplies this value with the given value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function mul( $value )
{
if ( $value instanceof Zend_Math_Vector )
{
foreach ( $value as $component => $componentValue )
{
$this->_components[ $component ] *= $componentValue;
}
}
else if ( is_int( $value ) || is_float( $value ) )
{
foreach ( $this->_components as $component => $componentValue )
{
$this->_components[ $component ] *= $value;
}
}
return $this;
}
/**
* Divides this value by the given value.
* @param mixed $value The value to calculate with.
* @return Zend_Math_Interface Returns the instance of this object.
*/
public function div( $value )
{
if ( $value instanceof Zend_Math_Vector )
{
foreach ( $value as $component => $componentValue )
{
if ( !$componentValue )
{
throw new Zend_Math_Exception_DivideByZero( 'A value cannot be divided by zero.' );
}
$this->_components[ $component ] /= $componentValue;
}
}
else if ( is_int( $value ) || is_float( $value ) )
{
if ( !$value )
{
throw new Zend_Math_Exception_DivideByZero( 'A value cannot be divided by zero.' );
}
foreach ( $this->_components as $component => $componentValue )
{
$this->_components[ $component ] /= $value;
}
}
return $this;
}
/**
* Calculates the dot product of this vector and the given vector.
* @param Zend_Math_Vector $value The vector to calculate with.
* @return float Returns the dot product between the two vectors.
*/
public function dot( Zend_Math_Vector $value )
{
if ( $this->getDimension() != $value->getDimension() )
{
throw new RuntimeException( 'We can only calculate the dot product between two vectors with the same dimension.' );
}
$result = 0.0;
foreach ( $value as $component => $componentValue )
{
$result += ( $this->_components[ $component ] * $componentValue );
}
return $result;
}
/**
* Calculates the length of this vector.
* @return float Returns the length of this vector.
*/
public function length()
{
$result = 0.0;
foreach ( $this->_components as $component => $value )
{
$result += ( $value * $value );
}
return sqrt( $result );
}
/**
* Calculates the lineair interpolation between the given vectors.
* @param Zend_Math_Vector $vector1 The vector to interpolte from.
* @param Zend_Math_Vector $vector2 The vector to interpolte to.
* @param float $amount The weight value. This is a value between 0.0 and 1.0.
* @return Zend_Math_Vector Returns a vector containing the interpolated values.
*/
public final static function lerp( Zend_Math_Vector $vector1, Zend_Math_Vector $vector2, $amount )
{
if ( $vector1->getDimension() != $vector2->getDimension() )
{
throw new InvalidArgumentException( 'Vectors must be of the same dimension.' );
}
$result = clone $vector1;
for ( $i = 0; $i < $result->getDimension(); ++$i )
{
$val1 = $vector1->get( $i );
$val2 = $vector2->get( $i );
$result->set( $i, $val1 + ( $val2 - $val1 ) * $amount );
}
return $result;
}
/**
* Negates this vector.
* @return Zend_Math_Vector Returns the instance of this vector.
*/
public function negate()
{
foreach ( $this->_components as $component => $value )
{
$this->_components[ $component ] = -$value;
}
return $this;
}
/**
* Normalizes this vector.
* @return Zend_Math_Vector Returns the instance of this vector.
*/
public function normalize()
{
$cloned = clone $this;
$cloned->div( $this->length() );
foreach ( $this->_components as $component => $value )
{
$this->_components[ $component ] = $cloned->get( $component );
}
return $this;
}
/**
* Gets the current value from the iterator.
* @return mixed Returns the current value of the iterator.
*/
public function current()
{
return $this->_components[ $this->_position ];
}
/**
* Gets the current key from the iterator.
* @return int Returns the current key of the iterator.
*/
public function key()
{
return $this->_position;
}
/**
* Increases the iterator's position.
* @return void
*/
public function next()
{
++$this->_position;
}
/**
* Rewinds the iterator's position.
* @return void
*/
public function rewind()
{
$this->_position = 0;
}
/**
* Checks if the iterator is still valid.
* @return boolean Returns true when the iterator is valid; false otherwise.
*/
public function valid()
{
return $this->_position < $this->getDimension();
}
/**
* Converts this value to a readable string.
* @return string
*/
public function __toString()
{
return implode( ',', $this->_components );
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Vector_Vector2 extends Zend_Math_Vector
{
/**
* Initializes a new instance of this class.
* @param float $X The X-value of this vector.
* @param float $Y The Y-value of this vector.
*/
public function __construct( $X = 0.0, $Y = 0.0 )
{
parent::__construct( 2 );
$this->set( 0, $X );
$this->set( 1, $Y );
}
/**
* Gets the X-value of this vector.
* @return float Returns the X-value as a float value.
*/
public function getX()
{
return $this->get( 0 );
}
/**
* Sets the X-value of this vector.
* @param float @value The value to set.
* @return Zend_Math_Vector_Interface Returns the instance of this class.
*/
public function setX( $value )
{
$this->set( 0, $value );
return $this;
}
/**
* Gets the Y-value of this vector.
* @return float Returns the Y-value as a float value.
*/
public function getY()
{
return $this->get( 1 );
}
/**
* Sets the Y-value of this vector.
* @param float @value The value to set.
* @return Zend_Math_Vector_Interface Returns the instance of this class.
*/
public function setY( $value )
{
$this->set( 1, $value );
return $this;
}
/**
* Gets the zero vector.
* @return Zend_Math_Vector2 Returns the zero vector.
*/
public final static function getZero()
{
return new Zend_Math_Vector2( 0.0, 0.0, 0.0, 0.0 );
}
/**
* Gets the X-unit vector.
* @return Zend_Math_Vector3 Returns the X-unit vector.
*/
public final static function getUnitX()
{
return new Zend_Math_Vector2( 1.0, 0.0 );
}
/**
* Gets the Y-unit vector.
* @return Zend_Math_Vector3 Returns the Y-unit vector.
*/
public final static function getUnitY()
{
return new Zend_Math_Vector2( 0.0, 1.0 );
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Vector_Vector3 extends Zend_Math_Vector
{
/**
* Initializes a new instance of this class.
* @param float $X The X-value of this vector.
* @param float $Y The Y-value of this vector.
* @param float $Z The Z-value of this vector.
*/
public function __construct( $X = 0.0, $Y = 0.0, $Z = 0.0 )
{
parent::__construct( 3 );
$this->set( 0, $X );
$this->set( 1, $Y );
$this->set( 2, $Z );
}
/**
* Gets the X-value of this vector.
* @return float Returns the X-value as a float value.
*/
public function getX()
{
return $this->get( 0 );
}
/**
* Sets the X-value of this vector.
* @param float @value The value to set.
* @return Zend_Math_Vector_Interface Returns the instance of this class.
*/
public function setX( $value )
{
$this->set( 0, $value );
return $this;
}
/**
* Gets the Y-value of this vector.
* @return float Returns the Y-value as a float value.
*/
public function getY()
{
return $this->get( 1 );
}
/**
* Sets the Y-value of this vector.
* @param float @value The value to set.
* @return Zend_Math_Vector_Interface Returns the instance of this class.
*/
public function setY( $value )
{
$this->set( 1, $value );
return $this;
}
/**
* Gets the Z-value of this vector.
* @return float Returns the Z-value as a float value.
*/
public function getZ()
{
return $this->get( 2 );
}
/**
* Sets the Z-value of this vector.
* @param float @value The value to set.
* @return Zend_Math_Vector_Interface Returns the instance of this class.
*/
public function setZ( $value )
{
$this->set( 2, $value );
return $this;
}
/**
* Gets the zero vector.
* @return Zend_Math_Vector_Vector3 Returns the zero vector.
*/
public final static function getZero()
{
return new Zend_Math_Vector_Vector3( 0.0, 0.0, 0.0, 0.0 );
}
/**
* Gets the X-unit vector.
* @return Zend_Math_Vector_Vector3 Returns the X-unit vector.
*/
public final static function getUnitX()
{
return new Zend_Math_Vector_Vector3( 1.0, 0.0, 0.0 );
}
/**
* Gets the Y-unit vector.
* @return Zend_Math_Vector_Vector3 Returns the Y-unit vector.
*/
public final static function getUnitY()
{
return new Zend_Math_Vector_Vector3( 0.0, 1.0, 0.0 );
}
/**
* Gets the Z-unit vector.
* @return Zend_Math_Vector_Vector3 Returns the Z-unit vector.
*/
public final static function getUnitZ()
{
return new Zend_Math_Vector_Vector3( 0.0, 0.0, 1.0 );
}
/**
* Calculates the cross product of this vector and the given vector.
* @param Zend_Math_Vector_Vector3 $value The vector to calculate with.
* @return Zend_Math_Vector_Vector3 Returns the cross product between the two vectors.
*/
public function cross( Zend_Math_Vector_Vector3 $value )
{
if ( $this->getDimension() != $value->getDimension() )
{
throw new RuntimeException( 'We can only calculate the cross product between two vectors with the same dimension.' );
}
$result = clone $value;
$result->set( 0, $this->_components[ 1 ] * $value->get( 2 ) - $this->_components[ 2 ] * $value->get( 1 ) );
$result->set( 1, $this->_components[ 2 ] * $value->get( 0 ) - $this->_components[ 0 ] * $value->get( 2 ) );
$result->set( 2, $this->_components[ 0 ] * $value->get( 1 ) - $this->_components[ 1 ] * $value->get( 0 ) );
return $result;
}
}
{code}
{code}<?php
/**
* @category Zend
* @package Zend_Math
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Math_Vector_Vector3 extends Zend_Math_Vector
{
/**
* Initializes a new instance of this class.
* @param float $X The X-value of this vector.
* @param float $Y The Y-value of this vector.
* @param float $Z The Z-value of this vector.
* @param float $W The W-value of this vector.
*/
public function __construct( $X = 0.0, $Y = 0.0, $Z = 0.0, $W = 0.0 )
{
parent::__construct( 4 );
$this->set( 0, $X );
$this->set( 1, $Y );
$this->set( 2, $Z );
$this->set( 3, $Z );
}
/**
* Gets the X-value of this vector.
* @return float Returns the X-value as a float value.
*/
public function getX()
{
return $this->get( 0 );
}
/**
* Sets the X-value of this vector.
* @param float @value The value to set.
* @return Zend_Math_Vector_Interface Returns the instance of this class.
*/
public function setX( $value )
{
$this->set( 0, $value );
return $this;
}
/**
* Gets the Y-value of this vector.
* @return float Returns the Y-value as a float value.
*/
public function getY()
{
return $this->get( 1 );
}
/**
* Sets the Y-value of this vector.
* @param float @value The value to set.
* @return Zend_Math_Vector_Interface Returns the instance of this class.
*/
public function setY( $value )
{
$this->set( 1, $value );
return $this;
}
/**
* Gets the Z-value of this vector.
* @return float Returns the Z-value as a float value.
*/
public function getZ()
{
return $this->get( 2 );
}
/**
* Sets the Z-value of this vector.
* @param float @value The value to set.
* @return Zend_Math_Vector_Interface Returns the instance of this class.
*/
public function setZ( $value )
{
$this->set( 2, $value );
return $this;
}
/**
* Gets the W-value of this vector.
* @return float Returns the W-value as a float value.
*/
public function getW()
{
return $this->get( 3 );
}
/**
* Sets the W-value of this vector.
* @param float @value The value to set.
* @return Zend_Math_Vector_Interface Returns the instance of this class.
*/
public function setW( $value )
{
$this->set( 3, $value );
return $this;
}
/**
* Gets the zero vector.
* @return Zend_Math_Vector4 Returns the zero vector.
*/
public final static function getZero()
{
return new Zend_Math_Vector4( 0.0, 0.0, 0.0, 0.0 );
}
/**
* Gets the X-unit vector.
* @return Zend_Math_Vector4 Returns the X-unit vector.
*/
public final static function getUnitX()
{
return new Zend_Math_Vector4( 1.0, 0.0, 0.0, 0.0 );
}
/**
* Gets the Y-unit vector.
* @return Zend_Math_Vector4 Returns the Y-unit vector.
*/
public final static function getUnitY()
{
return new Zend_Math_Vector4( 0.0, 1.0, 0.0, 0.0 );
}
/**
* Gets the Z-unit vector.
* @return Zend_Math_Vector4 Returns the Z-unit vector.
*/
public final static function getUnitZ()
{
return new Zend_Math_Vector4( 0.0, 0.0, 1.0, 0.0 );
}
/**
* Gets the W-unit vector.
* @return Zend_Math_Vector4 Returns the W-unit vector.
*/
public final static function getUnitW()
{
return new Zend_Math_Vector4( 0.0, 0.0, 0.0, 1.0 );
}
}
{code}
{zone-data}
{zone-template-instance}