View Source Graphmath.Vec2 (graphmath v2.6.0)

This is the 2D mathematics.

This submodule handles vectors stored as tuples of floats ex: {1.0, 2.0}.

Summary

Functions

add( a, b) adds a vec2 (a) to a vec2 (b).

chebyshev_distance(a,b) returns the Chebyshev distance between two points a and b.

create() creates a zero vec2.

create(vec) creates a vec2 from a list of 2 or more floats.

create(x,y) creates a vec2 of value (x,y).

dot( a, b ) finds the dot (inner) product of one vec2 with another vec2.

equal(a, b) checks to see if two vec2s a and b are equivalent.

equal(a, b, eps) checks to see if two vec2s a and b are equivalent within some tolerance.

length(a) finds the length (Eucldiean or L2 norm) of a vec2.

length_manhattan(a) finds the Manhattan (L1 norm) length of a vec2.

length_squared(a) finds the square of the length of a vec2 (a).

lerp(a,b,t) is used to linearly interpolate between two given vectors a and b along an interpolant t.

minkowski_distance(a,b,order) returns the Minkowski distance between two points a and b of order order.

multiply( a, b) mulitplies element-wise a vec2 (a) by a vec2 (b).

near(a,b, distance) checks whether two vec2s are within a certain distance of each other.

negate(v) creates a vector whose elements are opposite in sign to v.

normalize(a) finds the unit vector with the same direction as a vec2.

p_norm(v,order) returns the P-norm of vector v of order order.

perp(a) creates a vector perpendicular to another vector a.

perp_prod( a, b ) finds the perpindicular product of one vec2 with another vec2.

project(a,b) projects one vec2 onto another vec2.

random_box() generates a point on or inside the unit box [0,1]x[0,1].

random_circle() generates a point on the unit circle.

random_disc() generates a point on or inside the unit circle using the method here.

rotate(a,theta) rotates a vec2 CCW about the +Z axis.

scale( a, scale ) uniformly scales a vec2.

subtract(a, b ) subtracts one vec2 from another vec2.

weighted_sum(a, v1, b, v2) returns the sum of vectors v1 and v2 having been scaled by a and b, respectively.

Types

@type vec2() :: {float(), float()}

Functions

@spec add(vec2(), vec2()) :: vec2()

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 vec2s.

a is the first vec2.

b is the second vec2.

It returns a vec2 of the form { a<sub>x</sub> + b<sub>x</sub>, a<sub>y</sub> + b<sub>y</sub> }.

Link to this function

chebyshev_distance(arg1, arg2)

View Source
@spec chebyshev_distance(vec2(), vec2()) :: number()

chebyshev_distance(a,b) returns the Chebyshev distance between two points a and b.

@spec 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 }.

@spec 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.

@spec 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}.

@spec dot(vec2(), vec2()) :: float()

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 (a<sub>x</sub>b<sub>x</sub> + a<sub>y</sub>b<sub>y</sub> ).

@spec equal(vec2(), vec2()) :: boolean()

equal(a, b) checks to see if two vec2s a and b are equivalent.

a is the vec2.

b is the vec2.

It returns true if the vectors have equal elements.

Note that due to precision issues, you may want to use equal/3 instead.

@spec equal(vec2(), vec2(), float()) :: boolean()

equal(a, b, eps) checks to see if two vec2s a and b are equivalent within some tolerance.

a is the vec2.

b is the vec2.

eps is the tolerance, a float.

It returns true if the vectors have equal elements within some tolerance.

@spec 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( a<sub>x</sub><sup>2</sup> + a<sub>y</sub><sup>2</sup>)).

@spec 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 (a<sub>x</sub> + a<sub>y</sub>).

The Manhattan length is the sum of the components.

@spec 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 a<sub>x</sub><sup>2</sup> + a<sub>y</sub><sup>2</sup>.

In many cases, this is sufficient for comparisons and avoids a square root.

@spec lerp(vec2(), vec2(), float()) :: vec2()

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.

Link to this function

minkowski_distance(arg1, arg2, order)

View Source
@spec minkowski_distance(vec2(), vec2(), number()) :: number()

minkowski_distance(a,b,order) returns the Minkowski distance between two points a and b of order order.

Order 1 is equivalent to manhattan distance, 2 to Euclidean distance, otherwise all bets are off.

@spec multiply(vec2(), vec2()) :: vec2()

multiply( a, b) mulitplies element-wise a vec2 (a) by a vec2 (b).

It returns a tuple of the form { ax*bx, ay*by }.

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 { a<sub>x</sub>b<sub>x</sub>, a<sub>y</sub>b<sub>y</sub> }.

Link to this function

near(arg1, arg2, distance)

View Source
@spec near(vec2(), vec2(), float()) :: boolean()

near(a,b, distance) checks whether two vec2s 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.

@spec negate(vec2()) :: vec2()

negate(v) creates a vector whose elements are opposite in sign to v.

@spec normalize(vec2()) :: vec2()

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.

@spec p_norm(vec2(), number()) :: number()

p_norm(v,order) returns the P-norm of vector v of order order.

order needs to be greater than or equal to 1 to define a metric space.

order 1 is equivalent to manhattan distance, 2 to Euclidean distance, otherwise all bets are off.

@spec perp(vec2()) :: vec2()

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.

@spec perp_prod(vec2(), vec2()) :: float()

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 (a<sub>x</sub>b<sub>y</sub> - b<sub>x</sub>a<sub>y</sub>).

@spec project(vec2(), vec2()) :: vec2()

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.

@spec random_box() :: vec2()

random_box() generates a point on or inside the unit box [0,1]x[0,1].

@spec random_circle() :: vec2()

random_circle() generates a point on the unit circle.

It returns a vec2 with distance 1 from the origin.

@spec random_disc() :: vec2()

random_disc() generates a point on or inside the unit circle using the method here.

It returns a vec2 with distance 1 from the origin.

@spec rotate(vec2(), float()) :: vec2()

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.

@spec scale(vec2(), float()) :: vec2()

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 { a<sub>x</sub>scale, a<sub>y</sub>scale }.

@spec subtract(vec2(), vec2()) :: vec2()

subtract(a, b ) subtracts one vec2 from another vec2.

a is the vec2 minuend.

b is the vec2 subtrahend.

It returns a vec2 of the form { a<sub>x</sub> - b<sub>x</sub>, a<sub>y</sub> - b<sub>y</sub> }.

(the terminology was found here).

Link to this function

weighted_sum(a, arg1, b, arg2)

View Source
@spec weighted_sum(number(), vec2(), number(), vec2()) :: vec2()

weighted_sum(a, v1, b, v2) returns the sum of vectors v1 and v2 having been scaled by a and b, respectively.