1
0
Fork 0
freesewing/src/utils.js

112 lines
3 KiB
JavaScript
Raw Normal View History

import point from "./point";
2018-07-14 16:04:39 +00:00
2018-07-23 11:12:06 +00:00
/** Returns internal hook name for a macro */
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 */
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);
if (slopeA === slopeB) return false; // Parallel lines
2018-07-14 16:04:39 +00:00
if (a1.x === a2.x)
return new point(a1.x, slopeB * a1.x + (b1.y - slopeB * b1.x));
// Vertical line A
else if (b1.x === b2.x)
return new point(b1.x, slopeA * b1.x + (a1.y - slopeA * a1.x));
// Vertical line B
2018-07-14 16:04:39 +00:00
else {
// Swap points if line A or B goes from right to left
if (a1.x > a2.x) {
2018-07-14 16:04:39 +00:00
let tmp = a1.copy();
a1 = a2.copy();
a2 = tmp;
}
if (b1.x > b2.x) {
2018-07-14 16:04:39 +00:00
let tmp = b1.copy();
b1 = b2.copy();
b2 = tmp;
}
// Find y intercept
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);
let y = slopeA * x + iA;
2018-07-14 16:04:39 +00:00
2018-07-23 11:12:06 +00:00
return new point(x, y);
2018-07-14 16:04:39 +00:00
}
}
/** Find intersection of two line segments */
export function linesCross(a1, a2, b1, b2) {
let p = beamsCross(a1, a2, b1, b2);
if (p) {
2018-07-14 16:04:39 +00: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;
}
return false;
}
2018-07-19 14:02:04 +00:00
/** Find where an (endless) line crosses a certain Y-value */
export function beamCrossesY(from, to, y) {
if (from.y === to.y) return false; // Horizontal line
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);
}
/** Returns an object with shorthand access for pattern design */
2018-07-25 14:53:10 +00:00
export function shorthand(part) {
let final = part.context.settings.mode === "draft" ? true : false;
let paperless = part.context.settings.paperless === true ? true : false;
2018-07-19 14:02:04 +00:00
return {
2018-07-25 14:53:10 +00:00
measurements: part.context.settings.measurements || {},
options: part.context.options || {},
values: part.context.values || {},
2018-07-19 14:02:04 +00:00
points: part.points || {},
paths: part.paths || {},
snippets: part.snippets || {},
macro: part.macroRunner(),
2018-07-19 14:02:04 +00:00
final,
paperless
};
2018-07-19 14:02:04 +00:00
}
2018-07-26 13:43:12 +00:00
/** Offsets a line by distance */
export function offsetLine(from, to, distance) {
if (from.x === to.x && from.y === to.y) {
throw "Cannot offset line that starts and ends in the same point";
}
let angle = from.angle(to) + 90;
console.log("offsetting line from", from, "to", to, "angle is", angle);
return {
from: from.shift(angle, distance),
to: to.shift(angle, distance)
};
}
/** Offsets a path by distance */
export function pathOffset(path, distance) {
let offset = [];
let current;
for (let i in path.ops) {
let op = path.ops[i];
if (op.type === "line") {
offset.push(offsetLine(current, op.to, distance));
} else if (op.type === "curve") {
//offset.push(utils.offsetLine(current, op.to);
}
current = op.to;
}
//let orig = new Bezier(
console.log(offset);
}