ergogen/src/utils.js

45 lines
1 KiB
JavaScript
Raw Normal View History

2020-05-31 20:33:23 +02:00
const m = require('makerjs')
2020-06-16 22:24:46 +02:00
exports.deepcopy = (value) => JSON.parse(JSON.stringify(value))
2020-05-31 20:33:23 +02:00
2020-06-16 22:24:46 +02:00
const eq = exports.eq = (a=[], b=[]) => {
return a[0] === b[0] && a[1] === b[1]
2020-05-31 22:59:51 +02:00
}
const line = exports.line = (a, b) => {
return new m.paths.Line(a, b)
}
exports.circle = (p, r) => {
return new m.paths.Circle(p, r)
}
2020-06-02 22:59:05 +02:00
exports.rect = (w, h, o=[0, 0], mirrored=false) => {
const res = {
2020-05-31 22:59:51 +02:00
top: line([0, h], [w, h]),
right: line([w, h], [w, 0]),
bottom: line([w, 0], [0, 0]),
left: line([0, 0], [0, h])
2020-06-02 22:59:05 +02:00
}
if (mirrored) {
2020-06-16 22:24:46 +02:00
for (const segment of Object.values(res)) {
[segment.origin, segment.end] = [segment.end, segment.origin]
2020-06-02 22:59:05 +02:00
}
}
return m.model.move({paths: res}, o)
2020-05-31 22:59:51 +02:00
}
exports.poly = (arr) => {
let counter = 0
let prev = arr[arr.length - 1]
const res = {
paths: {}
}
for (const p of arr) {
2020-06-06 23:07:14 +02:00
if (eq(prev, p)) continue
2020-05-31 22:59:51 +02:00
res.paths['p' + (++counter)] = line(prev, p)
prev = p
}
return res
2020-06-02 22:59:05 +02:00
}