Current section

Files

Jump to
vec src vec@vec2f.erl
Raw

src/vec@vec2f.erl

-module(vec@vec2f).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/vec/vec2f.gleam").
-export([loosely_equals/3, min/2, max/2, clamp/3, ceiling/1, floor/1, round/1, truncate/1, to_precision/2, absolute_value/1, negate/1, modulo/2, divide/2, add/2, sum/1, multiply/2, product/1, subtract/2, length_squared/1, length/1, compare_length/2, loosely_compare_length/3, distance_squared/2, distance/2, compare_distance/3, loosely_compare_distance/4, scale/2, normalize/1, direction/2, cross/2, dot/2, project/2, slide/2, reflect/2, mirror/2, angle/2, rotate/2, anchor_position/3, anchor_rotation/3, decoder/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/vec/vec2f.gleam", 40).
?DOC(
" Checks for equality of two vectors within a tolerance, returning an `Bool`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4)\n"
" |> loosely_equals(Vec2(1.25, -3.43), tolerating: 0.1)\n"
" // -> True\n"
" ```\n"
).
-spec loosely_equals(vec@vec2:vec2(float()), vec@vec2:vec2(float()), float()) -> boolean().
loosely_equals(A, B, Tolerance) ->
case begin
_pipe = A,
vec@vec2:map2(
_pipe,
B,
fun(A@1, B@1) -> gleam@float:loosely_equals(A@1, B@1, Tolerance) end
)
end of
{vec2, true, true} ->
true;
_ ->
false
end.
-file("src/vec/vec2f.gleam", 60).
?DOC(
" Compares two vectors, returning the smaller of the two.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" min(Vec2(1.2, -3.4), Vec2(1.0, 2.1))\n"
" // -> Vec2(1.0, -3.4)\n"
" ```\n"
).
-spec min(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
min(A, B) ->
_pipe = A,
vec@vec2:map2(_pipe, B, fun gleam@float:min/2).
-file("src/vec/vec2f.gleam", 73).
?DOC(
" Compares two vectors, returning the larger of the two.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" max(Vec2(1.2, -3.4), Vec2(1.4, -9.3))\n"
" // -> Vec2(1.4, -3.4)\n"
" ```\n"
).
-spec max(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
max(A, B) ->
_pipe = A,
vec@vec2:map2(_pipe, B, fun gleam@float:max/2).
-file("src/vec/vec2f.gleam", 26).
?DOC(
" Returns a new vector with all components clamped between a lower and upper\n"
" bound.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> clamp(\n"
" min: Vec2(1.0, 2.1),\n"
" max: Vec2(1.4, 18.2),\n"
" )\n"
" // -> Vec2(1.2, 2.1)\n"
" ```\n"
).
-spec clamp(
vec@vec2:vec2(float()),
vec@vec2:vec2(float()),
vec@vec2:vec2(float())
) -> vec@vec2:vec2(float()).
clamp(Vector, Min_bound, Max_bound) ->
_pipe = Vector,
_pipe@1 = min(_pipe, Max_bound),
max(_pipe@1, Min_bound).
-file("src/vec/vec2f.gleam", 87).
?DOC(
" Returns a new vector with all elements rounded to the next highest whole\n"
" number as a `Float`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.6) |> ceiling()\n"
" // -> Vec2(2.0, -3.0)\n"
" ```\n"
).
-spec ceiling(vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
ceiling(Vector) ->
_pipe = Vector,
vec@vec2:map(_pipe, fun math:ceil/1).
-file("src/vec/vec2f.gleam", 101).
?DOC(
" Returns a new vector with all elements rounded to the next lowest whole\n"
" number as an `Float`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.6) |> floor()\n"
" // -> Vec2(1.0, -4.0)\n"
" ```\n"
).
-spec floor(vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
floor(Vector) ->
_pipe = Vector,
vec@vec2:map(_pipe, fun math:floor/1).
-file("src/vec/vec2f.gleam", 115).
?DOC(
" Returns a new vector with all elements rounded to the nearest whole number\n"
" as an `Int`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.6) |> round()\n"
" // -> Vec2(1, -4)\n"
" ```\n"
).
-spec round(vec@vec2:vec2(float())) -> vec@vec2:vec2(integer()).
round(Vector) ->
_pipe = Vector,
vec@vec2:map(_pipe, fun erlang:round/1).
-file("src/vec/vec2f.gleam", 128).
?DOC(
" Returns a new vector with all elements truncated as an `Int`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2323232827383238, -3.656565) |> truncate()\n"
" // -> Vec2(1, -3)\n"
" ```\n"
).
-spec truncate(vec@vec2:vec2(float())) -> vec@vec2:vec2(integer()).
truncate(Vector) ->
_pipe = Vector,
vec@vec2:map(_pipe, fun erlang:trunc/1).
-file("src/vec/vec2f.gleam", 146).
?DOC(
" Returns a new vector with all elements converted to a given precision.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(2.43434348473, -3.656565) |> to_precision(2)\n"
" // -> Vec2(2.43, -3.66)\n"
" ```\n"
"\n"
" ```gleam\n"
" Vec2(547_890.453444, -3.656565) |> to_precision(-3)\n"
" // -> Vec2(548_000.0, 0.0)\n"
" ```\n"
).
-spec to_precision(vec@vec2:vec2(float()), integer()) -> vec@vec2:vec2(float()).
to_precision(Vector, Precision) ->
_pipe = Vector,
vec@vec2:map(
_pipe,
fun(_capture) -> gleam@float:to_precision(_capture, Precision) end
).
-file("src/vec/vec2f.gleam", 159).
?DOC(
" Returns a new vector with all elements in absolute values.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> absolute_value()\n"
" // -> Vec2(1.2, 3.4)\n"
" ```\n"
).
-spec absolute_value(vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
absolute_value(Vector) ->
_pipe = Vector,
vec@vec2:map(_pipe, fun gleam@float:absolute_value/1).
-file("src/vec/vec2f.gleam", 172).
?DOC(
" Returns a new vector with all elements negated.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> negate()\n"
" // -> Vec2(-1.2, 3.4)\n"
" ```\n"
).
-spec negate(vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
negate(Vector) ->
_pipe = Vector,
vec@vec2:map(_pipe, fun gleam@float:negate/1).
-file("src/vec/vec2f.gleam", 226).
?DOC(
" Returns the modulo of the inputs as a `Result`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(13.3, 13.3) |> modulo(Vec2(3.3, -3.3))\n"
" // -> Ok(Vec2(0.1, -3.2))\n"
" ```\n"
"\n"
" ```gleam\n"
" Vec2(-13.3, -13.3) |> modulo(Vec2(3.3, -3.3))\n"
" // -> Ok(Vec2(3.2, -0.1))\n"
" ```\n"
).
-spec modulo(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> {ok,
vec@vec2:vec2(float())} |
{error, nil}.
modulo(Dividend, Divisor) ->
_pipe = Dividend,
_pipe@1 = vec@vec2:map2(_pipe, Divisor, fun gleam@float:modulo/2),
vec@vec2:result(_pipe@1).
-file("src/vec/vec2f.gleam", 244).
?DOC(
" Returns division of the inputs as a `Result`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> divide(Vec2(2.0, 0.5))\n"
" // -> Ok(Vec2(0.6, -6.8))\n"
" ```\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> divide(Vec2(0.0, 0.5))\n"
" // -> Error(Nil)\n"
" ```\n"
).
-spec divide(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> {ok,
vec@vec2:vec2(float())} |
{error, nil}.
divide(Dividend, Divisor) ->
_pipe = Dividend,
_pipe@1 = vec@vec2:map2(_pipe, Divisor, fun gleam@float:divide/2),
vec@vec2:result(_pipe@1).
-file("src/vec/vec2f.gleam", 257).
?DOC(
" Adds two vectors together.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> add(Vec2(2.1, 4.5))\n"
" // -> Vec2(3.3, 1.1)\n"
" ```\n"
).
-spec add(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
add(A, B) ->
_pipe = A,
vec@vec2:map2(_pipe, B, fun gleam@float:add/2).
-file("src/vec/vec2f.gleam", 190).
?DOC(
" Sums a list of vectors.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" [\n"
" Vec2(1.2, -3.4),\n"
" Vec2(2.1, 4.5),\n"
" Vec2(3.3, 0.0),\n"
" ]\n"
" |> sum()\n"
" // -> Vec2(6.6, 1.1)\n"
" ```\n"
).
-spec sum(list(vec@vec2:vec2(float()))) -> vec@vec2:vec2(float()).
sum(Vectors) ->
_pipe = Vectors,
gleam@list:fold(_pipe, vec@vec2:splat(+0.0), fun add/2).
-file("src/vec/vec2f.gleam", 270).
?DOC(
" Multiplies two vectors together.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> multiply(Vec2(2.1, -1.0))\n"
" // -> Vec2(2.52, 3.4)\n"
" ```\n"
).
-spec multiply(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
multiply(A, B) ->
_pipe = A,
vec@vec2:map2(_pipe, B, fun gleam@float:multiply/2).
-file("src/vec/vec2f.gleam", 208).
?DOC(
" Multiplies a list of vectors and returns the product.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" [\n"
" Vec2(1.2, -3.4),\n"
" Vec2(2.1, -1.0),\n"
" Vec2(3.2, 2.0),\n"
" ]\n"
" |> product()\n"
" // -> Vec2(8.064, 6.8)\n"
" ```\n"
).
-spec product(list(vec@vec2:vec2(float()))) -> vec@vec2:vec2(float()).
product(Vectors) ->
_pipe = Vectors,
gleam@list:fold(_pipe, vec@vec2:splat(1.0), fun multiply/2).
-file("src/vec/vec2f.gleam", 283).
?DOC(
" Subtracts one vector from another.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> subtract(Vec2(0.7, -4.5))\n"
" // -> Vec2(0.5, 1.1)\n"
" ```\n"
).
-spec subtract(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
subtract(A, B) ->
_pipe = A,
vec@vec2:map2(_pipe, B, fun gleam@float:subtract/2).
-file("src/vec/vec2f.gleam", 296).
?DOC(
" Returns the squared length (squared magnitude) of the vector.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> length_squared()\n"
" // -> 13.0\n"
" ```\n"
).
-spec length_squared(vec@vec2:vec2(float())) -> float().
length_squared(Vector) ->
_pipe = Vector,
_pipe@1 = vec@vec2:to_list(_pipe),
_pipe@2 = gleam@list:map(_pipe@1, fun(Element) -> Element * Element end),
gleam@float:sum(_pipe@2).
-file("src/vec/vec2f.gleam", 312).
?DOC(
" Returns the length (magnitude) of the vector.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> length()\n"
" // -> 3.61\n"
" ```\n"
).
-spec length(vec@vec2:vec2(float())) -> float().
length(Vector) ->
Length@1 = case begin
_pipe = Vector,
_pipe@1 = length_squared(_pipe),
gleam@float:square_root(_pipe@1)
end of
{ok, Length} -> Length;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"vec/vec2f"/utf8>>,
function => <<"length"/utf8>>,
line => 313,
value => _assert_fail,
start => 6405,
'end' => 6478,
pattern_start => 6416,
pattern_end => 6426})
end,
Length@1.
-file("src/vec/vec2f.gleam", 328).
?DOC(
" Compares two vector's lengths, returning an `Order`:\n"
" `Lt` for lower than, `Eq` for equals, or `Gt` for greater than.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" compare_length(Vec2(1.2, -3.4), Vec2(1.0, 2.1))\n"
" // -> Gt\n"
" ```\n"
).
-spec compare_length(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> gleam@order:order().
compare_length(A, B) ->
gleam@float:compare(
begin
_pipe = A,
length_squared(_pipe)
end,
begin
_pipe@1 = B,
length_squared(_pipe@1)
end
).
-file("src/vec/vec2f.gleam", 342).
?DOC(
" Compares two vector's lengths within a tolerance, returning an `Order`:\n"
" `Lt` for lower than, `Eq` for equals, or `Gt` for greater than.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" loosely_compare_length(Vec2(1.2, -3.4), Vec2(-1.25, 3.43), tolerating: 0.5)\n"
" // -> Eq\n"
" ```\n"
).
-spec loosely_compare_length(
vec@vec2:vec2(float()),
vec@vec2:vec2(float()),
float()
) -> gleam@order:order().
loosely_compare_length(A, B, Tolerance) ->
gleam@float:loosely_compare(
begin
_pipe = A,
length_squared(_pipe)
end,
begin
_pipe@1 = B,
length_squared(_pipe@1)
end,
Tolerance
).
-file("src/vec/vec2f.gleam", 359).
?DOC(
" Returns the squared distance between two vectors.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> distance_squared(Vec2(1.0, 2.1))\n"
" // -> 30.29\n"
" ```\n"
).
-spec distance_squared(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> float().
distance_squared(A, B) ->
_pipe = A,
_pipe@1 = vec@vec2:map2(_pipe, B, fun gleam@float:subtract/2),
length_squared(_pipe@1).
-file("src/vec/vec2f.gleam", 372).
?DOC(
" Returns the distance between two vectors.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> distance(Vec2(1.0, 2.1))\n"
" // -> 5.5\n"
" ```\n"
).
-spec distance(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> float().
distance(A, B) ->
Distance@1 = case begin
_pipe = distance_squared(A, B),
gleam@float:square_root(_pipe)
end of
{ok, Distance} -> Distance;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"vec/vec2f"/utf8>>,
function => <<"distance"/utf8>>,
line => 373,
value => _assert_fail,
start => 7833,
'end' => 7904,
pattern_start => 7844,
pattern_end => 7856})
end,
Distance@1.
-file("src/vec/vec2f.gleam", 388).
?DOC(
" Compares two vector's distances to a vector, returning an `Order`:\n"
" `Lt` for lower than, `Eq` for equals, or `Gt` for greater than.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" compare_distance(Vec2(1.2, -3.4), Vec2(1.0, 2.1), Vec2(-2.5, 6.7))\n"
" // -> Gt\n"
" ```\n"
).
-spec compare_distance(
vec@vec2:vec2(float()),
vec@vec2:vec2(float()),
vec@vec2:vec2(float())
) -> gleam@order:order().
compare_distance(A, B, Vector) ->
gleam@float:compare(
begin
_pipe = A,
distance_squared(_pipe, Vector)
end,
begin
_pipe@1 = B,
distance_squared(_pipe@1, Vector)
end
).
-file("src/vec/vec2f.gleam", 408).
?DOC(
" Compares two vector's distances to a vector within a tolerance, returning\n"
" an `Order`:\n"
" `Lt` for lower than, `Eq` for equals, or `Gt` for greater than.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" loosely_compare_distance(\n"
" Vec2(1.2, -3.4),\n"
" Vec2(1.25, -3.43),\n"
" Vec2(-2.5, 6.7),\n"
" tolerating: 1.0,\n"
" )\n"
" // -> Eq\n"
" ```\n"
).
-spec loosely_compare_distance(
vec@vec2:vec2(float()),
vec@vec2:vec2(float()),
vec@vec2:vec2(float()),
float()
) -> gleam@order:order().
loosely_compare_distance(A, B, Vector, Tolerance) ->
gleam@float:loosely_compare(
begin
_pipe = A,
distance_squared(_pipe, Vector)
end,
begin
_pipe@1 = B,
distance_squared(_pipe@1, Vector)
end,
Tolerance
).
-file("src/vec/vec2f.gleam", 430).
?DOC(
" Returns a new vector containing the elements multiplies by `scalar`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> scale(2.5)\n"
" // -> Vec2(3.0, -8.5)\n"
" ```\n"
).
-spec scale(vec@vec2:vec2(float()), float()) -> vec@vec2:vec2(float()).
scale(Vector, Scalar) ->
_pipe = Vector,
vec@vec2:map(
_pipe,
fun(_capture) -> gleam@float:multiply(_capture, Scalar) end
).
-file("src/vec/vec2f.gleam", 443).
?DOC(
" Normalize the vector.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> normalize()\n"
" // -> Vec2(0.33, -0.94)\n"
" ```\n"
).
-spec normalize(vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
normalize(Vector) ->
_pipe = Vector,
scale(
_pipe,
case begin
_pipe@1 = Vector,
length(_pipe@1)
end of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> 1.0 / Gleam@denominator
end
).
-file("src/vec/vec2f.gleam", 456).
?DOC(
" Returns a normalized vector pointing from `a` to `b`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> direction(Vec2(1.0, 2.1))\n"
" // -> Vec2(-0.04, 1.0)\n"
" ```\n"
).
-spec direction(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
direction(A, B) ->
_pipe = B,
_pipe@1 = subtract(_pipe, A),
normalize(_pipe@1).
-file("src/vec/vec2f.gleam", 469).
?DOC(
" Returns the cross product of two vectors.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> cross(Vec2(1.0, 2.1))\n"
" // -> 5.92\n"
" ```\n"
).
-spec cross(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> float().
cross(A, B) ->
(erlang:element(2, A) * erlang:element(3, B)) - (erlang:element(2, B) * erlang:element(
3,
A
)).
-file("src/vec/vec2f.gleam", 482).
?DOC(
" Returns the dot product of two vectors.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> dot(Vec2(1.0, 2.1))\n"
" // -> -5.94\n"
" ```\n"
).
-spec dot(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> float().
dot(A, B) ->
_pipe = A,
_pipe@1 = multiply(_pipe, B),
_pipe@2 = vec@vec2:to_list(_pipe@1),
gleam@float:sum(_pipe@2).
-file("src/vec/vec2f.gleam", 495).
?DOC(
" Returns the projection of a vector on another vector.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> project(Vec2(1.0, 2.1))\n"
" // -> Vec2(-1.1, -2.31)\n"
" ```\n"
).
-spec project(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
project(A, B) ->
_pipe = B,
scale(_pipe, case dot(B, B) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> dot(A, B) / Gleam@denominator
end).
-file("src/vec/vec2f.gleam", 509).
?DOC(
" Returns a new vector resulting from sliding this vector along a plane\n"
" defined by the given normal vector.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> slide(Vec2(1.0, 2.1))\n"
" // -> Vec2(2.3, -1.09)\n"
" ```\n"
).
-spec slide(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
slide(A, B) ->
_pipe = A,
subtract(
_pipe,
begin
_pipe@1 = A,
project(_pipe@1, B)
end
).
-file("src/vec/vec2f.gleam", 523).
?DOC(
" Returns the reflection of a vector through a plane defined by the given\n"
" normal vector.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> reflect(Vec2(1.0, 2.1))\n"
" // -> Vec2(-3.4, -1.21)\n"
" ```\n"
).
-spec reflect(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
reflect(Vector, Normal) ->
_pipe = Vector,
_pipe@1 = project(_pipe, Normal),
_pipe@2 = scale(_pipe@1, 2.0),
subtract(_pipe@2, Vector).
-file("src/vec/vec2f.gleam", 537).
?DOC(
" Returns the mirror of a vector through a plane defined by the given normal\n"
" vector.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> mirror(Vec2(1.0, 2.1))\n"
" // -> Vec2(3.34, 1.21)\n"
" ```\n"
).
-spec mirror(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> vec@vec2:vec2(float()).
mirror(Vector, Normal) ->
_pipe = Vector,
_pipe@1 = reflect(_pipe, Normal),
negate(_pipe@1).
-file("src/vec/vec2f.gleam", 550).
?DOC(
" Returns the angle (in radians) between two vectors.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> angle(Vec2(1.0, 2.1))\n"
" // -> 2.36\n"
" ```\n"
).
-spec angle(vec@vec2:vec2(float()), vec@vec2:vec2(float())) -> float().
angle(A, B) ->
Angle@1 = case begin
_pipe = dot(normalize(A), normalize(B)),
_pipe@1 = gleam@float:clamp(_pipe, -1.0, 1.0),
gleam_community@maths:acos(_pipe@1)
end of
{ok, Angle} -> Angle;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"vec/vec2f"/utf8>>,
function => <<"angle"/utf8>>,
line => 551,
value => _assert_fail,
start => 11687,
'end' => 11787,
pattern_start => 11698,
pattern_end => 11707})
end,
Angle@1.
-file("src/vec/vec2f.gleam", 566).
?DOC(
" Rotate a vector by an angle (in radians).\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4) |> rotate(maths.pi() *. 0.25)\n"
" // -> Vec2(3.25, -1.56)\n"
" ```\n"
).
-spec rotate(vec@vec2:vec2(float()), float()) -> vec@vec2:vec2(float()).
rotate(Vector, Angle) ->
Cos_angle = gleam_community@maths:cos(Angle),
Sin_angle = gleam_community@maths:sin(Angle),
{vec2,
(erlang:element(2, Vector) * Cos_angle) - (erlang:element(3, Vector) * Sin_angle),
(erlang:element(2, Vector) * Sin_angle) + (erlang:element(3, Vector) * Cos_angle)}.
-file("src/vec/vec2f.gleam", 589).
?DOC(
" Return the equivalent of `vector |> subtract(position) |> fun() |> add(position)`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4)\n"
" |> anchor_position(\n"
" Vec2(1.0, 2.1),\n"
" rotate(_, maths.pi() *. 0.25),\n"
" )\n"
" // -> Vec2(5.03, -1.65)\n"
" ```\n"
).
-spec anchor_position(
vec@vec2:vec2(float()),
vec@vec2:vec2(float()),
fun((vec@vec2:vec2(float())) -> vec@vec2:vec2(float()))
) -> vec@vec2:vec2(float()).
anchor_position(Vector, Position, Fun) ->
_pipe = Vector,
_pipe@1 = subtract(_pipe, Position),
_pipe@2 = Fun(_pipe@1),
add(_pipe@2, Position).
-file("src/vec/vec2f.gleam", 610).
?DOC(
" Return the equivalent of `vector |> rotate(float.negate(angle)) |> fun() |> rotate(angle)`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" Vec2(1.2, -3.4)\n"
" |> anchor_rotation(\n"
" maths.pi() *. 0.25,\n"
" add(_, Vec2(6.0, 9.0)),\n"
" )\n"
" // -> Vec2(-0.92, 7.21)\n"
" ```\n"
).
-spec anchor_rotation(
vec@vec2:vec2(float()),
float(),
fun((vec@vec2:vec2(float())) -> vec@vec2:vec2(float()))
) -> vec@vec2:vec2(float()).
anchor_rotation(Vector, Angle, Fun) ->
_pipe = Vector,
_pipe@1 = rotate(_pipe, gleam@float:negate(Angle)),
_pipe@2 = Fun(_pipe@1),
rotate(_pipe@2, Angle).
-file("src/vec/vec2f.gleam", 620).
?DOC(
" A decoder that decodes 2D float vectors where all elements are decoded with\n"
" a given decoder.\n"
).
-spec decoder() -> gleam@dynamic@decode:decoder(vec@vec2:vec2(float())).
decoder() ->
gleam@dynamic@decode:field(
<<"x"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(X) ->
gleam@dynamic@decode:field(
<<"y"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_float/1},
fun(Y) -> gleam@dynamic@decode:success({vec2, X, Y}) end
)
end
).