Skip to content

Instantly share code, notes, and snippets.

@alesanabriav
Created July 24, 2021 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alesanabriav/a840a30cc99015e097793aa5f5562bcb to your computer and use it in GitHub Desktop.
Save alesanabriav/a840a30cc99015e097793aa5f5562bcb to your computer and use it in GitHub Desktop.
function vec2(x, y) {
return { x ,y };
}
function vec3(x, y, z) {
return { x ,y, z };
}
function addVecs(v1, v2) {
let x = v1.x + v2.x;
let y = v1.y + v2.y;
return vec2(x, y);
}
function subVecs(v1, v2) {
let x = v1.x + (-v2.x);
let y = v1.y + (-v2.y);
return vec2(x, y);
}
// Magnitude
function vecLength(v) {
let x = Math.pow(v.x, 2);
let y = Math.pow(v.y, 2);
return Math.sqrt(x + y);
}
function vecNormalize(v) {
let vl = vecLength(v);
let x = v.x / vl;
let y = v.y / vl;
return vec2(x, y);
}
function vecDotProduct(v1, v2) {
let x = v1.x * v2.x;
let y = v1.y * v2.y;
return x + y;
}
function vecCrossProduct(v1, v2) {
let x = (v1.y * v2.z) - (v1.z * v2.y)
let y = (v1.z * v2.x) - (v1.x * v2.z)
let z = (v1.x * v2.y) - (v1.y * v2.x)
return vec3(x, y, z)
}
let v1 = vec2(2, 4);
let v2 = vec2(3, 6);
let vs = subVecs(v1, v2);
let va = addVecs(v1, v2);
let vl = vecLength(v1);
let vn1 = vecNormalize(v1);
let vn2 = vecNormalize(vec2(5, 20));
let vdp = vecDotProduct(vec2(0.6, -0.8), vec2(0, 1));
let vcp = vecCrossProduct(vec3(20, 30, 12), vec3(2, 4, 5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment