[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/library/ezc/Graph/src/math/ -> vector.php (source)

   1  <?php
   2  /**
   3   * File containing the ezcGraphVector class
   4   *
   5   * @package Graph
   6   * @version 1.5
   7   * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
   8   * @license http://ez.no/licenses/new_bsd New BSD License
   9   * @access private
  10   */
  11  /**
  12   * Represents two dimensional vectors
  13   *
  14   * This class is internally used to represent vectors for geometric calculation
  15   * in the two dimensional cartesian coordinate system.
  16   *
  17   * Vectors are an extension of the basic coordinate class, and add methods to
  18   * calculate the length of a vector, perform various operations on angles, like
  19   * rotations and the calculation of angles between two vectors.
  20   *
  21   * @version 1.5
  22   * @package Graph
  23   * @access private
  24   */
  25  class ezcGraphVector extends ezcGraphCoordinate
  26  {
  27      /**
  28       * Rotates vector to the left by 90 degrees
  29       * 
  30       * @return void
  31       */
  32      public function rotateCounterClockwise()
  33      {
  34          $tmp = $this->x;
  35          $this->x = $this->y;
  36          $this->y = -$tmp;
  37  
  38          return $this;
  39      }
  40  
  41      /**
  42       * Rotates vector to the right by 90 degrees
  43       * 
  44       * @return void
  45       */
  46      public function rotateClockwise()
  47      {
  48          $tmp = $this->x;
  49          $this->x = -$this->y;
  50          $this->y = $tmp;
  51  
  52          return $this;
  53      }
  54      
  55      /**
  56       * Unifies vector length to 1
  57       * 
  58       * @return void
  59       */
  60      public function unify()
  61      {
  62          $length = $this->length();
  63          if ( $length == 0 )
  64          {
  65              return $this;
  66          }
  67  
  68          $this->x /= $length;
  69          $this->y /= $length;
  70  
  71          return $this;
  72      }
  73  
  74      /**
  75       * Returns length of vector
  76       * 
  77       * @return float
  78       */
  79      public function length()
  80      {
  81          return sqrt(
  82              pow( $this->x, 2 ) +
  83              pow( $this->y, 2 )
  84          );
  85      }
  86  
  87      /**
  88       * Multiplies vector with a scalar
  89       * 
  90       * @param float $value 
  91       * @return void
  92       */
  93      public function scalar( $value )
  94      {
  95          $this->x *= $value;
  96          $this->y *= $value;
  97  
  98          return $this;
  99      }
 100  
 101      /**
 102       * Calculates scalar product of two vectors
 103       * 
 104       * @param ezcGraphCoordinate $vector 
 105       * @return void
 106       */
 107      public function mul( ezcGraphCoordinate $vector )
 108      {
 109          return $this->x * $vector->x + $this->y * $vector->y;
 110      }
 111  
 112      /**
 113       * Returns the angle between two vectors in radian
 114       * 
 115       * @param ezcGraphCoordinate $vector 
 116       * @return float
 117       */
 118      public function angle( ezcGraphCoordinate $vector )
 119      {
 120          if ( !$vector instanceof ezcGraphVector )
 121          {
 122              // Ensure beeing a vector for calling length()
 123              $vector = ezcGraphVector::fromCoordinate( $vector );
 124          }
 125          
 126          $factor = $this->length() * $vector->length();
 127  
 128          if ( $factor == 0 )
 129          {
 130              return false;
 131          }
 132          else
 133          {
 134              return acos( $this->mul( $vector ) / $factor );
 135          }
 136      }
 137  
 138      /**
 139       * Adds a vector to another vector
 140       * 
 141       * @param ezcGraphCoordinate $vector 
 142       * @return void
 143       */
 144      public function add( ezcGraphCoordinate $vector )
 145      {
 146          $this->x += $vector->x;
 147          $this->y += $vector->y;
 148  
 149          return $this;
 150      }
 151  
 152      /**
 153       * Subtracts a vector from another vector
 154       * 
 155       * @param ezcGraphCoordinate $vector 
 156       * @return void
 157       */
 158      public function sub( ezcGraphCoordinate $vector )
 159      {
 160          $this->x -= $vector->x;
 161          $this->y -= $vector->y;
 162  
 163          return $this;
 164      }
 165  
 166      /**
 167       * Creates a vector from a coordinate object
 168       * 
 169       * @param ezcGraphCoordinate $coordinate 
 170       * @return ezcGraphVector
 171       */
 172      public static function fromCoordinate( ezcGraphCoordinate $coordinate )
 173      {
 174          return new ezcGraphVector( $coordinate->x, $coordinate->y );
 175      }
 176  
 177      /**
 178       * Transform vector using transformation matrix
 179       * 
 180       * @param ezcGraphTransformation $transformation 
 181       * @return ezcGraphVector
 182       */
 183      public function transform( ezcGraphTransformation $transformation )
 184      {
 185          $result = $transformation->transformCoordinate( $this );
 186  
 187          $this->x = $result->x;
 188          $this->y = $result->y;
 189  
 190          return $this;
 191      }
 192  }
 193  
 194  ?>


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7