2019-08-03 15:03:33 +02:00
|
|
|
import Attributes from './attributes'
|
2018-07-11 12:45:02 +00:00
|
|
|
|
2020-07-19 13:01:01 +02:00
|
|
|
function Point(x, y, debug = false) {
|
2021-04-11 16:20:39 +02:00
|
|
|
this.x = x
|
|
|
|
this.y = y
|
2019-08-03 15:03:33 +02:00
|
|
|
this.attributes = new Attributes()
|
2020-07-23 11:54:55 +02:00
|
|
|
Object.defineProperty(this, 'debug', { value: debug, configurable: true })
|
2018-07-23 20:14:32 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 13:59:13 +02:00
|
|
|
/** Adds the raise method for a path not created through the proxy **/
|
|
|
|
Point.prototype.withRaise = function (raise = false) {
|
2020-07-23 10:26:04 +02:00
|
|
|
if (raise) Object.defineProperty(this, 'raise', { value: raise })
|
2020-07-21 13:59:13 +02:00
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2020-07-18 16:48:29 +02:00
|
|
|
/** Debug method to validate point data **/
|
|
|
|
Point.prototype.check = function () {
|
2020-07-23 11:54:55 +02:00
|
|
|
if (typeof this.x !== 'number') this.raise.warning('X value of `Point` is not a number')
|
|
|
|
if (typeof this.y !== 'number') this.raise.warning('Y value of `Point` is not a number')
|
2020-07-18 16:48:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
/** Radians to degrees */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.rad2deg = function (radians) {
|
2019-08-03 15:03:33 +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 */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.deg2rad = function (degrees) {
|
2019-08-03 15:03:33 +02:00
|
|
|
return degrees / 57.29577951308232
|
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Adds an attribute. This is here to make this call chainable in assignment */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.attr = function (name, value, overwrite = false) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
2019-08-03 15:03:33 +02:00
|
|
|
if (overwrite) this.attributes.set(name, value)
|
|
|
|
else this.attributes.add(name, value)
|
2018-07-23 17:35:06 +00:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this
|
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Returns the distance between this point and that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.dist = function (that) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
that.check()
|
2019-08-03 15:03:33 +02:00
|
|
|
let dx = this.x - that.x
|
|
|
|
let dy = this.y - that.y
|
2018-07-23 20:14:32 +02:00
|
|
|
|
2021-04-11 16:20:39 +02:00
|
|
|
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2))
|
2019-08-03 15:03:33 +02:00
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Returns slope of a line made by this point and that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.slope = function (that) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
that.check()
|
2019-08-03 15:03:33 +02:00
|
|
|
return (that.y - this.y) / (that.x - this.x)
|
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Returns the x-delta between this point and that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.dx = function (that) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
that.check()
|
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return that.x - this.x
|
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Returns the y-delta between this point and that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.dy = function (that) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
that.check()
|
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return that.y - this.y
|
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Returns the angle between this point and that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.angle = function (that) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
that.check()
|
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
let rad = Math.atan2(-1 * this.dy(that), this.dx(that))
|
|
|
|
while (rad < 0) rad += 2 * Math.PI
|
2018-07-23 20:14:32 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this.rad2deg(rad)
|
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Rotate this point deg around that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.rotate = function (deg, that) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
that.check()
|
|
|
|
if (typeof deg !== 'number')
|
|
|
|
this.raise.warning('Called `Point.rotate(deg,that)` but `deg` is not a number')
|
|
|
|
if (that instanceof Point !== true)
|
|
|
|
this.raise.warning('Called `Point.rotate(deg,that)` but `that` is not a `Point` object')
|
2019-08-03 15:03:33 +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))
|
2018-07-23 20:14:32 +02:00
|
|
|
|
2020-07-23 11:54:55 +02:00
|
|
|
return new Point(x, y, this.debug).withRaise(this.raise)
|
2019-08-03 15:03:33 +02:00
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** returns an identical copy of this point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.copy = function () {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
|
2020-07-23 11:54:55 +02:00
|
|
|
return new Point(this.x, this.y, this.debug).withRaise(this.raise)
|
2019-08-03 15:03:33 +02:00
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Mirrors this point around X value of that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.flipX = function (that = false) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
if (that) {
|
|
|
|
if (that instanceof Point !== true)
|
|
|
|
this.raise.warning('Called `Point.rotate(deg,that)` but `that` is not a `Point` object')
|
|
|
|
that.check()
|
2020-07-18 16:48:29 +02:00
|
|
|
}
|
2020-07-23 11:54:55 +02:00
|
|
|
if (that === false || that.x === 0)
|
|
|
|
return new Point(this.x * -1, this.y, this.debug).withRaise(this.raise)
|
|
|
|
else return new Point(that.x + this.dx(that), this.y, this.debug).withRaise(this.raise)
|
2019-08-03 15:03:33 +02:00
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Mirrors this point around Y value of that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.flipY = function (that = false) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
if (that) {
|
|
|
|
if (that instanceof Point !== true)
|
|
|
|
this.raise.warning('Called `Point.flipY(that)` but `that` is not a `Point` object')
|
|
|
|
that.check()
|
2020-07-18 16:48:29 +02:00
|
|
|
}
|
2020-07-23 11:54:55 +02:00
|
|
|
if (that === false || that.y === 0)
|
|
|
|
return new Point(this.x, this.y * -1, this.debug).withRaise(this.raise)
|
|
|
|
else return new Point(this.x, that.y + this.dy(that), this.debug).withRaise(this.raise)
|
2019-08-03 15:03:33 +02:00
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Shifts this point distance in the deg direction */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.shift = function (deg, distance) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
if (typeof distance !== 'number')
|
|
|
|
this.raise.warning('Called `Point.shift` but `distance` is not a number')
|
2019-08-03 15:03:33 +02:00
|
|
|
let p = this.copy()
|
|
|
|
p.x += distance
|
2018-07-23 20:14:32 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return p.rotate(deg, this)
|
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Shifts this point distance in the direction of that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.shiftTowards = function (that, distance) {
|
2022-01-28 18:18:37 +01:00
|
|
|
if (typeof distance !== 'number')
|
|
|
|
this.raise.warning('Called `Point.shiftTowards` but `distance` is not a number')
|
|
|
|
if (that instanceof Point !== true)
|
|
|
|
this.raise.warning(
|
|
|
|
'Called `Point.shiftTowards(that, distance)` but `that` is not a `Point` object'
|
|
|
|
)
|
|
|
|
this.check()
|
|
|
|
that.check()
|
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this.shift(this.angle(that), distance)
|
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
2018-08-06 20:43:19 +02:00
|
|
|
/** Checks whether this has the same coordinates as that */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.sitsOn = function (that) {
|
2022-01-28 18:18:37 +01:00
|
|
|
if (that instanceof Point !== true)
|
|
|
|
this.raise.warning('Called `Point.sitsOn(that)` but `that` is not a `Point` object')
|
|
|
|
this.check()
|
|
|
|
that.check()
|
2019-08-03 15:03:33 +02:00
|
|
|
if (this.x === that.x && this.y === that.y) return true
|
|
|
|
else return false
|
|
|
|
}
|
2018-08-06 20:43:19 +02:00
|
|
|
|
2018-08-21 13:49:12 +02:00
|
|
|
/** Checks whether this has roughly the same coordinates as that */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.sitsRoughlyOn = function (that) {
|
2022-01-28 18:18:37 +01:00
|
|
|
if (that instanceof Point !== true)
|
|
|
|
this.raise.warning('Called `Point.sitsRoughlyOn(that)` but `that` is not a `Point` object')
|
|
|
|
this.check()
|
|
|
|
that.check()
|
2019-08-03 15:03:33 +02:00
|
|
|
if (Math.round(this.x) === Math.round(that.x) && Math.round(this.y) === Math.round(that.y))
|
|
|
|
return true
|
|
|
|
else return false
|
|
|
|
}
|
2018-08-21 13:49:12 +02:00
|
|
|
|
2018-07-23 20:14:32 +02:00
|
|
|
/** Shifts this point fraction of the distance towards that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.shiftFractionTowards = function (that, fraction) {
|
2022-01-28 18:18:37 +01:00
|
|
|
if (that instanceof Point !== true)
|
|
|
|
this.raise.warning(
|
|
|
|
'Called `Point.shiftFractionTowards(that, fraction)` but `that` is not a `Point` object'
|
|
|
|
)
|
|
|
|
if (typeof fraction !== 'number')
|
|
|
|
this.raise.warning('Called `Point.shiftFractionTowards` but `fraction` is not a number')
|
|
|
|
this.check()
|
|
|
|
that.check()
|
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this.shiftTowards(that, this.dist(that) * fraction)
|
|
|
|
}
|
2018-07-23 20:14:32 +02:00
|
|
|
|
|
|
|
/** Shifts this point distance beyond that point */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.shiftOutwards = function (that, distance) {
|
2022-01-28 18:18:37 +01:00
|
|
|
if (that instanceof Point !== true)
|
|
|
|
this.raise.warning(
|
|
|
|
'Called `Point.shiftOutwards(that, distance)` but `that` is not a `Point` object'
|
|
|
|
)
|
|
|
|
if (typeof distance !== 'number')
|
|
|
|
this.raise.warning(
|
|
|
|
'Called `Point.shiftOutwards(that, distance)` but `distance` is not a number'
|
|
|
|
)
|
|
|
|
this.check()
|
|
|
|
that.check()
|
|
|
|
|
2019-08-03 15:03:33 +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 */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.clone = function () {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
const clone = new Point(this.x, this.y, this.debug).withRaise(this.raise)
|
2019-08-03 15:03:33 +02:00
|
|
|
clone.attributes = this.attributes.clone()
|
2018-08-03 14:20:28 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return clone
|
|
|
|
}
|
2018-08-03 14:20:28 +02:00
|
|
|
|
2018-09-06 15:32:43 +02:00
|
|
|
/** Applies a translate transform */
|
2020-07-18 16:48:29 +02:00
|
|
|
Point.prototype.translate = function (x, y) {
|
2022-01-28 18:18:37 +01:00
|
|
|
this.check()
|
|
|
|
if (typeof x !== 'number')
|
|
|
|
this.raise.warning('Called `Point.translate(x,y)` but `x` is not a number')
|
|
|
|
if (typeof y !== 'number')
|
|
|
|
this.raise.warning('Called `Point.translate(x,y)` but `y` is not a number')
|
|
|
|
const p = this.copy()
|
2019-08-03 15:03:33 +02:00
|
|
|
p.x += x
|
|
|
|
p.y += y
|
2018-09-06 15:32:43 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return p
|
|
|
|
}
|
2018-09-06 15:32:43 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
export default Point
|