'use strict';
const Rac = require('../Rac');
const utils = require('../util/utils');
module.exports = function attachRayFunctions(rac) {
/**
* Returns a new `Point` located where the ray touches the canvas edge.
*
* When the ray is outside the canvas and pointing away, `null` is
* returned.
*
* Added to `Rac.Ray.prototype` when `{@link Rac.P5Drawer}` is setup as
* `[rac.drawer]{@link Rac#drawer}`.
* @returns {?Rac.Point}
*/
Rac.Ray.prototype.pointAtCanvasEdge = function(margin = 0) {
let edgeRay = this.rayAtCanvasEdge(margin);
if (edgeRay == null) {
return null;
}
return edgeRay.start;
};
/**
* Returns a new `Ray` that starts at the point where the `this` touches
* the canvas edge and pointed towards the inside of the canvas.
*
* When the ray is outside the canvas and pointing away, `null` is
* returned.
*
* Added to `Rac.Ray.prototype` when `{@link Rac.P5Drawer}` is setup as
* `[rac.drawer]{@link Rac#drawer}`.
*
* @returns {?Rac.Ray}
*/
Rac.Ray.prototype.rayAtCanvasEdge = function(margin = 0) {
const turn = this.angle.turn;
const p5 = this.rac.drawer.p5;
const downEdge = p5.height - margin;
const leftEdge = margin;
const upEdge = margin;
const rightEdge = p5.width - margin;
// pointing down
if (turn >= 1/8 && turn < 3/8) {
let edgeRay = null;
if (this.start.y < downEdge) {
edgeRay = this.pointAtY(downEdge).ray(this.rac.Angle.up);
if (edgeRay.start.x > rightEdge) {
edgeRay = this.pointAtX(rightEdge).ray(this.rac.Angle.left);
} else if (edgeRay.start.x < leftEdge) {
edgeRay = this.pointAtX(leftEdge).ray(this.rac.Angle.right);
}
}
return edgeRay;
}
// pointing left
if (turn >= 3/8 && turn < 5/8) {
let edgeRay = null;
if (this.start.x >= leftEdge) {
edgeRay = this.pointAtX(leftEdge).ray(this.rac.Angle.right);
if (edgeRay.start.y > downEdge) {
edgeRay = this.pointAtY(downEdge).ray(this.rac.Angle.up);
} else if (edgeRay.start.y < upEdge) {
edgeRay = this.pointAtY(upEdge).ray(this.rac.Angle.down);
}
}
return edgeRay;
}
// pointing up
if (turn >= 5/8 && turn < 7/8) {
let edgeRay = null;
if (this.start.y >= upEdge) {
edgeRay = this.pointAtY(upEdge).ray(this.rac.Angle.down);
if (edgeRay.start.x > rightEdge) {
edgeRay = this.pointAtX(rightEdge).ray(this.rac.Angle.left);
} else if (edgeRay.start.x < leftEdge) {
edgeRay = this.pointAtX(leftEdge).ray(this.rac.Angle.right);
}
}
return edgeRay;
}
// pointing right
let edgeRay = null;
if (this.start.x < rightEdge) {
edgeRay = this.pointAtX(rightEdge).ray(this.rac.Angle.left);
if (edgeRay.start.y > downEdge) {
edgeRay = this.pointAtY(downEdge).ray(this.rac.Angle.up);
} else if (edgeRay.start.y < upEdge) {
edgeRay = this.pointAtY(upEdge).ray(this.rac.Angle.down);
}
}
return edgeRay;
};
} // attachRayFunctions