{"id":17981474,"url":"https://github.com/francisrstokes/vec-la-fp","last_synced_at":"2025-03-25T18:31:16.460Z","repository":{"id":28522505,"uuid":"118622464","full_name":"francisrstokes/vec-la-fp","owner":"francisrstokes","description":"↗️ A tiny (functional) 2d linear algebra library","archived":false,"fork":false,"pushed_at":"2023-01-05T23:28:46.000Z","size":913,"stargazers_count":28,"open_issues_count":12,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-18T15:49:44.680Z","etag":null,"topics":["composable","functional","javascript","math","matrix","vector"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/francisrstokes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-23T14:36:48.000Z","updated_at":"2024-03-02T06:52:16.000Z","dependencies_parsed_at":"2023-01-14T08:59:10.797Z","dependency_job_id":null,"html_url":"https://github.com/francisrstokes/vec-la-fp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fvec-la-fp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fvec-la-fp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fvec-la-fp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fvec-la-fp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/francisrstokes","download_url":"https://codeload.github.com/francisrstokes/vec-la-fp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245519954,"owners_count":20628804,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["composable","functional","javascript","math","matrix","vector"],"created_at":"2024-10-29T18:10:02.388Z","updated_at":"2025-03-25T18:31:16.106Z","avatar_url":"https://github.com/francisrstokes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vec-la-fp\n\n`Vec-la-fp` is the functional version of the `vec-la` library. All functions are curried with arguments reordered to support composition. MatrixBuilder is replaced with composable calls to `mRotate`, `mTranslate`, `mScale`, `mShear`, and `mCompose` for arbitrary matrix concatenations.\n\nIncludes typescript types.\n\n## Installation\n\n`npm install --save vec-la-fp`\n\nand import or require as needed. If you need to use a standalone windowed version in a script tag:\n\n`\u003cscript src=\"node_modules/vec-la-fp/dist/vec.window.js\"\u003e\u003c/script\u003e`\n\n## Features\n\n- Immutable functions for manipulating vectors and matrices\n- Vectors and matrices represented as pure, single dimensional arrays\n- Composable and fully (auto) curried\n- Typescript typings for all overloadings\n\n## API\n\n`vec.add(v, v2)` - adds `v` and `v2`\n\n`vec.add3(v, v2, v3)` - adds `v`, `v2`, `v3`\n\n`vec.addAll([v1, v2, ..., vN])` - adds all vectors together\n\n`vec.sub(v, v2)` - subtracts `v2` from `v1`\n\n`vec.sub3(v, v2, v3)` - subtracts `v`, `v2`, `v3`\n\n`vec.subAll([v1, v2, ..., vN])` - subtracts all vectors together\n\n`vec.mag(v)` - gets magnitude of `v`\n\n`vec.normal(v)` - gets normal vector of `v`\n\n`vec.scale(sc, v)` - scales `v` by `sc`\n\n`vec.towards(t, v, v2)` - gets the vector at \"time\" `t` between `v` and `v2`\n\n`vec.lerp(v, v2, t)` - `towards`, but with the `t` argument last\n\n`vec.scalarNear(e, n, n2)` - true if n is within epsilon of n2\n\n`vec.near(e, v, v2)` - true if every elment of v is near the same in v2\n\n`vec.clampMag(min, max, v)` - a vector in the same direction as v with magnitude clamped to at least min and at most max\n\n`vec.norm(v)` - normalises `v`\n\n`vec.mId` - immutable identity matrix\n\n`vec.createMatrix(a, b, c, d, tx, ty)` - helper function for creating matrices\n\n`vec.transform(m, v)` - transform `v` by matrix `m`\n\n`vec.compose(m, m2)` - compose matrices `m` and `m2`\n\n`vec.mRotate(a, m)` - compose matrix `m` with a rotation matrix using angle `a`\n\n`vec.mTranslate(v, m)` - compose matrix `m` with a translation matrix using vector `v` for x and y\n\n`vec.mId` - The identity matrix\n\n`vec.mScale(v, m)` - compose matrix `m` with a scale matrix using vector `v` for x and y\n\n`vec.mShear(v, m)` - compose matrix `m` with a shear matrix using vector `v` for x and y\n\n`vec.rotate(a, v)` - rotates `v` by angle `a`\n\n`vec.rotatePointAround(a, cp, v)` - rotates `v` by angle `a` around control point vector `cp`\n\n`vec.midpoint(v, v2)` - gets midpoint between `v` and `v2`\n\n`vec.alongAngle(a, r, v)` - gets a vector `r` units along angle `a` from vector `v`\n\n`vec.dist(v, v2)` - gets distance from `v` to `v2`\n\n`vec.fastDist(v, v2)` - gets \"fast\" distance from `v` to `v2` (no square root)\n\n`vec.dot(v, v2)` - gets dot product of `v` and `v2`\n\n`vec.perpDot(v, v2)` - the perpendicular dot product of `v` and `v2` (sometimes called cross)\n\n`vec.triangleArea(a, b, c)` - signed area of triangle abc\n\n`vec.colinear(a, b, c)` - true if a b and c are colinear\n\n`vec.det(m)` - calculates the determine of matrix `m`\n\n### Tree shaking\n\nAll the functions are exported for better tree shaking:\n\n- vAdd\n- vAdd3\n- vAddAll\n- vSub\n- vSub3\n- vSubAll\n- vMag\n- vNormal\n- vScale\n- vTowards\n- vLerp\n- vNorm\n- mId\n- vCreateMatrix\n- vTransform\n- mCompose\n- mRotate\n- mTranslate\n- mScale\n- mShear\n- vRotate\n- vRotatePointAround\n- vMidpoint\n- vAngle\n- vAlongAngle\n- vFastDist\n- vDist\n- vDot\n- vDet\n\nFinally, when using the window version you can call `vec.polute()` to insert these functions into the global scope with the naming convention:\n\n`vFunctionName` e.g `vAdd`, `vMidpoint`, `vDot` etc\n\nand `mCompose`, `mRotate` etc for functions associated with matrices\n\n## Composing matrices\n\nvec-la provided a dot-chain style API for building matrices, but since matrices compose the same as functions, this API can be captured via regular function composition. For example:\n\n**Note!** The `compose` function below is *function* compose, not the `vec.mCompose` function for matrices\n\n```javascript\nconst M = compose(\n  mTranslate([10, 20]),\n  mShear([0.2, 0.3]),\n  mScale([3.2, 2.3]),\n  mRotate(1.5)\n)(mId);\n```\n\nis equivilent to vec-la's:\n\n```javascript\nconst M = vMatrixBuilder()\n  .rotate(1.5)\n  .scale(3.2, 2.3)\n  .shear(0.2, 0.3)\n  .translate(10, 20)\n  .get();\n```\n\n## Tests\n\nClone the repository, and then run `npm install \u0026\u0026 npm test`.\n\n## Examples\n\n(all examples assume vec is imported under `vec`)\n\n### Addition\n\n```javascript\nconst v1 = [0, 1];\nconst v2 = [1, 0];\nconst v3 = vec.add(v1, v2); // [1, 1]\n```\n\n### Scaling\n\n```javascript\nconst v1 = [0, 1];\nconst scaler = 10;\nconst v2 = vec.scale(scaler, v1); // [0, 10]\n```\n\n### Normalising\n\n```javascript\nconst v1 = [6.32, -23.1];\nconst v2 = vec.norm(v1); // [0.2638946146581466, -0.9645515187663272]\n```\n\n### Magnitude\n\n```javascript\nconst v1 = [6.32, -23.1];\nconst mag = vec.mag(v1); // 23.948954048141644\n```\n\n\n### Matrix Transform\n\n```javascript\nconst v1 = [10, 10];\n\n// Inversion matrix\nconst m = [\n  -1, 0,  0\n   0, -1, 0,\n   0,  0, 1\n];\nconst v2 = vec.transform(m, v1); // [-10, -10]\n```\n\n### Computing determinants\n\n```javascript\nconst m = [\n  10, 0, 0,\n  0, 10, 0,\n  0,  0, 1\n];\nconst d = vec.det(m); // 100\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrancisrstokes%2Fvec-la-fp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrancisrstokes%2Fvec-la-fp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrancisrstokes%2Fvec-la-fp/lists"}