示例#1
0
 public function crossProduct(Vector $other)
 {
     if ($this->length() != 3) {
         throw new Exception("Must be 3 dimension");
     }
     if ($other->length() != 3) {
         throw new Exception("Must be 3 dimension");
     }
     $class = get_called_class();
     $result = new $class();
     /* @var $result Vector */
     $result->value = array($this->value[1] * $other->value[2] - $this->value[2] * $other->value[1], $this->value[2] * $other->value[0] - $this->value[0] * $other->value[2], $this->value[0] * $other->value[1] - $this->value[1] * $other->value[0]);
     return $result;
 }
 /**
  * Axiom: |projᵇA|² + |perpᵇA|² = |A|²
  * Sum of squared lengths of proj and perp equals squared length of A
  * @dataProvider dataProviderForProjPerp
  */
 public function testProjPerpSumOfSquares(array $A, array $B)
 {
     $A = new Vector($A);
     $B = new Vector($B);
     $│A│² = $A->length() ** 2;
     $│projᵇA│² = $A->projection($B)->length() ** 2;
     $│perpᵇA│² = $A->perp($B)->length() ** 2;
     $this->assertEquals($│A│², $│projᵇA│² + $│perpᵇA│²);
 }
 /**
  * @dataProvider dataProviderForLength
  */
 public function testLength(array $A, $l²norm)
 {
     $A = new Vector($A);
     $this->assertEquals($l²norm, $A->length(), '', 0.0001);
 }
 /**
  * @covers webd\vectors\Vector::length
  */
 public function testLength()
 {
     $this->assertEquals(3, $this->object->length());
 }
示例#5
0
 /**
  * Returns the angle between the two vectors.
  *
  * @param Vector $b The vector to compute the angle between.
  * @return float The angle between the two vectors in radians.
  * @throws \Exception if either of the vectors are zero-length.
  * @throws \Exception if the vectors are not in the same vector space.
  * @see checkVectorSpace() For Exception information.
  */
 public function angleBetween(Vector $b)
 {
     $denominator = $this->length() * $b->length();
     if ($denominator == 0) {
         throw new \Exception('Cannot divide by zero');
     }
     return acos($this->dotProduct($b) / $denominator);
 }