2018-08-05 18:19:48 +02:00
|
|
|
import Point from "./point";
|
|
|
|
import { round } from "./round";
|
2018-07-14 16:04:39 +00:00
|
|
|
|
2018-07-23 11:12:06 +00:00
|
|
|
/** Returns internal hook name for a macro */
|
2018-07-23 11:44:34 +00:00
|
|
|
export function macroName(name) {
|
2018-07-23 11:12:06 +00:00
|
|
|
return `_macro_${name}`;
|
2018-07-12 12:53:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 16:04:39 +00:00
|
|
|
/** Find intersection of two (endless) lines */
|
2018-07-23 11:44:34 +00:00
|
|
|
export function beamsCross(a1, a2, b1, b2) {
|
2018-07-14 16:04:39 +00:00
|
|
|
let slopeA = a1.slope(a2);
|
|
|
|
let slopeB = b1.slope(b2);
|
2018-07-23 11:44:34 +00:00
|
|
|
if (slopeA === slopeB) return false; // Parallel lines
|
2018-07-14 16:04:39 +00:00
|
|
|
|
2018-07-23 11:44:34 +00:00
|
|
|
if (a1.x === a2.x)
|
2018-08-05 18:19:48 +02:00
|
|
|
return new Point(a1.x, slopeB * a1.x + (b1.y - slopeB * b1.x));
|
2018-07-23 11:44:34 +00:00
|
|
|
// Vertical line A
|
|
|
|
else if (b1.x === b2.x)
|
2018-08-05 18:19:48 +02:00
|
|
|
return new Point(b1.x, slopeA * b1.x + (a1.y - slopeA * a1.x));
|
2018-07-23 11:44:34 +00:00
|
|
|
// Vertical line B
|
2018-07-14 16:04:39 +00:00
|
|
|
else {
|
|
|
|
// Swap points if line A or B goes from right to left
|
2018-08-13 11:00:51 +02:00
|
|
|
if (a1.x > a2.x) a1 = a2.copy();
|
|
|
|
if (b1.x > b2.x) b1 = b2.copy();
|
2018-07-14 16:04:39 +00:00
|
|
|
// Find y intercept
|
2018-07-23 11:44:34 +00:00
|
|
|
let iA = a1.y - slopeA * a1.x;
|
|
|
|
let iB = b1.y - slopeB * b1.x;
|
2018-07-14 16:04:39 +00:00
|
|
|
|
|
|
|
// Find intersection
|
|
|
|
let x = (iB - iA) / (slopeA - slopeB);
|
2018-07-15 17:15:56 +02:00
|
|
|
let y = slopeA * x + iA;
|
2018-07-14 16:04:39 +00:00
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
return new Point(x, y);
|
2018-07-14 16:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Find intersection of two line segments */
|
2018-07-23 11:44:34 +00:00
|
|
|
export function linesCross(a1, a2, b1, b2) {
|
|
|
|
let p = beamsCross(a1, a2, b1, b2);
|
2018-08-12 13:32:53 +02:00
|
|
|
if (!p) return false;
|
2018-08-13 10:57:35 +02:00
|
|
|
let lenA = a1.dist(a2);
|
|
|
|
let lenB = b1.dist(b2);
|
|
|
|
let lenC = a1.dist(p) + p.dist(a2);
|
|
|
|
let lenD = b1.dist(p) + p.dist(b2);
|
|
|
|
if (round(lenA) == round(lenC) && round(lenB) == round(lenD)) return p;
|
|
|
|
else return false;
|
2018-07-14 16:04:39 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 16:18:16 +02:00
|
|
|
/** Find where an (endless) line crosses a certain X-value */
|
|
|
|
export function beamCrossesX(from, to, x) {
|
|
|
|
if (from.x === to.x) return false; // Vertical line
|
|
|
|
let top = new Point(x, -10);
|
|
|
|
let bottom = new Point(x, 10);
|
|
|
|
|
|
|
|
return beamsCross(from, to, top, bottom);
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:02:04 +00:00
|
|
|
/** Find where an (endless) line crosses a certain Y-value */
|
2018-07-23 11:44:34 +00:00
|
|
|
export function beamCrossesY(from, to, y) {
|
|
|
|
if (from.y === to.y) return false; // Horizontal line
|
2018-08-05 18:19:48 +02:00
|
|
|
let left = new Point(-10, y);
|
|
|
|
let right = new Point(10, y);
|
2018-07-19 14:02:04 +00:00
|
|
|
|
|
|
|
return beamsCross(from, to, left, right);
|
|
|
|
}
|
|
|
|
|
2018-08-01 14:55:54 +02:00
|
|
|
/** Convert value in mm to cm or imperial units */
|
|
|
|
export function units(value, to = "metric") {
|
|
|
|
if (to === "imperial") return round(value / 25.4) + '"';
|
|
|
|
else return round(value / 10) + "cm";
|
|
|
|
}
|