summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/graph_autoload.php1
-rw-r--r--src/math/vector.php132
-rw-r--r--tests/suite.php2
-rw-r--r--tests/vector_test.php164
4 files changed, 299 insertions, 0 deletions
diff --git a/src/graph_autoload.php b/src/graph_autoload.php
index e24233f..6d80a09 100644
--- a/src/graph_autoload.php
+++ b/src/graph_autoload.php
@@ -99,6 +99,7 @@ return array(
'ezcGraphTooManyDataSetsExceptions' => 'Graph/exceptions/too_many_datasets.php',
'ezcGraphInvalidDisplayTypeException' => 'Graph/exceptions/invalid_display_type.php',
+ 'ezcGraphVector' => 'Graph/math/vector.php',
'ezcGraphMatrix' => 'Graph/math/matrix.php',
'ezcGraphMatrixInvalidDimensionsException' => 'Graph/exceptions/invalid_dimensions.php',
'ezcGraphMatrixOutOfBoundingsException' => 'Graph/exceptions/out_of_boundings.php',
diff --git a/src/math/vector.php b/src/math/vector.php
new file mode 100644
index 0000000..bef549c
--- /dev/null
+++ b/src/math/vector.php
@@ -0,0 +1,132 @@
+<?php
+/**
+ * File containing the ezcGraphVector 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
+ */
+/**
+ * Represents two dimensional vectors
+ *
+ * @package Graph
+ * @access private
+ */
+class ezcGraphVector extends ezcGraphCoordinate
+{
+ /**
+ * Rotates vector to the left by 90 degrees
+ *
+ * @return void
+ */
+ public function toLeft()
+ {
+ $tmp = $this->x;
+ $this->x = -$this->y;
+ $this->y = $tmp;
+ }
+
+ /**
+ * Rotates vector to the right by 90 degrees
+ *
+ * @return void
+ */
+ public function toRight()
+ {
+ $tmp = $this->x;
+ $this->x = $this->y;
+ $this->y = -$tmp;
+ }
+
+ /**
+ * Unifies vector length to 1
+ *
+ * @return void
+ */
+ public function unify()
+ {
+ $length = $this->length();
+ if ( $length == 0 )
+ {
+ return false;
+ }
+
+ $this->x /= $length;
+ $this->y /= $length;
+ }
+
+ /**
+ * Returns length of vector
+ *
+ * @return float
+ */
+ public function length()
+ {
+ return sqrt(
+ pow( $this->x, 2 ) +
+ pow( $this->y, 2 )
+ );
+ }
+
+ /**
+ * Multiplies vector with a scalar
+ *
+ * @param float $value
+ * @return void
+ */
+ public function scalar( $value )
+ {
+ $this->x *= $value;
+ $this->y *= $value;
+ }
+
+ /**
+ * Calculates scalar product of two vectors
+ *
+ * @param ezcGraphCoordinate $vector
+ * @return void
+ */
+ public function mul( ezcGraphCoordinate $vector )
+ {
+ return $this->x * $vector->x + $this->y * $vector->y;
+ }
+
+ /**
+ * Adds a vector to another vector
+ *
+ * @param ezcGraphCoordinate $vector
+ * @return void
+ */
+ public function add( ezcGraphCoordinate $vector )
+ {
+ $this->x += $vector->x;
+ $this->y += $vector->y;
+ }
+
+ /**
+ * Subtracts a vector from another vector
+ *
+ * @param ezcGraphCoordinate $vector
+ * @return void
+ */
+ public function sub( ezcGraphCoordinate $vector )
+ {
+ $this->x -= $vector->x;
+ $this->y -= $vector->y;
+ }
+
+ /**
+ * Creates a vector from a coordinate object
+ *
+ * @param ezcGraphCoordinate $coordinate
+ * @return ezcGraphVector
+ */
+ public static function fromCoordinate( ezcGraphCoordinate $coordinate )
+ {
+ return new ezcGraphVector( $coordinate->x, $coordinate->y );
+ }
+}
+
+?>
diff --git a/tests/suite.php b/tests/suite.php
index ce8a09c..6c75338 100644
--- a/tests/suite.php
+++ b/tests/suite.php
@@ -38,6 +38,7 @@ require_once 'driver_flash_test.php';
require_once 'font_test.php';
require_once 'palette_test.php';
require_once 'matrix_test.php';
+require_once 'vector_test.php';
require_once 'boundings_test.php';
require_once 'polynom_test.php';
require_once 'struct_test.php';
@@ -82,6 +83,7 @@ class ezcGraphSuite extends PHPUnit_Framework_TestSuite
$this->addTest( ezcGraphTextTest::suite() );
$this->addTest( ezcGraphPaletteTest::suite() );
$this->addTest( ezcGraphMatrixTest::suite() );
+ $this->addTest( ezcGraphVectorTest::suite() );
$this->addTest( ezcGraphBoundingsTest::suite() );
$this->addTest( ezcGraphPolynomTest::suite() );
$this->addTest( ezcGraphStructTest::suite() );
diff --git a/tests/vector_test.php b/tests/vector_test.php
new file mode 100644
index 0000000..a3cb429
--- /dev/null
+++ b/tests/vector_test.php
@@ -0,0 +1,164 @@
+<?php
+/**
+ * ezcGraphVectorTest
+ *
+ * @package Graph
+ * @version //autogen//
+ * @subpackage Tests
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Tests for ezcGraph class.
+ *
+ * @package ImageAnalysis
+ * @subpackage Tests
+ */
+class ezcGraphVectorTest extends ezcTestCase
+{
+ public static function suite()
+ {
+ return new PHPUnit_Framework_TestSuite( "ezcGraphVectorTest" );
+ }
+
+ public function testCreateVector()
+ {
+ $vector = new ezcGraphVector( 1, 2 );
+
+ $this->assertEquals(
+ 1,
+ $vector->x
+ );
+
+ $this->assertEquals(
+ 2,
+ $vector->y
+ );
+ }
+
+ public function testCreateVectorFromCoordinate()
+ {
+ $vector = ezcGraphVector::fromCoordinate( new ezcGraphCoordinate( 1, 2 ) );
+
+ $this->assertEquals(
+ 1,
+ $vector->x
+ );
+
+ $this->assertEquals(
+ 2,
+ $vector->y
+ );
+ }
+
+ public function testVectorLength()
+ {
+ $vector = new ezcGraphVector( 1, 2 );
+
+ $this->assertEquals(
+ sqrt( 5 ),
+ $vector->length()
+ );
+ }
+
+ public function testUnifyVector()
+ {
+ $vector = new ezcGraphVector( 2, 0 );
+ $vector->unify();
+
+ $this->assertEquals(
+ 1,
+ $vector->x
+ );
+
+ $this->assertEquals(
+ 0,
+ $vector->y
+ );
+ }
+
+ public function testVectorMultiplyScalar()
+ {
+ $vector = new ezcGraphVector( 1, 2 );
+ $vector->scalar( 2 );
+
+ $this->assertEquals(
+ 2,
+ $vector->x
+ );
+
+ $this->assertEquals(
+ 4,
+ $vector->y
+ );
+ }
+
+ public function testVectorMultiplyCoordinate()
+ {
+ $vector = new ezcGraphVector( 1, 2 );
+ $result = $vector->mul( new ezcGraphCoordinate( 3, 2 ) );
+
+ $this->assertEquals(
+ $result,
+ 7
+ );
+ }
+
+ public function testVectorMultiplyVector()
+ {
+ $vector = new ezcGraphVector( 1, 2 );
+ $result = $vector->mul( new ezcGraphVector( 3, 2 ) );
+
+ $this->assertEquals(
+ $result,
+ 7
+ );
+ }
+
+ public function testVectorAddCoordinate()
+ {
+ $vector = new ezcGraphVector( 1, 2 );
+ $vector->add( new ezcGraphCoordinate( 3, 2 ) );
+
+ $this->assertEquals(
+ $vector,
+ new ezcGraphVector( 4, 4 )
+ );
+ }
+
+ public function testVectorAddVector()
+ {
+ $vector = new ezcGraphVector( 1, 2 );
+ $vector->add( new ezcGraphVector( 3, 2 ) );
+
+ $this->assertEquals(
+ $vector,
+ new ezcGraphVector( 4, 4 )
+ );
+ }
+
+ public function testVectorSubCoordinate()
+ {
+ $vector = new ezcGraphVector( 1, 2 );
+ $vector->sub( new ezcGraphCoordinate( 3, 2 ) );
+
+ $this->assertEquals(
+ $vector,
+ new ezcGraphVector( -2, 0 )
+ );
+ }
+
+ public function testVectorSubVector()
+ {
+ $vector = new ezcGraphVector( 1, 2 );
+ $vector->sub( new ezcGraphVector( 3, 2 ) );
+
+ $this->assertEquals(
+ $vector,
+ new ezcGraphVector( -2, 0 )
+ );
+ }
+}
+
+?>
OpenPOWER on IntegriCloud