Current section
2 Versions
Jump to
Current section
2 Versions
Compare versions
12
files changed
+1969
additions
-181
deletions
| @@ -12,6 +12,7 @@ All contained matrices are column-major; that is, in a 2x2 matrix, the first _co | |
| 12 12 | ```sh |
| 13 13 | gleam add matrix_gleam |
| 14 14 | ``` |
| 15 | + |
| 15 16 | ```gleam |
| 16 17 | import matrix/mat3f |
| 17 18 | import vec/vec3 |
| @@ -1,20 +1,18 @@ | |
| 1 1 | name = "matrix_gleam" |
| 2 | - version = "0.1.0" |
| 2 | + version = "0.2.0" |
| 3 3 | |
| 4 | - # Fill out these fields if you intend to generate HTML documentation or publish |
| 5 | - # your project to the Hex package manager. |
| 6 | - # |
| 7 4 | description = "Matrix mathematics for Gleam" |
| 8 5 | licences = ["MIT"] |
| 9 6 | repository = { type = "github", user = "seancribbs", repo = "matrix" } |
| 10 | - # links = [{ title = "Website", href = "" }] |
| 11 | - # |
| 12 | - # For a full reference of all the available options, you can have a look at |
| 13 | - # https://gleam.run/writing-gleam/gleam-toml/. |
| 7 | + |
| 8 | + internal_modules = [ |
| 9 | + "matrix/internal/*", |
| 10 | + ] |
| 11 | + |
| 14 12 | |
| 15 13 | [dependencies] |
| 16 14 | gleam_stdlib = ">= 0.44.0 and < 2.0.0" |
| 17 | - vec = ">= 3.2.0 and < 4.0.0" |
| 15 | + vec = ">= 3.5.0 and < 4.0.0" |
| 18 16 | gleam_community_maths = ">= 2.0.1 and < 3.0.0" |
| 19 17 | |
| 20 18 | [dev-dependencies] |
| @@ -1,6 +1,6 @@ | |
| 1 1 | {<<"name">>, <<"matrix_gleam">>}. |
| 2 2 | {<<"app">>, <<"matrix_gleam">>}. |
| 3 | - {<<"version">>, <<"0.1.0">>}. |
| 3 | + {<<"version">>, <<"0.2.0">>}. |
| 4 4 | {<<"description">>, <<"Matrix mathematics for Gleam"/utf8>>}. |
| 5 5 | {<<"licenses">>, [<<"MIT">>]}. |
| 6 6 | {<<"build_tools">>, [<<"gleam">>]}. |
| @@ -13,24 +13,26 @@ | |
| 13 13 | {<<"optional">>, false}, |
| 14 14 | {<<"requirement">>, <<">= 0.44.0 and < 2.0.0">>} |
| 15 15 | ]}, |
| 16 | - {<<"vec">>, [ |
| 17 | - {<<"app">>, <<"vec">>}, |
| 18 | - {<<"optional">>, false}, |
| 19 | - {<<"requirement">>, <<">= 3.2.0 and < 4.0.0">>} |
| 20 | - ]}, |
| 21 16 | {<<"gleam_community_maths">>, [ |
| 22 17 | {<<"app">>, <<"gleam_community_maths">>}, |
| 23 18 | {<<"optional">>, false}, |
| 24 19 | {<<"requirement">>, <<">= 2.0.1 and < 3.0.0">>} |
| 20 | + ]}, |
| 21 | + {<<"vec">>, [ |
| 22 | + {<<"app">>, <<"vec">>}, |
| 23 | + {<<"optional">>, false}, |
| 24 | + {<<"requirement">>, <<">= 3.5.0 and < 4.0.0">>} |
| 25 25 | ]} |
| 26 26 | ]}. |
| 27 27 | {<<"files">>, [ |
| 28 28 | <<"LICENSE">>, |
| 29 29 | <<"README.md">>, |
| 30 30 | <<"gleam.toml">>, |
| 31 | + <<"src/matrix/internal/projection.gleam">>, |
| 31 32 | <<"src/matrix/mat2f.gleam">>, |
| 32 33 | <<"src/matrix/mat3f.gleam">>, |
| 33 34 | <<"src/matrix/mat4f.gleam">>, |
| 35 | + <<"src/matrix@internal@projection.erl">>, |
| 34 36 | <<"src/matrix@mat2f.erl">>, |
| 35 37 | <<"src/matrix@mat3f.erl">>, |
| 36 38 | <<"src/matrix@mat4f.erl">>, |
| @@ -0,0 +1,27 @@ | |
| 1 | + import vec/vec2.{type Vec2, Vec2} |
| 2 | + import vec/vec3.{type Vec3, Vec3} |
| 3 | + import vec/vec4.{type Vec4, Vec4} |
| 4 | + |
| 5 | + pub fn to_xz(v: Vec3(a)) -> Vec2(a) { |
| 6 | + Vec2(x: v.x, y: v.z) |
| 7 | + } |
| 8 | + |
| 9 | + pub fn to_xy(v: Vec3(a)) -> Vec2(a) { |
| 10 | + Vec2(x: v.x, y: v.y) |
| 11 | + } |
| 12 | + |
| 13 | + pub fn to_yz(v: Vec3(a)) -> Vec2(a) { |
| 14 | + Vec2(x: v.y, y: v.z) |
| 15 | + } |
| 16 | + |
| 17 | + pub fn to_xyz(vec: Vec4(a)) -> Vec3(a) { |
| 18 | + Vec3(x: vec.x, y: vec.y, z: vec.z) |
| 19 | + } |
| 20 | + |
| 21 | + pub fn extend2(vec: Vec2(a), z: a) -> Vec3(a) { |
| 22 | + Vec3(x: vec.x, y: vec.y, z:) |
| 23 | + } |
| 24 | + |
| 25 | + pub fn extend3(vec: Vec3(a), w: a) -> Vec4(a) { |
| 26 | + Vec4(x: vec.x, y: vec.y, z: vec.z, w:) |
| 27 | + } |
| @@ -1,10 +1,11 @@ | |
| 1 1 | //// 2x2 matrices of floats |
| 2 2 | |
| 3 3 | import gleam/float |
| 4 | + import gleam/list |
| 4 5 | import gleam/result |
| 5 6 | import gleam_community/maths |
| 6 7 | import vec/vec2.{type Vec2, Vec2} |
| 7 | - import vec/vec2f.{type Vec2f} |
| 8 | + import vec/vec2f.{type Vec2f, positive_x, positive_y} |
| 8 9 | |
| 9 10 | /// Mat2f is a 2x2 column-major matrix of `Float`s. |
| 10 11 | pub type Mat2f = |
| @@ -13,12 +14,8 @@ pub type Mat2f = | |
| 13 14 | /// A Mat2f with all elements set to `0.0` |
| 14 15 | pub const zero: Mat2f = vec2.Vec2(vec2f.zero, vec2f.zero) |
| 15 16 | |
| 16 | - const x: Vec2f = Vec2(1.0, 0.0) |
| 17 | - |
| 18 | - const y: Vec2f = Vec2(0.0, 1.0) |
| 19 | - |
| 20 17 | /// A Mat2f representing the identity, i.e. `1.0` is on the diagonal. |
| 21 | - pub const identity: Mat2f = vec2.Vec2(x, y) |
| 18 | + pub const identity: Mat2f = vec2.Vec2(positive_x, positive_y) |
| 22 19 | |
| 23 20 | /// Constructs a `Mat2f` from its components: |
| 24 21 | /// ```text |
| @@ -66,6 +63,11 @@ pub fn transpose(mat: Mat2f) -> Mat2f { | |
| 66 63 | new(mat.x.x, mat.y.x, mat.x.y, mat.y.y) |
| 67 64 | } |
| 68 65 | |
| 66 | + /// Returns the diagonal of the `Mat2f` |
| 67 | + pub fn diagonal(mat: Mat2f) -> Vec2f { |
| 68 | + Vec2(mat.x.x, mat.y.y) |
| 69 | + } |
| 70 | + |
| 69 71 | /// Returns the determinant for the `Mat2f`. |
| 70 72 | pub fn determinant(mat: Mat2f) -> Float { |
| 71 73 | mat.x.x *. mat.y.y -. mat.x.y *. mat.y.x |
| @@ -100,6 +102,11 @@ pub fn negate(mat: Mat2f) -> Mat2f { | |
| 100 102 | vec2.map(mat, vec2f.negate) |
| 101 103 | } |
| 102 104 | |
| 105 | + /// Takes the absolute value of each element in the `Mat2f`. |
| 106 | + pub fn absolute_value(mat: Mat2f) -> Mat2f { |
| 107 | + vec2.map(mat, vec2f.absolute_value) |
| 108 | + } |
| 109 | + |
| 103 110 | /// Adds two `Mat2f` together. |
| 104 111 | pub fn add(a: Mat2f, b: Mat2f) -> Mat2f { |
| 105 112 | vec2.map2(a, b, vec2f.add) |
| @@ -132,3 +139,25 @@ pub fn scale(mat: Mat2f, scale: Float) -> Mat2f { | |
| 132 139 | pub fn scale_diagonal(mat: Mat2f, scale: Vec2f) -> Mat2f { |
| 133 140 | vec2.map2(mat, scale, vec2f.scale) |
| 134 141 | } |
| 142 | + |
| 143 | + /// Returns a matrix containing the reciprocal of each element of the `Mat2f`. |
| 144 | + /// |
| 145 | + /// If any of the elements is zero, an error is returned. |
| 146 | + pub fn reciprocal(mat: Mat2f) -> Result(Mat2f, Nil) { |
| 147 | + vec2.map(mat, fn(column) { |
| 148 | + column |
| 149 | + |> vec2.map(float.divide(1.0, _)) |
| 150 | + |> vec2.result() |
| 151 | + }) |
| 152 | + |> vec2.result |
| 153 | + } |
| 154 | + |
| 155 | + /// Sums a list of `Mat2f`s. |
| 156 | + pub fn sum(mats: List(Mat2f)) -> Mat2f { |
| 157 | + list.fold(mats, zero, add) |
| 158 | + } |
| 159 | + |
| 160 | + /// Multiplies a list of `Mat2f`s. |
| 161 | + pub fn product(mats: List(Mat2f)) -> Mat2f { |
| 162 | + list.fold(mats, identity, multiply) |
| 163 | + } |
Loading more files…