Ergogen reorg

This commit is contained in:
Bán Dénes 2020-06-16 22:24:46 +02:00
parent cd1e10293c
commit 0ab5a246e5
14 changed files with 2289 additions and 309 deletions

44
src/utils.js Normal file
View file

@ -0,0 +1,44 @@
const m = require('makerjs')
exports.deepcopy = (value) => JSON.parse(JSON.stringify(value))
const eq = exports.eq = (a=[], b=[]) => {
return a[0] === b[0] && a[1] === b[1]
}
const line = exports.line = (a, b) => {
return new m.paths.Line(a, b)
}
exports.circle = (p, r) => {
return new m.paths.Circle(p, r)
}
exports.rect = (w, h, o=[0, 0], mirrored=false) => {
const res = {
top: line([0, h], [w, h]),
right: line([w, h], [w, 0]),
bottom: line([w, 0], [0, 0]),
left: line([0, 0], [0, h])
}
if (mirrored) {
for (const segment of Object.values(res)) {
[segment.origin, segment.end] = [segment.end, segment.origin]
}
}
return m.model.move({paths: res}, o)
}
exports.poly = (arr) => {
let counter = 0
let prev = arr[arr.length - 1]
const res = {
paths: {}
}
for (const p of arr) {
if (eq(prev, p)) continue
res.paths['p' + (++counter)] = line(prev, p)
prev = p
}
return res
}