JSFiddle - React, Tailwind, and code Playground

by Darby Rathbone

JavaScript

var Vector = (function () {
    Vector.add = function (v1, v2) {
        return new Vector(v1.x + v2.x, v1.y + v2.y)
    };
    Vector.sub = function (v1, v2) {
        return new Vector(v1.x - v2.x, v1.y - v2.y)
    };
    Vector.project = function (v1, v2) {
        return v1.clone().scale((v1.dot(v2)) / v1.magSq())
    };
    function Vector(x, y) {
        this.x = x != null ? x : 0;
        this.y = y != null ? y : 0
    }
    Vector.prototype.set = function (x, y) {
        this.x = x;
        this.y = y;
        return this
    };
    Vector.prototype.add = function (v) {
        this.x += v.x;
        this.y += v.y;
        return this
    };
    Vector.prototype.sub = function (v) {
        this.x -= v.x;
        this.y -= v.y;
        return this
    };
    Vector.prototype.scale = function (f) {
        this.x *= f;
        this.y *= f;
        return this
    };
    Vector.prototype.dot = function (v) {
        return this.x * v.x + this.y * v.y
    };
    Vector.prototype.cross = function (v) {
        return (this.x * v.y) - (this.y * v.x)
    };
    Vector.prototype.mag = function () {
        return Math.sqrt(this.x * this.x + this.y * this.y)
    };
    Vector.prototype.magSq = function () {
        return this.x * this.x + this.y * this.y
    };
    Vector.prototype.dist = function (v) {
        var dx, dy;
        dx = v.x - this.x;
        dy = v.y - this.y;
        return Math.sqrt(dx * dx + dy * dy)
    };
    Vector.prototype.distSq = function (v) {
        var dx, dy;
        dx = v.x - this.x;
        dy = v.y - this.y;
        return dx * dx + dy * dy
    };
    Vector.prototype.norm = function () {
        var m;
        m = Math.sqrt(this.x * this.x + this.y * this.y);
        this.x /= m;
        this.y /= m;
        return this
    };
    Vector.prototype.limit = function (l) {
        var m, mSq;
        mSq = this.x * this.x + this.y * this.y;
        if (mSq > l * l) {
            m = Math.sqrt(mSq);
            this.x /= m;
     ...