{"id":13838125,"url":"https://github.com/silviopaganini/maths","last_synced_at":"2025-03-21T12:45:00.152Z","repository":{"id":50748284,"uuid":"38685452","full_name":"silviopaganini/maths","owner":"silviopaganini","description":"learning maths again","archived":false,"fork":false,"pushed_at":"2019-11-13T17:37:19.000Z","size":10,"stargazers_count":193,"open_issues_count":0,"forks_count":11,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-01-26T08:45:17.319Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/silviopaganini.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-07T12:14:07.000Z","updated_at":"2024-10-15T07:56:16.000Z","dependencies_parsed_at":"2022-09-24T19:04:01.555Z","dependency_job_id":null,"html_url":"https://github.com/silviopaganini/maths","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/silviopaganini%2Fmaths","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silviopaganini%2Fmaths/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silviopaganini%2Fmaths/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silviopaganini%2Fmaths/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/silviopaganini","download_url":"https://codeload.github.com/silviopaganini/maths/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244801125,"owners_count":20512599,"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":[],"created_at":"2024-08-04T15:01:38.352Z","updated_at":"2025-03-21T12:45:00.128Z","avatar_url":"https://github.com/silviopaganini.png","language":null,"funding_links":[],"categories":["Others","Math"],"sub_categories":["Other"],"readme":"# Learning Maths again \n\n### [Useful GLSL Functions](https://github.com/silviopaganini/maths/blob/master/GLSL.md)\n\n### [Basic light shading](https://github.com/silviopaganini/maths/blob/master/Lighting.md)\n\nthanks to everyone who created the [Shader School](https://www.npmjs.com/package/shader-school) =) \n\n## Contents\n\n- [Radians / Degrees](#radians--degrees)\n- [Calculate side lengths](#calculate-side-lengths)\n- [Rotate a 2D point](#rotate-a-2d-point)\n- [Linear Distance 2 points](#linear-distance-2-points)\n- [Linear distance between 2 vectors](#linear-distance-between-2-vectors)\n- [Length of a vector](#length-of-a-vector)\n- [Add and substract vectors](#add-and-substract-vectors)\n- [Normalize vector](#normalize-vector)\n- [Dot product vectors](#dot-product-vectors)\n- [Finding angle between 2 points](#finding-angle-between-2-points)\n- [Finding angle between 2 vectors](#finding-angle-between-2-vectors)\n- [Cross Product](#cross-product)\n\n#### Radians \u0026 Degrees\n\n```js\n// JavaScript\nvar angleInDegrees = 45;\nvar radians = angleInDegrees * Math.PI / 180;\nvar backToDegrees = radians * 180 / Math.PI;\n```\n\n```glsl\n// GLSL\nfloat angleInDegrees = 90.0;\nfloat r = radians(angleInDegrees);\nfloat angleInDegrees = degrees(r);\n```\n\n#### Calculate side lengths\n##### SOHCAHTOA\n\n```js\n// Javascript\nvar hyp = 100;\nvar angleDegrees = 45;\nvar angleRadians = angleDegrees * Math.PI / 180;\n\nvar opposite = Math.sin( angleRadians ) * hyp;\nvar adjacent = Math.cos( angleRadians ) * hyp;\nvar tangent  = opposite / adjacent;\n```\n\n```glsl\n// GLSL\nfloat hyp = 100.;\nfloat angleDegrees = 45.;\nfloat angleRadians = radians(angleDegrees);\n\nfloat opposite = sin(angleRadians) * hyp;\nfloat adjacent = cos(angleRadians) * hyp;\nfloat tangent  = opposite / adjacent;\n\n```\n\n#### Rotate a 2D point\n```js\nvar vec2 = {x: 2, y: 3};\nvar rotatedVector = rotate2D(vec2, angle);\n\nfunction rotate2D(vector, angle)\n{\n\tvar theta = angle * Math.PI / 180; // radians\n\tvar matrix = [  Math.cos(theta),  Math.sin(theta), \n\t\t\t\t\t-Math.sin(theta), Math.cos(theta)\n\t\t\t\t\t];\n\t\t\t\t\t\n\treturn { \n\t\tx: matrix[0] * vector.x + matrix[1] * vector.y, \n\t\ty: matrix[2] * vector.x + matrix[3] * vector.y\n\t};\n}\n\n```\n\n```glsl\n// GLSL\nvec2 rotate2D(vec2 position, float theta)\n{\n    // theta in radians\n    mat2 m = mat2( cos(theta), sin(theta), -sin(theta), cos(theta) );\n    return m * position;\n}\n```\n\n#### Linear Distance 2 points\n\n```js\n// JavaScript\nvar x1 = 3;\nvar x2 = 5;\nvar distance = x2 — x1;\n```\n\n```glsl\n// GLSL\nfloat x1 = 3.0;\nfloat x2 = 5.0;\nfloat distance = x2 — x1;\n```\n\n#### Linear distance between 2 vectors\n\n###### a² + b² = c²\n\n```js\n// Javascript\nvar v1 = {x: 4, y: -9};\nvar v2 = {x: 5, y: 15};\n\nvar distance = Math.sqrt( Math.pow((v2.x — v1.x), 2) + Math.pow((v2.y — v1.y), 2) );\n```\n\n```glsl\n// GLSL\nvec2 x1 = vec2(1.0, 2.0);\nvec2 x2 = vec2(2.0, 1.0);\nfloat distance = distance(vec2(x1), vec2(x2));\n```\n\n#### Length of a vector\n###### a.k.a Magnitude \n\n```js\n// Javascript\n// 2D -\u003e hypotenuse\nvar v = {x: 4, y:-9};\nvar length = Math.sqrt( (Math.pow(v.x, 2) + Math.pow(v.y, 2)) );\n\n// 3D\nvar v = {x: 4, y:-9, z: 0.5};\nvar length = Math.sqrt( (Math.pow(v.x, 2) + Math.pow(v.y, 2) + Math.pow(v.z, 2) ));\n```\n\n```glsl\n// GLSL\nvec2 v = vec2(1.0, 2.0);\nfloat l = length(v);\n```\n\n#### Add and substract vectors\n\n```js\nvar v1 = {x: 2, y: 3};\nvar v2 = {x: 2, y: -2};\nvar addedVec = {x: v1.x + v2.x, y: v1.y + v2.y};\nvar subVec = {x: v1.x - v2.x, y: v1.y - v2.y};\n```\n\n#### Normalize vector\n\n```js\n// Javascript\n// 2D\nvar v = {x: 4, y:-9};\nvar length = Math.sqrt( (Math.pow(v.x, 2) + Math.pow(v.y,2)) );\nvar n = {x: v.x / length, y: v.y / length};\n\n// 3D\nvar v = {x: 4, y:-9, z: 3};\nvar length = Math.sqrt( Math.pow(v.x, 2) + Math.pow(v.y,2) + Math.pow(v.z,2) );\nvar n = {x: v.x / length, y: v.y / length, z: v.z / length};\n```\n\n```glsl\n// GLSL\nvec2 v = vec2(4.0, -9.0);\nvec2 n = normalize(v);\n\nvec3 v = vec3(4.0, -9.0, 3.0);\nvec3 n = normalize(v);\n```\n\n#### Dot product vectors\n\n```js\n// Javascript\nvar v1 = {x: 4, y: 5, z: 9};\nvar v2 = {x: 5, y: 9, z: -5};\nvar dot = (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);\n```\n```glsl\n// GLSL \nvec3 v1 = vec2(4., 5., 9.);\nvec3 v2 = vec2(5., 9., -5.);\nfloat dot = dot(v1, v2);\n```\n\n#### Finding angle between 2 points\n\n```js\n//Javascript\nvar x = -3;\nvar y = -2;\nvar radians = Math.atan2(x, y);\nvar degrees = radians * 180 / Math.PI;\n```\n\n```glsl\n//GLSL\nfloat x = -3.0;\nfloat y = -2.0;\nfloat radians = atan(x, y);\nfloat degrees = degrees(radians);\n```\n\n#### Finding angle between 2 vectors\n\n```js\n// Javascript\nvar v1 = {x: 4, y: 5, z: 9};\nvar v2 = {x: 5, y: 9, z: -5};\nvar dot = (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);\n\nvar lengthv1 = length(v1); // see length\nvar lengthv2 = length(v2); // see length\n\nvar radians = Math.acos(dot / (lengthv1 * lengthv2));\nvar angle = radians * 180 / Math.PI;\n```\n```glsl\n// GLSL \nvec3 v1 = vec2(4., 5., 9.);\nvec3 v2 = vec2(5., 9., -5.);\nfloat dot = dot(v1, v2);\nfloat l1 = length(v1);\nfloat l2 = length(v2);\n\nfloat radians = acos(dot / (l1 * l2));\nfloat angle = degrees(radians);\n```\n\n#### Cross Product\n\n```js\n// Javascript\nvar v1 = {x: 1, y: 2, z: 3};\nvar v2 = {x: 3, y: 2, z: 1};\nvar cross = {\n\tx: v1.y*v2.z - v1.z*v2.y, \n\ty: v1.z*v2.x - v1.x*v2.z, \n\tz: v1.x*v2.y - v1.y*v2.x\n};\n\n```\n```glsl\n// GLSL\nvec3 v1 = vec3(1.0, 2.0, 3.0);\nvec3 v2 = vec3(2.0, 2.0, 1.0);\nvec3 cross = cross(v1, v2);\n```\n\n#### Projection of point B over A\n\n\n![](data:image/gif;base64,R0lGODdhegBFAIAAAAAAAP///ywAAAAAegBFAAAC/oyPqcvtD6OctEqAgd28+5ll30iWYBia6mqiLgvHmyvK9v3QGM73B+0L4l4aoXFVTB2XpaJB5GRKK9HnlLmzVBPb6y1L6XqxSogYcR6zsuBGWm1kv98/OE/uoNuDeIa+vmfTp/AX2IPip2WItdjIxVHoSBIpuUT5WPmV2XhJuBnT+XnXJKoSWipoijpyugrTmug6IzsGS0sKenuhO2Wbw5sHzDgk7Fkc5+MbqHwcJsT81eZmWuaKOE0NhJapzaXzDR5+za2DJn6Onq6+zo4uCF76PdQd/3JXvTp+D1zTfOgP0FA/VJGksdJg0F/CSdAgBeiHMAnEMBFbDLTkRCIgUisPKeKDVLHXto4bSRbccXFGSClRkozkePKHr4/IALUcGfNJwys3SXKESVHnzl4Zf/o06U2MPWERi5oDo3QByqE8HSYNiE0RTqyxghJayDWshwIAOw==)\n\n```js\n// Javascript\n// ||b -\u003e a|| \nvar b = {x: 1, y: 3};\nvar theta = 45;\nvar projection = normalize(b) * Math.cos(theta); // see Normalize Vector\n```\n\n```glsl\n// GLSL\nvec2 b = vec2(1, 3);\nfloat theta = 45;\nfloat projection = normalize(b) * cos(theta);\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilviopaganini%2Fmaths","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsilviopaganini%2Fmaths","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilviopaganini%2Fmaths/lists"}