2018-07-23 11:12:06 +00:00
|
|
|
import attributes from './attributes'
|
2018-07-11 12:45:02 +00:00
|
|
|
|
2018-07-23 11:12:06 +00:00
|
|
|
function point (x, y)
|
|
|
|
{
|
|
|
|
this.x = this.round(x);
|
|
|
|
this.y = this.round(y);
|
|
|
|
this.attributes = new attributes();
|
2018-07-14 16:04:39 +00:00
|
|
|
|
2018-07-23 11:12:06 +00:00
|
|
|
return this;
|
2018-07-11 12:45:02 +00:00
|
|
|
|
2018-07-23 11:12:06 +00:00
|
|
|
/** Rounds a value to PRECISION */
|
|
|
|
this.prototype.round = function (value)
|
|
|
|
{
|
|
|
|
return Math.round(value * 1e2) / 1e2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Radians to degrees */
|
|
|
|
this.prototype.rad2deg = function (radians)
|
|
|
|
{
|
|
|
|
return radians * 57.29577951308232;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Degrees to radians */
|
|
|
|
this.prototype.deg2rad (degrees)
|
|
|
|
{
|
|
|
|
return degrees / 57.29577951308232;
|
2018-07-11 12:45:02 +00:00
|
|
|
}
|
2018-07-12 07:37:52 +00:00
|
|
|
|
2018-07-22 18:22:51 +02:00
|
|
|
/** Adds an attribute. This is here to make this call chainable in assignment */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.attr = function (name, value)
|
|
|
|
{
|
2018-07-22 18:22:51 +02:00
|
|
|
this.attributes.add(name, value);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-07-12 07:37:52 +00:00
|
|
|
/** Returns the distance between this point and that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.dist = function (that)
|
|
|
|
{
|
2018-07-12 07:37:52 +00:00
|
|
|
let dx = this.x - that.x;
|
|
|
|
let dy = this.y - that.y;
|
|
|
|
|
2018-07-23 11:12:06 +00:00
|
|
|
return this.round(Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)));
|
2018-07-12 07:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns slope of a line made by this point and that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.slope = function (that)
|
|
|
|
{
|
2018-07-12 07:37:52 +00:00
|
|
|
return (that.y - this.y) / (that.x - this.x);
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:53:49 +00:00
|
|
|
/** Returns the x-delta between this point and that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.dx = function (that)
|
|
|
|
{
|
2018-07-12 12:53:49 +00:00
|
|
|
return that.x - this.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the y-delta between this point and that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.dy = function (that)
|
|
|
|
{
|
2018-07-12 12:53:49 +00:00
|
|
|
return that.y - this.y;
|
|
|
|
}
|
|
|
|
|
2018-07-12 07:37:52 +00:00
|
|
|
/** Returns the angle between this point and that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.angle = function (that)
|
|
|
|
{
|
2018-07-12 12:53:49 +00:00
|
|
|
let rad = Math.atan2(-1 * this.dy(that), this.dx(that));
|
2018-07-12 07:37:52 +00:00
|
|
|
while (rad < 0) rad += 2 * Math.PI;
|
|
|
|
|
2018-07-23 11:12:06 +00:00
|
|
|
return this.rad2deg(rad);
|
2018-07-12 07:37:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 12:53:49 +00:00
|
|
|
/** Rotate this point deg around that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.rotate = function (deg, that)
|
|
|
|
{
|
2018-07-12 12:53:49 +00:00
|
|
|
let radius = this.dist(that);
|
2018-07-12 07:37:52 +00:00
|
|
|
let angle = this.angle(that);
|
2018-07-23 11:12:06 +00:00
|
|
|
let x = that.x + radius * Math.cos(this.deg2rad(angle + deg)) * -1;
|
|
|
|
let y = that.y + radius * Math.sin(this.deg2rad(angle + deg));
|
2018-07-12 07:37:52 +00:00
|
|
|
|
|
|
|
return new Point(x, y);
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:53:49 +00:00
|
|
|
/** returns an identical copy of this point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.copy = function ()
|
|
|
|
{
|
2018-07-12 12:53:49 +00:00
|
|
|
return new Point(this.x, this.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** checks whether this point is equal to that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.equals = function (that)
|
|
|
|
{
|
2018-07-12 12:53:49 +00:00
|
|
|
return (this.x === that.x && this.y === that.y) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Mirrors this point around X value of that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.flipX = function (that)
|
2018-07-12 12:53:49 +00:00
|
|
|
{
|
|
|
|
return new Point(that.x + this.dx(that), that.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Mirrors this point around Y value of that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.flipY = function (that)
|
2018-07-12 12:53:49 +00:00
|
|
|
{
|
|
|
|
return new Point(that.x, that.y + this.dy(that));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Shifts this point distance in the deg direction */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.shift = function (deg, distance)
|
|
|
|
{
|
2018-07-12 07:37:52 +00:00
|
|
|
let p = this.copy();
|
|
|
|
p.x += distance;
|
|
|
|
|
2018-07-12 12:53:49 +00:00
|
|
|
return p.rotate(deg, this);
|
2018-07-12 07:37:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 12:53:49 +00:00
|
|
|
/** Shifts this point distance in the direction of that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.shiftTowards = function (that, distance)
|
|
|
|
{
|
2018-07-12 12:53:49 +00:00
|
|
|
return this.shift(this.angle(that), distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Shifts this point fraction of the distance towards that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.shiftFractionTowards = function (that, fraction)
|
|
|
|
{
|
2018-07-12 12:53:49 +00:00
|
|
|
return this.shiftTowards(that, this.dist(that) * fraction);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Shifts this point distance beyond that point */
|
2018-07-23 11:12:06 +00:00
|
|
|
this.prototype.shiftOutwards = function (that, distance)
|
|
|
|
{
|
2018-07-12 12:53:49 +00:00
|
|
|
return this.shiftTowards(that, this.dist(that) + distance);
|
2018-07-12 07:37:52 +00:00
|
|
|
}
|
2018-07-11 12:45:02 +00:00
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
|
|
|
|
export default point;
|