1
0
Fork 0
freesewing/packages/core/src/point.js

143 lines
3.9 KiB
JavaScript
Raw Normal View History

import Attributes from "./attributes";
import { round } from "./utils";
2018-07-11 12:45:02 +00:00
function Point(x, y) {
this.x = round(x);
this.y = round(y);
this.attributes = new Attributes();
2018-07-23 20:14:32 +02:00
}
/** Radians to degrees */
Point.prototype.rad2deg = function(radians) {
2018-07-23 20:14:32 +02:00
return radians * 57.29577951308232;
};
2018-07-14 16:04:39 +00:00
2018-07-23 20:14:32 +02:00
/** Degrees to radians */
Point.prototype.deg2rad = function(degrees) {
2018-07-23 20:14:32 +02:00
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) {
if (overwrite) this.attributes.set(name, value);
else this.attributes.add(name, value);
return this;
2018-07-23 20:14:32 +02:00
};
/** Returns the distance between this point and that point */
Point.prototype.dist = function(that) {
2018-07-23 20:14:32 +02:00
let dx = this.x - that.x;
let dy = this.y - that.y;
return round(Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)));
2018-07-23 20:14:32 +02:00
};
/** Returns slope of a line made by this point and that point */
Point.prototype.slope = function(that) {
2018-07-23 20:14:32 +02:00
return (that.y - this.y) / (that.x - this.x);
};
/** Returns the x-delta between this point and that point */
Point.prototype.dx = function(that) {
2018-07-23 20:14:32 +02:00
return that.x - this.x;
};
/** Returns the y-delta between this point and that point */
Point.prototype.dy = function(that) {
2018-07-23 20:14:32 +02:00
return that.y - this.y;
};
/** Returns the angle between this point and that point */
Point.prototype.angle = function(that) {
2018-07-23 20:14:32 +02:00
let rad = Math.atan2(-1 * this.dy(that), this.dx(that));
while (rad < 0) rad += 2 * Math.PI;
return this.rad2deg(rad);
};
/** Rotate this point deg around that point */
Point.prototype.rotate = function(deg, that) {
2018-07-23 20:14:32 +02:00
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);
2018-07-23 20:14:32 +02:00
};
/** returns an identical copy of this point */
Point.prototype.copy = function() {
return new Point(this.x, this.y);
2018-07-23 20:14:32 +02:00
};
/** Mirrors this point around X value of that point */
Point.prototype.flipX = function(that = false) {
2019-04-13 14:45:17 +02:00
if (that === false || that.x === 0) return new Point(this.x * -1, this.y);
else return new Point(that.x + this.dx(that), this.y);
2018-07-23 20:14:32 +02:00
};
/** Mirrors this point around Y value of that point */
Point.prototype.flipY = function(that = false) {
2019-04-13 14:45:17 +02:00
if (that === false || that.y === 0) return new Point(this.x, this.y * -1);
else return new Point(this.x, that.y + this.dy(that));
2018-07-23 20:14:32 +02:00
};
/** Shifts this point distance in the deg direction */
Point.prototype.shift = function(deg, distance) {
2018-07-23 20:14:32 +02:00
let p = this.copy();
p.x += distance;
return p.rotate(deg, this);
};
/** Shifts this point distance in the direction of that point */
Point.prototype.shiftTowards = function(that, distance) {
2018-07-23 20:14:32 +02:00
return this.shift(this.angle(that), distance);
};
/** Checks whether this has the same coordinates as that */
Point.prototype.sitsOn = function(that) {
if (this.x === that.x && this.y === that.y) return true;
else return false;
};
/** Checks whether this has roughly the same coordinates as that */
Point.prototype.sitsRoughlyOn = function(that) {
if (
Math.round(this.x) === Math.round(that.x) &&
Math.round(this.y) === Math.round(that.y)
)
return true;
else return false;
};
2018-07-23 20:14:32 +02:00
/** Shifts this point fraction of the distance towards that point */
Point.prototype.shiftFractionTowards = function(that, fraction) {
2018-07-23 20:14:32 +02:00
return this.shiftTowards(that, this.dist(that) * fraction);
};
/** Shifts this point distance beyond that point */
Point.prototype.shiftOutwards = function(that, distance) {
2018-07-23 20:14:32 +02:00
return this.shiftTowards(that, this.dist(that) + distance);
};
2018-07-23 11:12:06 +00:00
2018-08-03 14:20:28 +02:00
/** Returns a deep copy of this */
Point.prototype.clone = function() {
let clone = new Point(this.x, this.y);
2018-08-03 14:20:28 +02:00
clone.attributes = this.attributes.clone();
return clone;
};
/** Applies a translate transform */
Point.prototype.translate = function(x, y) {
let p = this.copy();
p.x += x;
p.y += y;
return p;
};
export default Point;