Author: Kore Nordmann
Date: 2007-01-25 16:32:47 +0100 (Thu, 25 Jan 2007)
New Revision: 4577

Log:
- Added simple class for 2d vectors as an extension for coordinates to 
  simplify calculations.

Added:
   trunk/Graph/src/math/vector.php
   trunk/Graph/tests/vector_test.php
Modified:
   trunk/Graph/src/graph_autoload.php
   trunk/Graph/tests/suite.php

Modified: trunk/Graph/src/graph_autoload.php
===================================================================
--- trunk/Graph/src/graph_autoload.php  2007-01-25 13:24:32 UTC (rev 4576)
+++ trunk/Graph/src/graph_autoload.php  2007-01-25 15:32:47 UTC (rev 4577)
@@ -99,6 +99,7 @@
     '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',

Added: trunk/Graph/src/math/vector.php
===================================================================
--- trunk/Graph/src/math/vector.php     2007-01-25 13:24:32 UTC (rev 4576)
+++ trunk/Graph/src/math/vector.php     2007-01-25 15:32:47 UTC (rev 4577)
@@ -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 );
+    }
+}
+
+?>


Property changes on: trunk/Graph/src/math/vector.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/Graph/tests/suite.php
===================================================================
--- trunk/Graph/tests/suite.php 2007-01-25 13:24:32 UTC (rev 4576)
+++ trunk/Graph/tests/suite.php 2007-01-25 15:32:47 UTC (rev 4577)
@@ -38,6 +38,7 @@
 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 @@
         $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() );

Added: trunk/Graph/tests/vector_test.php
===================================================================
--- trunk/Graph/tests/vector_test.php   2007-01-25 13:24:32 UTC (rev 4576)
+++ trunk/Graph/tests/vector_test.php   2007-01-25 15:32:47 UTC (rev 4577)
@@ -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 )
+        );
+    }
+}
+
+?>


Property changes on: trunk/Graph/tests/vector_test.php
___________________________________________________________________
Name: svn:eol-style
   + native

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to