Example #1
0
 /**
  * Calculates the intersection point bnetween the current and two other planes
  *
  * @param Plane $plane1
  * @param Plane $plane2
  * @return bool|\Math_Vector3
  * @throws InvalidArgumentException
  */
 public function calculateIntersectionPointWithTwoPlanes(self $plane1, self $plane2)
 {
     $n1 = $this->normalVectorNormalized;
     $n2 = $plane1->normalVectorNormalized;
     $n3 = $plane2->normalVectorNormalized;
     $d1 = $this->distanceToOrigin;
     $d2 = $plane1->distanceToOrigin;
     $d3 = $plane2->distanceToOrigin;
     $n2_x_n3 = \Math_VectorOp::crossProduct($n2, $n3);
     $n3_x_n1 = \Math_VectorOp::crossProduct($n3, $n1);
     $n1_x_n2 = \Math_VectorOp::crossProduct($n1, $n2);
     $p = new \Math_Vector3(\Math_VectorOp::add(\Math_VectorOp::add(\Math_VectorOp::scale($d1, $n2_x_n3), \Math_VectorOp::scale($d2, $n3_x_n1)), \Math_VectorOp::scale($d3, $n1_x_n2))->getTuple());
     $divisor = \Math_VectorOp::dotProduct($n1, $n2_x_n3);
     if ((double) 0 === $divisor) {
         throw new \InvalidArgumentException('no point-intersection');
     }
     $p->scale(1 / $divisor);
     return $p;
 }
echo "Converting to a unit vector\n";
$v->normalize();
echo $v->toString() . "\n";
echo "Length after normalizing: " . $v->length() . "\n";
echo "Reversing vector\n";
$v->reverse();
echo $v->toString() . "\n";
echo "==\nVector from tuple\n";
$w = new Math_Vector($t);
echo "Cartesian distance(v,w) = " . $v->distance($w) . "\n";
echo "Manhattan distance(v,w) = " . $v->distance($w, 'manhattan') . "\n";
echo "Chessboard distance(v,w) = " . $v->distance($w, 'chessboard') . "\n";
echo "Vector v: " . $v->toString() . "\n";
echo "Vector w: " . $w->toString() . "\n";
echo "==\nVector from another vector\n";
$z = new Math_Vector(new Math_Vector(range(2, 5)));
echo $z->toString() . "\n";
echo "==\nVector3 vector\n";
$x = new Math_Vector3(new Math_Tuple(array(1, 0, 1)));
echo $x->toString() . "\n";
echo "==\nVector2 vector\n";
$y = new Math_Vector2(array(1, 3));
echo $y->toString() . "\n";
echo "==\nInvalid vector\n";
$bar = new Math_Vector("foo");
if ($bar->isValid()) {
    echo "bar is good\n";
} else {
    echo "bar is bad\n";
}
print_r($bar);
Example #3
0
 /**
  * Takes all point vectors ("vertexes") of the polygon describing the face and sorts them
  * in clockwise order.
  */
 public function sortVerticesClockwise()
 {
     $center = $this->calculateCenter();
     for ($n = 0; $n <= count($this->vertexes) - 3; $n++) {
         $a = new \Math_Vector3(\Math_VectorOp::substract($this->vertexes[$n], $center)->getTuple());
         $a->normalize();
         $p = Plane::getInstanceByThreePositionVectors($this->vertexes[$n], $center, new \Math_Vector3(\Math_VectorOp::add($center, $this->plane->getNormalVectorNormalized())->getTuple()));
         $smallestAngle = -1;
         $smallest = -1;
         for ($m = $n + 1; $m <= count($this->vertexes) - 1; $m++) {
             if ($p->calculateSideOfPointVector($this->vertexes[$m]) !== Plane::SIDE_BACK) {
                 $b = new \Math_Vector3(\Math_VectorOp::substract($this->vertexes[$m], $center)->getTuple());
                 $b->normalize();
                 $angle = \Math_VectorOp::dotProduct($a, $b);
                 if ($angle > $smallestAngle) {
                     $smallestAngle = $angle;
                     $smallest = $m;
                 }
             }
         }
         if ($smallest == -1) {
             throw new \RuntimeException('Error: Degenerate polygon!');
         }
         //swap vertices
         $temp = $this->vertexes[$n + 1];
         $this->vertexes[$n + 1] = $this->vertexes[$smallest];
         $this->vertexes[$smallest] = $temp;
         unset($temp);
     }
     // Check if vertex order needs to be reversed for back-facing polygon
     $newPlane = Plane::getInstanceByThreePositionVectors($this->vertexes[0], $this->vertexes[1], $this->vertexes[2]);
     if (\Math_VectorOp::dotProduct($newPlane->getNormalVectorNormalized(), $this->plane->getNormalVectorNormalized()) < 0) {
         array_reverse($this->vertexes);
     }
 }
<?php

require_once "Math/Vector/Vector.php";
require_once "Math/Vector/Vector2.php";
require_once "Math/Vector/Vector3.php";
require_once "Math/Vector/VectorOp.php";
$v1 = new Math_Vector2(array(1, 2));
$v2 = new Math_Vector2(array(2, 4));
$w1 = new Math_Vector3(array(2, 3, 1));
$w2 = new Math_Vector3(array(1, -1, 0));
$w3 = new Math_Vector3(array(7, 3, 2));
echo date("Y-m-d H:i:s") . "\n";
echo "==\nVector v1: " . $v1->toString() . "\n";
echo "Vector v2: " . $v2->toString() . "\n";
$r = Math_VectorOp::add($v1, $v2);
echo "v1 + v2: " . $r->toString() . "\n";
$r = Math_VectorOp::substract($v1, $v2);
echo "v1 - v2: " . $r->toString() . "\n";
$r = Math_VectorOp::multiply($v1, $v2);
echo "v1 * v2: " . $r->toString() . "\n";
$r = Math_VectorOp::divide($v1, $v2);
echo "v1 / v2: " . $r->toString() . "\n";
echo "==\nVector w1: " . $w1->toString() . "\n";
echo "Vector w2: " . $w2->toString() . "\n";
echo "Vector w3: " . $w3->toString() . "\n";
$r = Math_VectorOp::scale(2.0, $w1);
echo " 2.0 * w1 = " . $r->toString() . "\n";
$r = Math_VectorOp::dotProduct($w1, $w2);
echo "w1 . w2 = {$r}\n";
$r = Math_VectorOp::crossProduct($w2, $w3);
echo "w2 x w3 = " . $r->toString() . "\n";