Graphmath.Vec2
This is the 2D mathematics library for graphmath.
This submodule handles vectors stored as a tuple.
Summary
add(a, b) |
|
create() |
|
create(vec) |
|
create(x, y) |
|
dot(a, b) |
|
length(a) |
|
length_manhattan(a) |
|
length_squared(a) |
|
lerp(a, b, t) |
|
multiply(a, b) |
|
near(a, b, distance) |
|
normalize(a) |
|
perp(a) |
|
perp_prod(a, b) |
|
project(a, b) |
|
rotate(a, theta) |
|
scale(a, scale) |
|
subtract(a, b) |
|
Functions
Specs:
add( a, b)
adds a vec2 (a) to a vec2 (b).
It returns a tuple of the form { ax + bx, ay + by }.
add( a, b )
adds two vec2
s.
a
is the first vec2
.
b
is the second vec2
.
It returns a vec2
of the form { ax + bx, ay + by }.
Specs:
- create :: vec2
create()
creates a zero vec2.
It will return a tuple of the form {0.0,0.0}.
create()
creates a zeroed vec2
.
It takes no arguments.
It returns a vec2
of the form { 0.0, 0.0 }
.
Specs:
- create([float]) :: vec2
create(vec)
creates a vec2
from a list of 2 or more floats.
vec
is a list of 2 or more floats.
It returns a vec2
of the form {x,y}
, where x
and y
are the first three elements in vec
.
Specs:
- create(float, float) :: vec2
create(x,y)
creates a vec2
of value (x,y).
x
is the first element of the vec3
to be created.
y
is the second element of the vec3
to be created.
It returns a vec2
of the form {x,y}
.
Specs:
dot( a, b )
finds the dot (inner) product of one vec2
with another vec2
.
a
is the first vec2
.
b
is the second vec2
.
It returns a float of the value (axbx + ayby ).
Specs:
- length(vec2) :: float
length(a)
finds the length (Eucldiean or L2 norm) of a vec2
.
a
is the vec2
to find the length of.
It returns a float of the value (sqrt( ax2 + ay2)).
Specs:
- length_manhattan(vec2) :: float
length_manhattan(a)
finds the Manhattan (L1 norm) length of a vec2
.
a
is the vec2
to find the Manhattan length of.
It returns a float of the value (ax + ay).
The Manhattan length is the sum of the components.
Specs:
- length_squared(vec2) :: float
length_squared(a)
finds the square of the length of a vec2 (a).
In many cases, this is sufficient for comparisions and avaoids a sqrt.
It returns a float of the value (axax + ayay).
length_squared(a)
finds the square of the length of a vec2
.
a
is the vec2
to find the length squared of.
It returns a float of the value ax2 + ay2.
In many cases, this is sufficient for comparisons and avoids a square root.
Specs:
lerp(a,b,t)
is used to linearly interpolate between two given vectors a and b along an interpolant t.
The interpolant t
is on the domain [0,1]. Behavior outside of that is undefined.
lerp(a,b,t)
linearly interpolates between one vec2
and another vec2
along an interpolant.
a
is the starting vec2
.
b
is the ending vec2
.
t
is the interpolant float, on the domain [0,1].
It returns a vec2
of the form (1-t)a - (t)b.
The interpolant t
is on the domain [0,1]. Behavior outside of that is undefined.
Specs:
multiply( a, b)
mulitplies element-wise a vec2 (a) by a vec2 (b).
It returns a tuple of the form { axbx, ayby }.
multiply( a, b )
multiplies element-wise a vec2
by another vec2
.
a
is the vec2
multiplicand.
b
is the vec2
multiplier.
It returns a vec2
of the form { axbx, ayby }.
Specs:
near(a,b, distance)
checks whether two vec2
s are within a certain distance of each other.
a
is the first vec2
.
b
is the second vec2
.
distance
is the distance between them as a float.
Specs:
normalize(a)
finds the unit vector with the same direction as a vec2
.
a
is the vec2
to be normalized.
It returns a vec2
of the form {normx, normy}
.
This is done by dividing each component by the vector’s magnitude.
Specs:
perp(a)
creates a vector perpendicular to another vector a
.
a
is the vec2
to be perpindicular to.
This returns a vec2
perpindicular to a
, to the right of the original a
.
Specs:
perp_prod( a, b )
finds the perpindicular product of one vec2
with another vec2
.
a
is the first vec2
.
b
is the second vec2
.
The perpindicular product is the magnitude of the cross-product between the two vectors.
It returns a float of the value (axby - bxay).
Specs:
project(a,b)
projects one vec2
onto another vec2
.
a
is the first vec2
.
b
is the second vec2
.
This returns a vec2
representing the image of a
in the direction of b
.
Specs:
rotate(a,theta)
rotates a vec2
CCW about the +Z axis.
a
is the vec2
to rotate.
theta
is the number of radians to rotate by as a float.
This returns a vec2
.
Specs:
scale( a, scale )
uniformly scales a vec2
.
a
is the vec2
to be scaled.
scale
is the float to scale each element of a
by.
It returns a tuple of the form { axscale, ayscale }.