PHP code example of macocci7 / php-math-vector

1. Go to this page and download the library: Download macocci7/php-math-vector library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

macocci7 / php-math-vector example snippets




acocci7\PhpMathVector\Vector2d;

$a = new Vector2d(
    initialPoint: [1, 2],   // (x, y) = (1, 2)
    components: [3, 4],     // (x, y) = (3, 4)
);

$b = new Vector2d(
    initialPoint: [5, 6],   // (x, y) = (5, 6)
    components: [7, 8],     // (x, y) = (7, 8)
);

// Equivalent to:
// $a->magnitude()
// $b->magnitude()
echo "length of the vector a:" . $a->length() . PHP_EOL;
echo "length of the vector b:" . $b->length() . PHP_EOL;

[$x1, $y1] = $a->components();
[$x2, $y2] = $b->components();

[$x1, $y1] = $a->initialPoint();
[$x2, $y2] = $a->terminalPoint();

$ua = $a->unitVector();
$ub = $b->unitVector();

$c = $a->multiply(2);
$d = $b->multiply(0.8);

// Equivalent to:
// $c = new Vector2d([1, 2], [3 + 7, 4 + 8]);
$c = $a->add($b);

// Equivalent to:
// $c = new Vector2d([1, 2], [3 - 7, 4 - 8]);
$c = $a->subtract($b);

// Equivalent to:
// $b->dotProduct($a)
echo "dot product:" . $a->dotProduct($b);

// Equivalent to:
// $b->cos($a)
echo "cos:" . $a->cos($b) . PHP_EOL;

echo "cos:" . $a->cos() . PHP_EOL;
echo "cos:" . $b->cos() . PHP_EOL;

// Equivalent to:
// $b->degrees($a)
// $b->radian($a)
echo "degrees:" . $a->degrees($b) . PHP_EOL;
echo "radian:" . $a->radian($b) . PHP_EOL;

echo "degrees:" . $a->degrees() . PHP_EOL;
echo "radian:" . $a->radian() . PHP_EOL;

$a = new Vector2d([1, 2], [3, 4]);
$b = $a->rotate(90.0);



acocci7\PhpMathVector\Vector3d;

$a = new Vector3d(
    initialPoint: [1, 2, 3],    // (x, y, z) = (1, 2, 3)
    components: [3, 4, 5],      // (x, y, z) = (3, 4, 5)
);

$b = new Vector3d(
    initialPoint: [1, 2, 3],    // (x, y, z) = (1, 2, 3)
    components: [5, 4, 3],      // (x, y, z) = (5, 4, 3)
);

// Equivalent to:
// $a->magnitude()
// $b->magnitude()
echo "length of the vector a:" . $a->length() . PHP_EOL;
echo "length of the vector b:" . $b->length() . PHP_EOL;

[$x1, $y1, $z1] = $a->components();
[$x2, $y2, $z2] = $b->components();

[$x1, $y1, $z1] = $a->initialPoint();
[$x2, $y2, $z2] = $a->terminalPoint();

$ua = $a->unitVector();
$ub = $b->unitVector();

$c = $a->multiply(2);
$d = $b->multiply(0.8);

// Equivalent to:
// $c = new Vector2d([1, 2, 3], [3 + 5, 4 + 4, 5 + 3]);
$c = $a->add($b);

// Equivalent to:
// $c = new Vector2d([1, 2, 3], [3 - 5, 4 - 4, 5 - 3]);
$c = $a->subtract($b);

// Equivalent to:
// $b->dotProduct($a)
echo "dot product:" . $a->dotProduct($b);

// Equivalent to:
// $b->crossProduct($a)
$c = $a->crossProduct($b);

// Equivalent to:
// $b->cos($a)
echo "cos:" . $a->cos($b) . PHP_EOL;

echo "cos:" . $a->cos() . PHP_EOL;
echo "cos:" . $b->cos() . PHP_EOL;

// Equivalent to:
// $b->degrees($a)
// $b->radian($a)
echo "degrees:" . $a->degrees($b) . PHP_EOL;
echo "radian:" . $a->radian($b) . PHP_EOL;

echo "degrees:" . $a->degrees() . PHP_EOL;
echo "radian:" . $a->radian() . PHP_EOL;

$a = new Vector3d([1, 2, 3], [4, 5, 6]);
$b = new Vector3d([4, 5, 6], [7, 8, 9]);
$c = $a->rotate($b, 90.0);