Vector::$verbose = True; $vtxO = new Vertex(array('x' => 0.0, 'y' => 0.0, 'z' => 0.0)); $vtxX = new Vertex(array('x' => 1.0, 'y' => 0.0, 'z' => 0.0)); $vtxY = new Vertex(array('x' => 0.0, 'y' => 1.0, 'z' => 0.0)); $vtxZ = new Vertex(array('x' => 0.0, 'y' => 0.0, 'z' => 1.0)); $vtcXunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxX)); $vtcYunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxY)); $vtcZunit = new Vector(array('orig' => $vtxO, 'dest' => $vtxZ)); print $vtcXunit . PHP_EOL; print $vtcYunit . PHP_EOL; print $vtcZunit . PHP_EOL; $dest1 = new Vertex(array('x' => -12.34, 'y' => 23.45, 'z' => -34.56)); Vertex::$verbose = True; $vtc1 = new Vector(array('dest' => $dest1)); Vertex::$verbose = False; $orig2 = new Vertex(array('x' => 23.87, 'y' => -37.95, 'z' => 78.34)); $dest2 = new Vertex(array('x' => -12.34, 'y' => 23.45, 'z' => -34.56)); $vtc2 = new Vector(array('orig' => $orig2, 'dest' => $dest2)); print 'Magnitude is ' . $vtc2->magnitude() . PHP_EOL; $nVtc2 = $vtc2->normalize(); print 'Normalized $vtc2 is ' . $nVtc2 . PHP_EOL; print 'Normalized $vtc2 magnitude is ' . $nVtc2->magnitude() . PHP_EOL; print '$vtc1 + $vtc2 is ' . $vtc1->add($vtc2) . PHP_EOL; print '$vtc1 - $vtc2 is ' . $vtc1->sub($vtc2) . PHP_EOL; print 'opposite of $vtc1 is ' . $vtc1->opposite() . PHP_EOL; print 'scalar product of $vtc1 and 42 is ' . $vtc1->scalarProduct(42) . PHP_EOL; print 'dot product of $vtc1 and $vtc2 is ' . $vtc1->dotProduct($vtc2) . PHP_EOL; print 'cross product of $vtc1 and $vtc2 is ' . $vtc1->crossProduct($vtc2) . PHP_EOL; print 'cross product of $vtcXunit and $vtcYunit is ' . $vtcXunit->crossProduct($vtcYunit) . 'aka $vtcZunit' . PHP_EOL; print 'cosinus of angle between $vtc1 and $vtc2 is ' . $vtc1->cos($vtc2) . PHP_EOL; print 'cosinus of angle between $vtcXunit and $vtcYunit is ' . $vtcXunit->cos($vtcYunit) . PHP_EOL;
/** * @covers webd\vectors\Vector::normalize */ public function testNormalize() { $this->assertEquals(1, $this->object->normalize()->norm()); }
/** * @dataProvider dataProviderForNormalize */ public function testNormalize(array $A, array $expected) { $A = new Vector($A); $Â = $A->normalize(); $expected = new Vector($expected); $this->assertEquals($expected, $Â, '', 1.0E-8); $this->assertEquals($expected->getVector(), $Â->getVector(), '', 1.0E-8); }
/** * Project the vector onto another vector. * * @param Vector $b The vector to project this vector onto. * @return Vector The vector projection of this vector onto $b. * @throws \Exception if the vector length of $b is zero. * @throws \Exception if the vectors are not in the same vector space. * @see checkVectorSpace() For Exception information. */ public function projectOnto(Vector $b) { $bUnit = $b->normalize(); return $bUnit->multiplyByScalar($this->dotProduct($bUnit)); }