Created
November 29, 2025 06:22
-
-
Save un4ckn0wl3z/84ff9dc8b7920aea6be728ec8aa4c086 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Vec3 { | |
| constructor(x, y, z) { | |
| this.x = x; | |
| this.y = y; | |
| this.z = z; | |
| } | |
| add(v) { | |
| this.x += v.x; | |
| this.y += v.y; | |
| this.z += v.z; | |
| } | |
| static add(a, b) { | |
| let result = new Vec3(0, 0, 0); | |
| result.x = a.x + b.x; | |
| result.y = a.y + b.y; | |
| result.z = a.z + b.z; | |
| return result; | |
| } | |
| sub(v) { | |
| this.x -= v.x; | |
| this.y -= v.y; | |
| this.z -= v.z; | |
| } | |
| static sub(a, b) { | |
| let result = new Vec3(0, 0, 0); | |
| result.x = a.x - b.x; | |
| result.y = a.y - b.y; | |
| result.z = a.z - b.z; | |
| return result; | |
| } | |
| scale(n) { | |
| this.x *= n; | |
| this.y *= n; | |
| this.z *= n; | |
| } | |
| dot(v){ | |
| return (this.x * v.x + this.y * v.y + this.z * v.z); | |
| } | |
| mag() { | |
| return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z)); | |
| } | |
| cross(v) { | |
| let result = new Vec3(0, 0, 0); | |
| result.x = this.y * v.z - this.z * v.y; | |
| result.y = this.z * v.x - this.x * v.z; | |
| result.z = this.x * v.y - this.y * v.x; | |
| return result; | |
| } | |
| normalize() { | |
| let len = this.mag(); | |
| this.x /= len; | |
| this.y /= len; | |
| this.z /= len; | |
| } | |
| // TODO: draw 3d | |
| } | |
| // blueprint for vector | |
| class Vec2 { | |
| constructor(x, y) { | |
| this.x = x; | |
| this.y = y; | |
| } | |
| add(v) { | |
| this.x += v.x; | |
| this.y += v.y; | |
| } | |
| static add(a, b){ | |
| let result = new Vec2(0, 0); | |
| result.x = a.x + b.x; | |
| result.y = a.y + b.y; | |
| return result; | |
| } | |
| sub(v) { | |
| this.x -= v.x; | |
| this.y -= v.y; | |
| } | |
| static sub(a, b){ | |
| let result = new Vec2(0, 0); | |
| result.x = a.x - b.x; | |
| result.y = a.y - b.y; | |
| return result; | |
| } | |
| scale(n){ | |
| this.x *= n; | |
| this.y *= n; | |
| } | |
| dot(v) { | |
| return ((this.x * v.x) + (this.y * v.y)) | |
| } | |
| perpendicular(){ | |
| let result = new Vec2(this.y, -this.x); | |
| } | |
| mag() { | |
| return Math.sqrt((this.x * this.x) + (this.y * this.y)); | |
| } | |
| normalize() { | |
| let len = this.mag(); | |
| this.x /= len; | |
| this.y /= len; | |
| } | |
| rotate(angle) { | |
| let result = new Vec2(0, 0); | |
| result.x = this.x * Math.cos(angle) - this.y * Math.sin(angle); | |
| result.y = this.x * Math.sin(angle) - this.y * Math.cos(angle); | |
| return result | |
| } | |
| draw(color) { | |
| fill(color); | |
| stroke("white") | |
| line(0, 0, this.x, this.y) | |
| circle(this.x, this.y, 15); | |
| } | |
| } | |
| // create new vec2 | |
| let position = new Vec2(100, 0); | |
| // create new vec2 | |
| let velocity = new Vec2(1, 2); | |
| // angle | |
| let angle = 0.0; | |
| // // create new vec3 | |
| // let position3 = new Vec3(10, 20, -2); | |
| // // create new vec3 | |
| // let velocity3 = new Vec3(2, 1, 0); | |
| function setup() { | |
| createCanvas(windowWidth, windowHeight); | |
| //position.scale(5.0); | |
| } | |
| function draw() { | |
| translate(windowWidth /2, windowHeight /2); | |
| background("black"); | |
| // position = Vec2.add(position, velocity) | |
| // position.draw("red"); | |
| // position.add(velocity) | |
| // position.x = mouseX; | |
| // position.y = mouseY; | |
| // position.draw("white"); | |
| // console.log(position.mag()) | |
| // position3.add(velocity3); | |
| // console.log("position3: ", position3); | |
| // position.normalize(); | |
| // console.log("The length of the vector is now: ", position.mag()); | |
| let rotatedPosition = position.rotate(angle); | |
| rotatedPosition.draw("red"); | |
| angle += 0.03; | |
| } | |
| // cont-> 30. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment