diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2007-03-27 14:46:09 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2007-03-27 14:46:09 +0000 |
commit | 46b36627ff6184fed301ab68c428a26ac5412cda (patch) | |
tree | 567f5673cad427d22601f9823d8fb1fdb3e400c7 /src/math | |
parent | 8c9729064b7d8fde5aa75701c799e4ea0181f23c (diff) | |
download | zetacomponents-graph-46b36627ff6184fed301ab68c428a26ac5412cda.zip zetacomponents-graph-46b36627ff6184fed301ab68c428a26ac5412cda.tar.gz |
- Added private classes for more convenient coordinate and vector
transformations
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/rotation.php | 67 | ||||
-rw-r--r-- | src/math/transformation.php | 86 | ||||
-rw-r--r-- | src/math/translation.php | 29 | ||||
-rw-r--r-- | src/math/vector.php | 16 |
4 files changed, 198 insertions, 0 deletions
diff --git a/src/math/rotation.php b/src/math/rotation.php new file mode 100644 index 0000000..13583df --- /dev/null +++ b/src/math/rotation.php @@ -0,0 +1,67 @@ +<?php +/** + * File containing the ezcGraphRotation class + * + * @package Graph + * @version //autogentag// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @access private + */ +/** + * Class to create rotation matrices from given rotation and center point + * + * @package Graph + * @access private + */ +class ezcGraphRotation extends ezcGraphTransformation +{ + /** + * Rotation in degrees + * + * @var float + */ + protected $rotation; + + /** + * Center point + * + * @var ezcGraphCoordinate + */ + protected $center; + + /** + * Construct rotation matrix from rotation (in degrees) and optional + * center point. + * + * @param int $rotation + * @param ezcGraphCoordinate $center + * @return ezcGraphTransformation + */ + public function __construct( $rotation = 0, ezcGraphCoordinate $center = null ) + { + $this->rotation = (float) $rotation; + + if ( $center === null ) + { + $clockwiseRotation = deg2rad( $rotation ); + $rotationMatrixArray = array( + array( cos( $clockwiseRotation ), -sin( $clockwiseRotation ), 0 ), + array( sin( $clockwiseRotation ), cos( $clockwiseRotation ), 0 ), + array( 0, 0, 1 ), + ); + + return parent::__construct( $rotationMatrixArray ); + } + + parent::__construct(); + + $this->center = $center; + + $this->multiply( new ezcGraphTranslation( $center->x, $center->y ) ); + $this->multiply( new ezcGraphRotation( $rotation ) ); + $this->multiply( new ezcGraphTranslation( -$center->x, -$center->y ) ); + } +} + +?> diff --git a/src/math/transformation.php b/src/math/transformation.php new file mode 100644 index 0000000..cddec68 --- /dev/null +++ b/src/math/transformation.php @@ -0,0 +1,86 @@ +<?php +/** + * File containing the ezcGraphTransformation class + * + * @package Graph + * @version //autogentag// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @access private + */ +/** + * Class defining transformations like scaling and rotation by a + * 3x3 transformation matrix for |R^2 + * + * @package Graph + * @access private + */ +class ezcGraphTransformation extends ezcGraphMatrix +{ + /** + * Constructor + * + * Creates a matrix with 3x3 dimensions. Optionally accepts an array to + * define the initial matrix values. If no array is given an identity + * matrix is created. + * + * @param int $rows + * @param int $columns + * @param array $values + * @return void + */ + public function __construct( array $values = null ) + { + parent::__construct( 3, 3, $values ); + } + + /** + * Multiplies two matrices + * + * Multiply current matrix with another matrix and returns the result + * matrix. + * + * @param ezcGraphMatrix $matrix Second factor + * @returns ezcGraphMatrix Result matrix + */ + public function multiply( ezcGraphMatrix $matrix ) + { + $mColumns = $matrix->columns(); + + // We want to ensure, that the matrix stays 3x3 + if ( ( $this->columns !== $matrix->rows() ) && + ( $this->rows !== $mColumns ) ) + { + throw new ezcGraphMatrixInvalidDimensionsException( $this->columns, $this->rows, $mColumns, $matrix->rows() ); + } + + $result = parent::multiply( $matrix ); + + // The matrix dimensions stay the same, so that we can modify $this. + for ( $i = 0; $i < $this->rows; ++$i ) + { + for ( $j = 0; $j < $mColumns; ++$j ) + { + $this->set( $i, $j, $result->get( $i, $j ) ); + } + } + + return $this; + } + + /** + * Transform a coordinate with the current transformation matrix. + * + * @param ezcGraphCoordinate $coordinate + * @return ezcGraphCoordinate + */ + public function transformCoordinate( ezcGraphCoordinate $coordinate ) + { + $vector = new ezcGraphMatrix( 3, 1, array( array( $coordinate->x ), array( $coordinate->y ), array( 1 ) ) ); + $vector = parent::multiply( $vector ); + + return new ezcGraphCoordinate( $vector->get( 0, 0 ), $vector->get( 1, 0 ) ); + } +} + +?> diff --git a/src/math/translation.php b/src/math/translation.php new file mode 100644 index 0000000..04fe642 --- /dev/null +++ b/src/math/translation.php @@ -0,0 +1,29 @@ +<?php +/** + * File containing the ezcGraphTranslation class + * + * @package Graph + * @version //autogentag// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + * @access private + */ +/** + * Class creating translation matrices from given movements + * + * @package Graph + * @access private + */ +class ezcGraphTranslation extends ezcGraphTransformation +{ + public function __construct( $x = 0, $y = 0 ) + { + parent::__construct( array( + array( 1, 0, $x ), + array( 0, 1, $y ), + array( 0, 0, 1 ), + ) ); + } +} + +?> diff --git a/src/math/vector.php b/src/math/vector.php index c8ff3bc..6397c31 100644 --- a/src/math/vector.php +++ b/src/math/vector.php @@ -165,6 +165,22 @@ class ezcGraphVector extends ezcGraphCoordinate { return new ezcGraphVector( $coordinate->x, $coordinate->y ); } + + /** + * Transform vector using transformation matrix + * + * @param ezcGraphTransformation $transformation + * @return ezcGraphVector + */ + public function transform( ezcGraphTransformation $transformation ) + { + $result = $transformation->transformCoordinate( $this ); + + $this->x = $result->x; + $this->y = $result->y; + + return $this; + } } ?> |