1
0
Fork 0

💥 Switched to Capitals for constructors

Also moved round method to avoid circular dependency
This commit is contained in:
Joost De Cock 2018-08-05 18:19:48 +02:00
parent 718c9acda5
commit 6167db92ac
13 changed files with 250 additions and 249 deletions

View file

@ -1,24 +1,24 @@
import attributes from "./attributes";
import { round } from "./utils";
import Attributes from "./attributes";
import { round } from "./round";
function point(x, y) {
function Point(x, y) {
this.x = round(x);
this.y = round(y);
this.attributes = new attributes();
this.attributes = new Attributes();
}
/** Radians to degrees */
point.prototype.rad2deg = function(radians) {
Point.prototype.rad2deg = function(radians) {
return radians * 57.29577951308232;
};
/** Degrees to radians */
point.prototype.deg2rad = function(degrees) {
Point.prototype.deg2rad = function(degrees) {
return degrees / 57.29577951308232;
};
/** Adds an attribute. This is here to make this call chainable in assignment */
point.prototype.attr = function(name, value, overwrite = false) {
Point.prototype.attr = function(name, value, overwrite = false) {
if (overwrite) this.attributes.set(name, value);
else this.attributes.add(name, value);
@ -26,7 +26,7 @@ point.prototype.attr = function(name, value, overwrite = false) {
};
/** Returns the distance between this point and that point */
point.prototype.dist = function(that) {
Point.prototype.dist = function(that) {
let dx = this.x - that.x;
let dy = this.y - that.y;
@ -34,22 +34,22 @@ point.prototype.dist = function(that) {
};
/** Returns slope of a line made by this point and that point */
point.prototype.slope = function(that) {
Point.prototype.slope = function(that) {
return (that.y - this.y) / (that.x - this.x);
};
/** Returns the x-delta between this point and that point */
point.prototype.dx = function(that) {
Point.prototype.dx = function(that) {
return that.x - this.x;
};
/** Returns the y-delta between this point and that point */
point.prototype.dy = function(that) {
Point.prototype.dy = function(that) {
return that.y - this.y;
};
/** Returns the angle between this point and that point */
point.prototype.angle = function(that) {
Point.prototype.angle = function(that) {
let rad = Math.atan2(-1 * this.dy(that), this.dx(that));
while (rad < 0) rad += 2 * Math.PI;
@ -57,37 +57,37 @@ point.prototype.angle = function(that) {
};
/** Rotate this point deg around that point */
point.prototype.rotate = function(deg, that) {
Point.prototype.rotate = function(deg, that) {
let radius = this.dist(that);
let angle = this.angle(that);
let x = that.x + radius * Math.cos(this.deg2rad(angle + deg)) * -1;
let y = that.y + radius * Math.sin(this.deg2rad(angle + deg));
return new point(x, y);
return new Point(x, y);
};
/** returns an identical copy of this point */
point.prototype.copy = function() {
return new point(this.x, this.y);
Point.prototype.copy = function() {
return new Point(this.x, this.y);
};
/** checks whether this point is equal to that point */
point.prototype.equals = function(that) {
Point.prototype.equals = function(that) {
return this.x === that.x && this.y === that.y ? true : false;
};
/** Mirrors this point around X value of that point */
point.prototype.flipX = function(that) {
return new point(that.x + this.dx(that), that.y);
Point.prototype.flipX = function(that) {
return new Point(that.x + this.dx(that), that.y);
};
/** Mirrors this point around Y value of that point */
point.prototype.flipY = function(that) {
return new point(that.x, that.y + this.dy(that));
Point.prototype.flipY = function(that) {
return new Point(that.x, that.y + this.dy(that));
};
/** Shifts this point distance in the deg direction */
point.prototype.shift = function(deg, distance) {
Point.prototype.shift = function(deg, distance) {
let p = this.copy();
p.x += distance;
@ -95,26 +95,26 @@ point.prototype.shift = function(deg, distance) {
};
/** Shifts this point distance in the direction of that point */
point.prototype.shiftTowards = function(that, distance) {
Point.prototype.shiftTowards = function(that, distance) {
return this.shift(this.angle(that), distance);
};
/** Shifts this point fraction of the distance towards that point */
point.prototype.shiftFractionTowards = function(that, fraction) {
Point.prototype.shiftFractionTowards = function(that, fraction) {
return this.shiftTowards(that, this.dist(that) * fraction);
};
/** Shifts this point distance beyond that point */
point.prototype.shiftOutwards = function(that, distance) {
Point.prototype.shiftOutwards = function(that, distance) {
return this.shiftTowards(that, this.dist(that) + distance);
};
/** Returns a deep copy of this */
point.prototype.clone = function() {
let clone = new point(this.x, this.y);
Point.prototype.clone = function() {
let clone = new Point(this.x, this.y);
clone.attributes = this.attributes.clone();
return clone;
};
export default point;
export default Point;