Download the PHP package macocci7/php-math-vector without Composer
On this page you can find all versions of the php package macocci7/php-math-vector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-math-vector
PHP-Math-Vector
A math library for handling vectors.
1. Features
PHP-Math-Vector
handles 2-dimensional vectors and 3-dimensional vectors.
PHP-Math-Vector
can:
- return the length (magnitude) of the vector
- return the unit vector
- return a vector multiplied by a real number
- add two vectors
- subtract one vector from another
- return the
dot product
of the two vectors - return the
cross product
of the two vectors - return the
cosine
of the angle between two vectors - return the angle between two vectors in
degrees
orradian
- return the
initial point
orterminal point
- return a set of each component of x and y
- rotate the vector by a specified angle
2. Contents
- 1. Features
- 2.Contents
- 3. Requirements
- 4. Installation
- 5. Usage
- 6. Examples
- 7. LICENSE
3. Requiremnets
- PHP 8.1 or later
- Composer
4. Installation
5. Usage
5.1. Two-dimensional Vectors
To handle two-dimensional vectors, create instances of Vector2d
class at first.
5.1.1. Instantiation
Vector2d
class needs initialPoint
and components
as arguments.
Now, you can handle the two-dimensional vectors $a
and $b
.
5.1.2. Length (Magnitude)
To get the length (magnitude) of the vector, use length()
or magnitude()
methods.
Note: Actually, the
length()
method is an alias of themagnitude()
method.
5.1.3. Components
To get components of the vector, use the components()
method.
5.1.4. Initial and Terminal points
To get initial point or terminal poinnt, use the initialPoint()
or terminalPoint()
method.
5.1.5. Unit vector
To get unit vectors, use the unitVector()
method.
This method returns a new vector, and the original vector remains in its original state.
5.1.6. Multiplying by a real number
To multiply the vector by a real number, use the multiply()
method.
This method returns a new vector, and the original vector remains in its original state.
5.1.7. Adding two vectors
To perform (vector a) + (vector b)
, use the add()
method of the former vector ($a
in this case), and set the latter vector as a parameter of this method ($b
in this case).
This returns a new vector that has the initial point
of the former vector and whose components are the sum of the components of both vectors.
The former vector ($a
in this case) and the latter vector ($b
in this case) remain in their original state.
5.1.8. Subtracting one vector from another
To perform (vector a) - (vector b)
, use the subtract()
method of the former vector ($a
in this case), and set the latter vector as a parameter of this method ($b
in this case).
This returns a new vector that has the initial point
of the former vector and whose compoents are the components obtained by subtracting the latter vector from the former vector.
The former vector ($a
in this case) and the latter vector ($b
in this case) remain in their original state.
5.1.9. Dot product
To get the dot product of two vectors, use dotProduct()
method.
The result is the same for the methods in either vector.
5.1.10. Cosine
To get the cosine of the angle between two vectors, use cos()
method.
The result is the same for the methods in either vector.
If the argument is omitted, the cosine of the angle with the x-axis is returned.
5.1.11. Angle
To get the angle between two vectors, use degrees()
or radian()
method.
The result is the same for the methods in either vector.
If the argument is omitted, the angle made with the x-axis is returned.
5.1.12. Rotation
To get a rotated vector, use rotate()
method.
Pass degrees to rotate as an argument.
5.2. Three-dimensional Vectors
To handle three-dimensional vectors, create instances of Vector3d
class at first.
5.2.1 Instantiation
Vector3d
class needs initialPoint
and components
as arguments.
Now, you can handle the three-dimensional vectors $a
and $b
.
5.2.2. Length (Magnitude)
To get the length (magnitude) of the vector, use length()
or magnitude()
methods.
Note: Actually, the
length()
method is an alias of themagnitude()
method.
5.2.3. Components
To get components of the vector, use the components()
method.
5.2.4. Initial and Terminal Points
To get initial point or terminal poinnt, use the initialPoint()
or terminalPoint()
methods.
5.2.5. Unit Vector
To get unit vectors, use the unitVector()
method.
This method returns a new vector, and the original vector remains in its original state.
5.2.6. Multiplying by a real number
To multiply the vector by a real number, use the multiply()
method.
This method returns a new vector, and the original vector remains in its original state.
5.2.7. Adding two vectors
To perform (vector a) + (vector b)
, use the add()
method of the former vector ($a
in this case), and set the latter vector as a parameter of this method ($b
in this case).
This returns a new vector that has the initial point of the former vector and whose components are the sum of the components of both vectors.
The former vector ($a
in this case) and the latter vector ($b
in this case) remain in their original state.
5.2.8. Subtracting one vector from another
To perform (vector a) - (vector b)
, use the subtract()
method of the former vector ($a
in this case), and set the latter vector as a parameter of this method ($b
in this case).
This returns a new vector that has the initial point of the former vector and whose compoents are the components obtained by subtracting the latter vector from the former vector.
The former vector ($a
in this case) and the latter vector ($b
in this case) remain in their original state.
5.2.9. Dot product
To get the dot product of two vectors, use dotProduct()
method.
The result is the same for the methods in either vector.
5.2.10. Cross product
To get the cross product of two vectors, use crossProduct()
method.
The result is the same for the methods in either vector.
This method returns a new vector, and the both original vectors remain in their original state.
5.2.11. Cosine
To get the cosine of the angle between two vectors, use cos()
method.
The result is the same for the methods in either vector.
If the argument is omitted, the cosine of the angle with the xy-plane is returned.
5.2.12. Angle
To get the angle between two vectors, use degrees()
or radian()
method.
The result is the same for the methods in either vector.
If the argument is omitted, the angle made with the xy-plane is returned.
5.2.13. Rotation
To get a rotated vector around a vector, use rotate()
method.
Pass a vector of rotation axis, and degrees to rotate as an argument.
This method returns a new vector, and the both original vectors remain in their original state.
6. Examples
- UsingVector2d.php
- ApplicationVector2d.php
- RotateVector2d.php
- UsingVector3d.php
7. LICENSE
MIT
Copyright 2024 - 2025 macocci7.