{"id":13838479,"url":"https://github.com/polymonster/maths-rs","last_synced_at":"2026-03-27T02:44:56.802Z","repository":{"id":39914360,"uuid":"502647683","full_name":"polymonster/maths-rs","owner":"polymonster","description":"Linear algebra library for graphics and gamedev.","archived":false,"fork":false,"pushed_at":"2026-02-25T18:59:16.000Z","size":465,"stargazers_count":58,"open_issues_count":2,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-02-25T21:30:19.645Z","etag":null,"topics":["gamedev","linear-algebra","math","matrix","quaternion","swizzling","vector"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/polymonster.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-06-12T15:03:35.000Z","updated_at":"2026-02-25T18:57:53.000Z","dependencies_parsed_at":"2024-01-15T19:43:39.626Z","dependency_job_id":"13378aef-82c2-4b7f-b996-eff8c53a885b","html_url":"https://github.com/polymonster/maths-rs","commit_stats":{"total_commits":241,"total_committers":2,"mean_commits":120.5,"dds":0.06639004149377592,"last_synced_commit":"1a86cda018c062f40cebc897883867c42188436e"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/polymonster/maths-rs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polymonster%2Fmaths-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polymonster%2Fmaths-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polymonster%2Fmaths-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polymonster%2Fmaths-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/polymonster","download_url":"https://codeload.github.com/polymonster/maths-rs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polymonster%2Fmaths-rs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31011921,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-27T02:33:22.146Z","status":"ssl_error","status_checked_at":"2026-03-27T02:33:21.763Z","response_time":164,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gamedev","linear-algebra","math","matrix","quaternion","swizzling","vector"],"created_at":"2024-08-04T15:01:58.934Z","updated_at":"2026-03-27T02:44:56.779Z","avatar_url":"https://github.com/polymonster.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# Maths\n\n[![build](https://github.com/polymonster/maths-rs/actions/workflows/build.yml/badge.svg)](https://github.com/polymonster/maths-rs/actions/workflows/build.yml)\n[![publish](https://github.com/polymonster/maths-rs/actions/workflows/publish.yml/badge.svg)](https://github.com/polymonster/maths-rs/actions/workflows/build.yml)\n[![docs](https://img.shields.io/docsrs/maths-rs/latest)](https://docs.rs/maths-rs/latest/maths_rs/index.html)\n[![crates](https://img.shields.io/crates/v/maths-rs)](https://crates.io/crates/maths-rs)\n\nMaths is a linear algebra library which aims to be ergonmic and fun to use for gamedev and graphics. If you like writing shaders you should feel right at home. In addition to the usual implementation of vectors, matrices and quaternions it includes a comprehensive collection of utility functions, vector swizzling, intersection tests, point tests, distance functions, trig functions, graphs and signal processing functions.\n\nMost features of the crate are enabled by default, you can choose to opt out if you wish. `serde` and `hash` support is optional and disabled by default they can be enabled with:\n\n```text\nfeatures = [\"serde\", \"hash\"]\n```\n\nMost of this code was ported from my C++ [maths library](https://github.com/polymonster/maths), there is a live [WebGL demo](https://www.polymonster.co.uk/pmtech/examples/maths_functions.html) which showcases a lot of the features in an interactive way and was used to generate test data and verify the functions work correctly.\n\n## Vector\n\nThis is a narrow vector library with `Vec2`, `Vec3` and `Vec4` column-vector implementations.\n\n```rust\n/// abbrivated types #[cfg(feature = \"short_types\")]\npub type Vec2f = Vec2\u003cf32\u003e;\npub type Vec3f = Vec3\u003cf32\u003e;\npub type Vec4f = Vec4\u003cf32\u003e;\npub type Vec4d = Vec4\u003cf64\u003e;\n/// ... etc\n\n// construct a vector\nlet v2 = Vec2f::new(2.0, 3.0);\n\n// the short way #[cfg(feature = \"short_hand_constructors\")]\nlet v2_short = vec2f(5.0, 6.0);\n\n// splat a scalar #[cfg(feature = \"short_hand_constructors\")]\nlet v3_splat = splat3f(9.0);\n\n// quick common constructors\nlet y = Vec3f::unit_y();\nlet b = Vec3f::blue();\nlet z = Vec3f::zero();\nlet o = Vec3f::one();\nlet m = Vec3f::max_value();\n// + more\n\n// default\nlet vdefault = Vec3f::default(); // default is zero\n\n// arithmetic / operators\nlet result = v2 * v2_short;\nlet result = v2 + v2_short;\nlet result = v2 / v2_short;\nlet result = v2 - v2_short;\nlet result = -v2;\n\n// arithmetic with values and refs so no need for dereferencing\nlet v2ref = \u0026v2;\nlet result = v2ref * v2; // ref * value\nlet result = v2 * v2ref; // value * ref\nlet result = v2ref * v2ref; // ref * ref\n\n// construct from tuples and vectors of various sizes\nlet v4 = Vec4f::from((v2, v2)); // vec4 from 2x v2's\nlet v3 = Vec3f::from((v2, 1.0)); // vec3 from 1x v2 and 1x scalar\nlet v2 = Vec2f::from((5.0, 6.0)); // vec2 from 2x scalars\nlet v4 = Vec4f::from((v2, 0.0, 1.0)); // vec4 from 1x v2 and 2x scalars\nlet v4 = Vec4f::from(v2); // vec4 from vec2 (splat 0's)\nlet v2 = Vec2f::from(v4); // vec2 from vec4 (truncate)\n// .. and so on\n\n// swizzling\nlet wxyz = v4.wxyz(); // swizzle\nlet xyz = v4.xyz(); // truncate\nlet xxx = v4.xxx(); // and so on..\nlet xy = v3.yx(); // ..\n\n// mutable swizzles\nlet mut v = Vec4f::zero();\nx.set_xwyz(v); // set swizzle\nx.set_xy(v.yx()); // assign truncated\nx.set_yzx(v.zzz()); // etc..\n\n// type casts #[cfg(feature = \"casts\")]\nlet veci = Vec3i::from(vec3f(1.0, 2.0, 3.0));\n```\n\n## Matrix\n\nRow-major matrices with `Mat2`, `Mat3`, `Mat34` and `Mat4` implementations.\n\n```rust\n/// abbrivated types #[cfg(feature = \"short_types\")]\npub type Mat2f = Mat2\u003cf32\u003e;\npub type Mat3f = Mat3\u003cf32\u003e;\npub type Mat34f = Mat34\u003cf32\u003e;\npub type Mat4f = Mat4\u003cf32\u003e;\n\n// identity\nlet mat_ident = Mat4f::identity();\nlet mat_default = Mat4f::default(); // default is identity\n\n// construct from floats\nlet m4 = Mat4f::new(\n    1.0, 0.0, 0.0, 0.0,\n    0.0, 1.0, 0.0, 0.0,\n    0.0, 0.0, 1.0, 0.0,\n    0.0, 0.0, 0.0, 1.0\n);\n\n// construct from common matrices\nlet m34 = Mat34f::from_translation(vec3f(50.0, -10.0, 20.0));\nlet m4 = Mat4f::from_z_rotation(f32::deg_to_rad(90.0));\nlet m4 = Mat4f::from_scale(vec3f(10.0, 2.0, 30.0));\nlet m3 = Mat34f::from_scale(vec3f(10.0, 2.0, 30.0));\n\n// arithmetic\n// matrix multiplication\nlet result = x4 * m4;\n\n// vector transformation\nlet v4 = vec4f(0.0, 1.0, 0.0, 1.0);\nlet result = m4 * v4;\n\n// arithmetic with values and refs so no need for dereferencing\nlet v4ref = \u0026v4;\nlet m4ref = \u0026m4;\nlet result = m4ref * v4; // ref * value\nlet result = m4 * v4ref; // value * ref\nlet result = m4ref * v4ref; // ref * ref\n\n// functions\nlet det = m4.determinant();\nlet inv = m4.inverse();\n\n// construct rows from tuples\nlet m3v = Mat3f::from((\n    vec3f(1.0, 2.0, 3.0),\n    vec3f(4.0, 5.0, 6.0),\n    vec3f(7.0, 8.0, 9.0)\n));\n\nlet m4v = Mat4f::from((\n    vec4f(1.0, 2.0, 3.0, 4.0),\n    vec4f(5.0, 6.0, 7.0, 8.0),\n    vec4f(9.0, 10.0, 11.0, 12.0),\n    vec4f(13.0, 14.0, 15.0, 16.0),\n));\n\nlet m4_rot = Mat4f::from(m3v); // mat4 from mat3\nlet m4r = Mat3f::from(quat); // from quaternion\n\n// type casts #[cfg(feature = \"casts\")]\nlet matd = Mat4d::from(m4v);\n```\n\n## Quaternion\n\nGeneric floating-point quaternion.\n\n```rust\n// abbrivated types #[cfg(feature = \"short_types\")]\npub type Quatf = Quat\u003cf32\u003e;\npub type Quatd = Quat\u003cf64\u003e;\n\n// construct from euler angles\nlet q = Quatf::from_euler_angles(x, y, z);\n\n// construct from axis angle\nlet q2 = Quatf::from_axis_angle(axis, angle);\n\n// identity\nlet qident = Quatf::identity();\nlet qdefault = Quat::default(); // default is identity\n\n// arithmetic\nlet q3 = q * q2;\nlet q4 = q + q2;\nlet q5 = q - q2;\nlet q6 = -q;\n\n// quat * vec\nlet v = vec3f(1.0, 0.0, 0.0);\nlet vv = q * v3;\n\n// multiply with refs or values without need to deref\nlet qref = \u0026q;\nlet vref = \u0026v3;\n\nlet qr = qref * v3;\nlet qr = q * v3ref;\nlet qr = qref * vref;\n\n// functions\nlet rev = q.reverse();\nlet inv = q.inverse();\n\n// type casts #[cfg(feature = \"casts\")]\nlet quatd = Quatd::from(q2);\n```\n\n## Generic Functions\n\nYou can use generic functions on different sized vectors or scalars: `min, max, clamp, step, signum, copysign, abs, deg_to_rad, rad_to_deg, floor, ceil, round, approx, sqrt, powi, powf, sqrt, frac, trunc, modf, rsqrt, recip lerp, nlerp, slerp, smoothstep, dot, perp, cross, mag, mag2, length, distance, dist, dist2, normalize, chebyshev_normalize`\n\n```rust\n// numeric ops\nlet int_abs = abs(-1);\nlet float_abs = abs(-1.0);\nlet int_max = max(5, 6);\nlet float_max = max(1.0, 2.0);\nlet vec_max = max(vec3f(1.0, 1.0, 1.0), vec3f(2.0, 2.0, 2.0));\nlet vec_min = min(vec4f(8.0, 7.0, 6.0, 5.0), vec4f(-8.0, -7.0, -6.0, -5.0));\n\n// float ops\nlet fsat = saturate(22.0);\nlet vsat = saturate(vec3f(22.0, 22.0, 22.0));\nlet f = floor(5.5);\nlet vc = ceil(vec3f(5.0, 5.0, 5.0));\n\n// vector ops\nlet dp = dot(vec2, vec2);\nlet dp = dot(vec3, vec3);\nlet cp = cross(vec3, Vec3::unit_y());\nlet n = normalize(vec3);\nlet qn = normalize(quat);\nlet m = mag(vec4);\nlet d = dist(vec31, vec32);\n\n// interpolation\nlet fi : f32 = lerp(10.0, 2.0, 0.2);\nlet vi = lerp(vec2, vec2, 0.5);\nlet qi = lerp(quat1, quat2, 0.75);\nlet qs = slerp(quat1, quat2, 0.1);\nlet vn = nlerp(vec2, vec2, 0.3);\nlet f = smoothstep(5.0, 1.0, f);\n```\n\n## Trigonometry and Logarithmic Functions\n\nThese functions are availble for all floating point scalar or vector types: `cos, sin, tan, acos, asin, atan, cosh, sinh, tanh, sin_cos, atan2, exp, exp2, log2, log10`.\n\n```rust\n// trig functions\nlet s = sin(vec2);\nlet x = cos(vec3);\nlet f = acos(x);\nlet d = atan2(y, x);\n\n// exp / log\nlet d = exp(y);\nlet l = log2(x);\n```\n\n### Functions\n\nPlane Classification: `point_vs_plane, aabb_vs_plane, sphere_vs_plane, capsule_vs_plane, cone_vs_plane`.\n\nOverlaps: `sphere_vs_sphere, sphere_vs_aabb, sphere_vs_obb, aabb_vs_aabb, aabb_vs_frustum, sphere_vs_frustum, sphere_vs_capsule, capsule_vs_capsule, obb_vs_obb, aabb_vs_obb, convex_hull_vs_convex_hull, gjk_2d, gjk_3d`.\n\nPoint Inside: `point_inside_aabb, point_inside_sphere, point_inside_obb, point_inside_triangle, point_inside_cone, point_inside_convex_hull, point_inside_poly, point_inside_frustum`.\n\nClosest Point: `closest_point_on_aabb, closest_point_on_line, closest_point_on_plane, closest_point_on_obb, closest_point_on_sphere, closest_point_on_ray, closest_point_on_triangle, closest_point_on_polygon, closest_point_on_convex_hull, closest_point_on_cone`.\n\nPoint Distance: `point_aabb_distance, point_segment_distance, point_triangle_distance, distance_on_line, point_plane_distance, plane_distance, point_sphere_distance, point_polygon_distance, point_convex_hull_distance, point_cone_distance, point_obb_distance`.\n\nRay / Line: `ray_vs_plane, ray_vs_triangle, ray_vs_sphere, ray_vs_line_segment, ray_vs_aabb, ray_vs_obb, ray_vs_capsule, ray_vs_cylinder, line_vs_line, line_vs_poly, shortest_line_segment_between_lines, shortest_line_segment_between_line_segments`.\n\nShader Style Functions: `dot, cross, normalize, mag, mag2, dist, dist2, triple, vector_triple, lerp, nlerp, slerp, saturate, clamp, normalize, chebyshev_normalize, all, any, min, max, smoothstep, step, round, floor, ceil, abs, frac, trunc, exp, exp2, log, log2, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh`.\n\nGraph Functions: `smooth_start, smooth_stop, impulse, cubic_pulse, exp_step, parabola, pcurve, exp_sustained_impulse, sinc, gain, almost_identity, integral_smoothstep, quad_impulse, poly_impulse`.\n\n\\+ More included!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolymonster%2Fmaths-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpolymonster%2Fmaths-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolymonster%2Fmaths-rs/lists"}