JSFiddle - React, Tailwind, and code Playground
by bornander
HTML
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="800" height="600"></canvas>
<script>
function fltEquals(lhs, rhs) {
return Math.abs(lhs - rhs) < 0.00001;
}
function fltRound4(num) {
return Math.round(num * 10000.0) / 10000.0;
}
var TWO_PI = Math.PI * 2;
var HALF_PI = Math.PI / 2;
function Vector2D(x, y) {
this.x = x;
this.y = y;
}
Vector2D.prototype.str = function() {
return "(" + this.x + ", " + this.y + ")";
};
Vector2D.prototype.clear = function() {
this.x = 0;
this.y = 0;
};
Vector2D.prototype.length = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
Vector2D.prototype.equals = function(other) {
return fltEquals(this.x, other.x) && fltEquals(this.y, other.y);
};
Vector2D.normalize = function(v) {
var l = v.length();
return new Vector2D(v.x / l, v.y / l);
};
Vector2D.add = function(lhs, rhs) {
return new Vector2D(lhs.x + rhs.x, lhs.y + rhs.y);
};
Vector2D.sub = function(lhs, rhs) {
return new Vector2D(lhs.x - rhs.x, lhs.y - rhs.y);
};
Vector2D.mul = function(lhs, factor) {
return new Vector2D(lhs.x * factor, lhs.y * factor);
};
Vector2D.dot = function(lhs, rhs) {
return lhs.x * rhs.x + lhs.y * rhs.y;
};
/* not an actual cross-product */
Vector2D.cross = function(vector) {
return new Vector2D(vector.y, -vector.x);
};
Vector2D.distanceBetween = function(lhs, rhs) {
var...