4230-hw-1/Pendulum/Homogeneous2D.js

39 lines
817 B
JavaScript
Raw Permalink Normal View History

2022-07-01 08:45:53 +00:00
// Homogeneous2D.js
// John Breaux 2022-07-01
// 2D Homogeneous coordinate library
2022-07-01 08:49:08 +00:00
"use strict";
2022-07-01 08:45:53 +00:00
//
// Classes
//
// Homogeneous2D: Stores a 2d vector or point in homogeneous coords
class Homogeneous2D {
constructor(x = 0, y = 0, w = 0) {
this.x = x;
this.y = y;
this.w = w;
}
// Add with modify
add_m(rhs) {
if (rhs) {
this.x += rhs.x;
this.y += rhs.y;
this.w += rhs.w;
return this;
}
else return null;
}
// copy
copy() {
return new Homogeneous2D(this.x, this.y, this.w);
}
// create vector/point from 2d polar coordinates
from_polar(r = 0, theta = 0, w = 0) {
this.x = r * Math.cos(theta + Math.PI / 2);
this.y = r * Math.sin(theta + Math.PI / 2);
this.w = w;
return this;
}
}