<?php require "./vendor/nubs/vectorix/src/Vector.php"; use Nubs\Vectorix\Vector; $v = new Vector([2, 5]); $ary = $v->components(); print $ary[0] . " " . $ary[1] . "\n"; $w = $v->multiplyByScalar(3); $ary = $w->components(); print $ary[0] . " " . $ary[1] . "\n";
/** * Verify that vector() returns the correct vector between a degenerate line * segment. * * @test * @uses \Nubs\Geometron\Point::__construct * @uses \Nubs\Geometron\Point::vector * @uses \Nubs\Geometron\Point::isSameSpace * @uses \Nubs\Geometron\LineSegment::__construct * @uses \Nubs\Geometron\LineSegment::a * @uses \Nubs\Geometron\LineSegment::b * @covers ::vector */ public function vectorOfDegenerateLineSegment() { $a = new Point(new Vector([1, 3])); $b = new Point(new Vector([1, 3])); $line = new LineSegment($a, $b); $expected = new Vector([0, 0]); $this->assertTrue($expected->isEqual($line->vector())); }
/** * Returns the angle between the two vectors. * * @api * @param self $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 self::_checkVectorSpace() For exception information. */ public function angleBetween(self $b) { $denominator = $this->length() * $b->length(); if ($denominator == 0) { throw new Exception('Cannot divide by zero'); } return acos($this->dotProduct($b) / $denominator); }
/** * Verify that the angle between vectors of differently keyed components * fails. * * @test * @uses \Nubs\Vectorix\Vector::__construct * @uses \Nubs\Vectorix\Vector::components * @uses \Nubs\Vectorix\Vector::dimension * @uses \Nubs\Vectorix\Vector::isSameDimension * @uses \Nubs\Vectorix\Vector::isSameVectorSpace * @uses \Nubs\Vectorix\Vector::_checkVectorSpace * @uses \Nubs\Vectorix\Vector::length * @uses \Nubs\Vectorix\Vector::dotProduct * @covers ::angleBetween * @expectedException Exception * @expectedExceptionMessage The vectors' components must have the same keys */ public function angleBetweenDifferentlyKeyedVectors() { $a = new Vector([2, 4]); $b = new Vector(['x' => 3, 'y' => 7]); $a->angleBetween($b); }