View Source Scurry.Vector (Scurry v2.0.0)
Functions to work on 2D vectors.
Vectors are represented as tuples of x and y components, {x, y}
. This
module provides basic trigonometry functions.
Link to this section Summary
Functions
Add two vectors together.
Get the angle of a vector in radians.
Get the cross product of two vectors.
This is a screen oriented degree.
This is a graph oriented degree.
Get the distance of two vectors.
Get the distance squared of two vectors.
Divide a vector by a constant.
Get the dot product of two vectors.
Get the length of a vector, aka magnitude.
Get the magnitude of a vector, aka len.
Multiply a vector by a constant.
Normalise a vector to length 1.
Calls round on a vector v
to make the vector work with wx (requires integers).
Subtract a vector from another.
Calls trunc on a vector v
to make the vector work with wx (requires integers).
Link to this section Functions
Add two vectors together.
params
Params
v1
a tuple{x, y}
of coordinates describing the first vector.v2
a tuple{x, y}
of coordinates describing the second vector.
Returns the two vectors added together.
examples
Examples
iex> Vector.add({1, 2}, {3, 4})
{4, 6}
Get the angle of a vector in radians.
params
Params
v
a tuple{x, y}
of coordinates describing the vector to normalise.
Returns the angle in radians in relationship to the x-axis.
examples
Examples
iex> Vector.angle({1, 1})
0.7853981633974483
iex> Vector.angle({0, 1})
1.5707963267948966
iex> Vector.angle({1, 0})
0.0
iex> Vector.angle({-1, 1})
2.356194490192345
iex> Vector.angle({-1, 0})
3.141592653589793
iex> Vector.angle({-1, -1})
3.9269908169872414
iex> Vector.angle({0, -1})
4.71238898038469
iex> Vector.angle({1, -1})
5.497787143782138
Get the cross product of two vectors.
params
Params
v1
a tuple{x, y}
of coordinates describing the first vector.v2
a tuple{x, y}
of coordinates describing the second vector.
examples
Examples
iex> Vector.cross({1, 2}, {3, 4})
-2
This is a screen oriented degree.
Screen degrees are in North=up (0) South=down (180) degrees. Vectors are in
x,y screen coordinate, so 1,1
= 135 degrees (down to the right/ south-east)
from 0,0
, the top left corner of 0,0
.
params
Params
v
a tuple{x, y}
of coordinates describing the vector.
Returns the vector in degrees relative to the y-axis.
examples
Examples
iex> Vector.degrees({0, -1})
0
iex> Vector.degrees({1, -1})
45
iex> Vector.degrees({1, 0})
90
iex> Vector.degrees({1, 1})
135
iex> Vector.degrees({0, 1})
180
iex> Vector.degrees({-1, 1})
225
iex> Vector.degrees({-1, 0})
270
iex> Vector.degrees({-1, -1})
315
This is a graph oriented degree.
Graph degrees are oriented as for a graph layout. Right (along the x-axis) is 0, up (along y-axis) is 90, 45 is "north-east / up and to the left".
params
Params
v
a tuple{x, y}
of coordinates describing the vector.
Returns the vector in degrees relative to the x-axis.
examples
Examples
iex> Vector.degrees_graph({1, 1})
45.0
iex> Vector.degrees_graph({0, 1})
90.0
iex> Vector.degrees_graph({1, 0})
-0.0
iex> Vector.degrees_graph({-1, 1})
135.0
iex> Vector.degrees_graph({-1, 0})
180.0
iex> Vector.degrees_graph({-1, -1})
225.0
iex> Vector.degrees_graph({0, -1})
270.0
iex> Vector.degrees_graph({1, -1})
315.0
Get the distance of two vectors.
The distance is the length of the vector between the ends (second tuple) of the vectors.
This is equivalent to the square root of the distance_squared
.
params
Params
v1
a tuple{x, y}
of coordinates describing the first vector.v2
a tuple{x, y}
of coordinates describing the second vector.
examples
Examples
iex> Vector.distance({0, 0}, {1, 1})
1.4142135623730951
iex> Vector.distance({5, 7}, {2, 3})
5.0
Get the distance squared of two vectors.
This is equivalent to the square of the distance
.
params
Params
v1
a tuple{x, y}
of coordinates describing the first vector.v2
a tuple{x, y}
of coordinates describing the second vector.
examples
Examples
iex> Vector.distance_squared({0, 0}, {1, 1})
2.0
iex> Vector.distance_squared({5, 7}, {2, 3})
25.0
Divide a vector by a constant.
params
Params
v
a tuple{x, y}
of coordinates describing the vector.c
the constant to divide by.
examples
Examples
iex> Vector.div({10, 14}, 2)
{5.0, 7.0}
Get the dot product of two vectors.
params
Params
v1
a tuple{x, y}
of coordinates describing the first vector.v2
a tuple{x, y}
of coordinates describing the second vector.
examples
Examples
iex> Vector.dot({1, 2}, {3, 4})
11
Get the length of a vector, aka magnitude.
params
Params
v
, a tuple{x, y}
of coordinates describing the vector.
Returns the length of the vector v
.
examples
Examples
iex> Vector.len({1, 1})
1.4142135623730951
iex> Vector.len({3, 4})
5.0
iex> Vector.len({12, 5})
13.0
Get the magnitude of a vector, aka len.
This is just another word for len
.
examples
Examples
iex> Vector.mag({1, 1})
1.4142135623730951
iex> Vector.mag({3, 4})
5.0
iex> Vector.mag({12, 5})
13.0
Multiply a vector by a constant.
params
Params
v
a tuple{x, y}
of coordinates describing the vector.c
the constant to multiple by.
examples
Examples
iex> Vector.mul({5, 7}, 2)
{10, 14}
Normalise a vector to length 1.
This shortens the vector by it's length, so the resulting vector w
has
len(w) == 1
, but same x/y ratio.
params
Params
v
a tuple{x, y}
of coordinates describing the vector to normalise.
examples
Examples
iex> Vector.normalise({0, 1})
{0.0, 1.0}
iex> Vector.normalise({10, 0})
{1.0, 0.0}
iex> Vector.normalise({10, 10})
{0.7071067811865475, 0.7071067811865475}
Calls round on a vector v
to make the vector work with wx (requires integers).
params
Params
v
a tuple{x, y}
of coordinates describing the vector.
Returns the vector with it's components converted to integers using round/1
.
examples
Examples
iex> Vector.round_pos({10.1, 10.9})
{10, 11}
Subtract a vector from another.
params
Params
v1
a tuple{x, y}
of coordinates describing the first vector.v2
a tuple{x, y}
of coordinates describing the second vector.
Returns the the subtraction of v2
from v1
.
examples
Examples
iex> Vector.sub({5, 7}, {1, 2})
{4, 5}
Calls trunc on a vector v
to make the vector work with wx (requires integers).
params
Params
v
a tuple{x, y}
of coordinates describing the vector.
Returns the vector with it's components converted to integers using trunc/1
.
examples
Examples
iex> Vector.trunc_pos({10.1, 10.9})
{10, 10}