{"id":17981466,"url":"https://github.com/francisrstokes/vec-la","last_synced_at":"2025-03-25T18:31:16.236Z","repository":{"id":57391306,"uuid":"91846529","full_name":"francisrstokes/vec-la","owner":"francisrstokes","description":"Tiny linear algebra library specifically for 2d","archived":false,"fork":false,"pushed_at":"2017-12-06T08:41:09.000Z","size":58,"stargazers_count":43,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T23:06:26.153Z","etag":null,"topics":["javascript","javascript-library","linear-algebra","matrices","vectors"],"latest_commit_sha":null,"homepage":null,"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":"2017-05-19T21:24:09.000Z","updated_at":"2024-03-01T12:59:31.000Z","dependencies_parsed_at":"2022-09-19T20:14:19.391Z","dependency_job_id":null,"html_url":"https://github.com/francisrstokes/vec-la","commit_stats":null,"previous_names":["francisrstokes/vec"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fvec-la","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fvec-la/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fvec-la/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francisrstokes%2Fvec-la/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/francisrstokes","download_url":"https://codeload.github.com/francisrstokes/vec-la/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245519952,"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":["javascript","javascript-library","linear-algebra","matrices","vectors"],"created_at":"2024-10-29T18:10:00.458Z","updated_at":"2025-03-25T18:31:15.888Z","avatar_url":"https://github.com/francisrstokes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vec\n\nTiny linear algebra library specifically for 2d.\n\nSee it in action: https://codepen.io/fstokesman/pen/aWgEXv\n\n## Installation\n\n`npm install --save vec-la`\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/dist/vec.window.js\"\u003e\u003c/script\u003e`\n\n## Features\n\n- Immutable functions for manipulating vectors\n- Vectors and matrices represented as pure, single dimensional arrays\n- Immutable Matrix builder helper object for sequentially composing matrices\n\n## API\n\n- `vec.add(v, v2)` : Result of adding `v` and `v2`\n- `vec.sub(v, v2)` : Result of subtracting `v2` from `v`\n- `vec.scale(v, sc)` : Result of multiplying components of `v` by `sc`\n- `vec.midpoint(v, v2)` : Midpoint between `v` and `v2`\n- `vec.norm(v)` : Result of normalising `v`\n- `vec.mag(v)` : Magnitude of `v`\n- `vec.normal(v)`: Normal vector of `v`\n- `vec.towards(v, v2, t)`: A point in the interval [v, v2] along the direction formed from `v2 - v1`. `t` is a normalalised percentage [0, 1] of where in the interval the point falls.\n- `vec.rotate(v, a)` : Result of rotating `v` around the origin by `a` radians\n- `vec.rotatePointAround(v, cp, a)` : Result of rotating `v` around `cp` by `a` radians\n- `vec.dot(v, v2)` : Dot product of `v` and `v2`\n- `vec.det(v)` : Determinant of `v`\n- `vec.dist(v, v2)` : Euclidean distance between `v` and `v2`\n- `vec.matrixBuilder(m)` : Creates a matrix builder (see below)\n- `vec.createMatrix(a, b, c, d, tx, ty)` : Helper function for matrix creation. Defaults to an identity matrix\n- `vec.transform(v, m)` : Result of applying matrix tranformation `m` to `v`\n- `vec.composeTransform(m, m2)` : Result of composing transformation matrix `m` with `m2`\n\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\n## Matrix Builder\n\n`vec.matrixBuilder(m)` creates a builder object that can be used to easily chain together transformations. Call `get()` on the builder at any time to get a copy of the matrix at that point.\n\n```javascript \nconst mb = vec.matrixBuilder(); // Defaults to identity matrix\nconst finalMatrix = mb\n  .rotate(Math.PI/6)\n  .scale(2, 3)\n  .shear(0.2, 0)\n  .translate(20, 40)\n  .get();\n\n// [ \n//  2.0320508075688775, -0.48038475772933664, 20,\n//  1.4999999999999998, 2.598076211353316, 40,\n//  0, 0, 1\n// ]\n```\n\nThe function also accepts a matrix as it's argument. \n\n- `rotate(a)` : Concatenate a rotation matrix of `a` radians\n- `scale(x, y)` : Concatenate a scaling matrix\n- `shear(x, y)` : Concatenate a shearing matrix\n- `translate(x, y)` : Concatenate a translation matrix\n- `add(m)` : Concatenate an arbitrary matrix\n- `get()` : Return the resulting matrix\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(v1, scaler); // [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(v1, m); // [-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\n### Composing Matrices\n\n```javascript \nconst v = [10, 10];\nconst m = [\n  0, -1, 0,\n  -1, 0, 0,\n   0, 0, 1\n];\nconst m2 = [\n  Math.cos(Math.PI/2), -Math.sin(Math.PI/2), 0,\n  Math.sin(Math.PI/2), Math.cos(Math.PI/2)   0,\n  0, 0, 1\n];\nconst m3 = vec.composeTransform(m2, m);\n\nconst v2 = vec.transform(v1, m1); // is the same as\nconst v3 = vec.transform(vec.transform(v1, m), m2);\n```\n\n## Motivation\n\n Many linear algebra libraries represent their vectors as object like `{ x, y, mutableMethod, ... }`, which can be cumbersome to work with. Arrays are easier to map, reduce, combine and generally work with symbolically. Additionally, Vec is designed to be used with ES6 and thus the `...` rest syntax, and so can easily and cleanly be supplied to functions expecting `x` and `y` parameters as sequential arguments.\n \n For example: \n \n `ctx.arc(...point, radius, 0, 2 * Math.PI, false)`;\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrancisrstokes%2Fvec-la","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrancisrstokes%2Fvec-la","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrancisrstokes%2Fvec-la/lists"}