123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- class vec3
- {
- constructor(x, y, z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- mag()
- {
- return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z));
- }
- add(v)
- {
- this.x += v.x;
- this.y += v.y;
- this.z += v.z;
- }
- static add(v1, v2)
- {
- let result = new vec3(0, 0, 0);
- result.x = v1.x + v2.x;
- result.y = v1.y + v2.y;
- result.z = v1.z + v2.z;
- return result;
- }
- sub(v)
- {
- this.x -= v.x;
- this.y -= v.y;
- this.z -= v.z;
- }
- static sub(v1, v2)
- {
- return new vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
- }
- scale(n)
- {
- this.x *= n;
- this.y *= n;
- this.z *= n;
- }
- translate(n) { this.add(n); }
- rotateX(n) { /* TODO */ }
- rotateY(n) { /* TODO */ }
- rotateZ(n) { /* TODO */ }
- dot(v)
- {
- return (this.x * v.x) + (this.y + v.y) + (this.y * v.z)
- }
- cross(v)
- {
- return new vec3(this.y * v.z - this.z * v.y,
- this.z * v.x - this.x * v.z,
- this.x * v.y - this.y * v.x);
- }
- norm()
- {
- let m = this.mag();
- this.x /= m;
- this.y /= m;
- this.z /= m;
- }
- draw(colour)
- {
- fill(colour);
- circle(this.x, this.y, 10);
- }
- }
- class vec2
- {
- constructor(x, y)
- {
- this.x = x;
- this.y = y;
- }
- mag()
- {
- return Math.sqrt((this.x * this.x) + (this.y * this.y));
- }
- add(v)
- {
- this.x += v.x;
- this.y += v.y;
- }
- static add(v1, v2)
- {
- let result = new vec2(0, 0);
- result.x = v1.x + v2.x;
- result.y = v1.y + v2.y;
- return result;
- }
- sub(v)
- {
- this.x -= v.x;
- this.y -= v.y;
- }
- static sub(v1, v2)
- {
- return new vec2(v1.x - v2.x, v1.y - v2.y);
- }
- scale(n)
- {
- this.x *= n;
- this.y *= n;
- }
- translate(n) { this.add(n); }
- rotate(angle)
- {
- return new vec2(x * Math.cos(angle) - y * Math.sin(angle),
- this.y = x * Math.sin(angle) + y * Math.cos(angle));
- }
- dot(v)
- {
- return (this.x * v.x) + (this.y + v.y)
- }
- perp()
- {
- return new vec2(this.y, -this.x);
- }
- draw(colour)
- {
- fill(colour);
- circle(this.x, this.y, 10);
- }
- norm()
- {
- let m = this.mag();
- this.x /= m;
- this.y /= m;
- }
- }
- let position = new vec2(100, 100);
- let velocity = new vec2(1, 2);
- function setup()
- {
- createCanvas(windowWidth, windowHeight);
- }
- function draw()
- {
- background("black");
- position = position.rotate(0.1);
- position.draw("red");
- }
|