-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, distance_squared/2, distance/2, 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]). -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", 38). ?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", 58). ?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", 71). ?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", 24). ?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", 85). ?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", 99). ?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", 113). ?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", 126). ?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", 144). ?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", 157). ?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", 170). ?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", 224). ?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", 242). ?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", 255). ?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", 188). ?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", 268). ?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", 206). ?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", 281). ?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", 294). ?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", 310). ?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 => <>, module => <<"vec/vec2f"/utf8>>, function => <<"length"/utf8>>, line => 311, value => _assert_fail, start => 6330, 'end' => 6403, pattern_start => 6341, pattern_end => 6351}) end, Length@1. -file("src/vec/vec2f.gleam", 325). ?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", 338). ?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 => <>, module => <<"vec/vec2f"/utf8>>, function => <<"distance"/utf8>>, line => 339, value => _assert_fail, start => 6912, 'end' => 6983, pattern_start => 6923, pattern_end => 6935}) end, Distance@1. -file("src/vec/vec2f.gleam", 353). ?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", 366). ?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", 379). ?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", 392). ?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", 405). ?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", 418). ?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", 432). ?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", 446). ?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", 460). ?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", 473). ?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 => <>, module => <<"vec/vec2f"/utf8>>, function => <<"angle"/utf8>>, line => 474, value => _assert_fail, start => 9725, 'end' => 9825, pattern_start => 9736, pattern_end => 9745}) end, Angle@1. -file("src/vec/vec2f.gleam", 489). ?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", 512). ?DOC( " Return the equivalent of `vector |> subtract(position) |> fun() |> add(position)`.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " Vec2(1.2, -3.4)\n" " |> vec2f.anchor_position(\n" " Vec2(1.0, 2.1),\n" " vec2f.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", 533). ?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" " |> vec2f.anchor_rotation(\n" " maths.pi() *. 0.25,\n" " vec2f.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).