You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.6 KiB
JavaScript
71 lines
1.6 KiB
JavaScript
function distance([x1, y1], [x2, y2]) {
|
|
return Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
|
|
}
|
|
|
|
function midpoints([x1, y1], [x2, y2], count) {
|
|
const dx = x2 - x1
|
|
const dy = y2 - y1
|
|
|
|
const polyline = []
|
|
|
|
for (let i = 1; i < count; i++) {
|
|
const ratio = i / count
|
|
polyline.push([x1 + dx * ratio, y1 + dy * ratio])
|
|
}
|
|
|
|
return polyline
|
|
}
|
|
|
|
export function resampleCurve(curve, minDist, maxDist) {
|
|
if (curve.length === 0) return []
|
|
|
|
const newCurve = [curve[0]]
|
|
|
|
let lastPt = curve[0]
|
|
for (let i = 1; i < curve.length; i++) {
|
|
const pt = curve[i]
|
|
|
|
const segmentLength = distance(lastPt, pt)
|
|
|
|
if (segmentLength < minDist) {
|
|
// console.log('skipping point, segmentLength:', segmentLength)
|
|
continue
|
|
}
|
|
if (segmentLength > maxDist) {
|
|
// console.log('adding points, segmentLength:', segmentLength)
|
|
newCurve.push(...midpoints(lastPt, pt, Math.floor(segmentLength / maxDist)))
|
|
}
|
|
|
|
newCurve.push(pt)
|
|
lastPt = pt
|
|
}
|
|
|
|
if (lastPt !== curve.at(-1)) {
|
|
newCurve.push(curve.at(-1))
|
|
}
|
|
|
|
return newCurve
|
|
}
|
|
|
|
export function simplifyCurve(curve, minDistance) {
|
|
if (curve.length === 0) return []
|
|
|
|
const newCurve = [curve[0]]
|
|
|
|
let lastPt = curve[0]
|
|
for (let i = 1; i < curve.length; i++) {
|
|
const pt = curve[i]
|
|
|
|
if (distance(lastPt, pt) >= minDistance) {
|
|
newCurve.push(pt)
|
|
lastPt = pt
|
|
}
|
|
}
|
|
|
|
if (lastPt !== curve.at(-1)) {
|
|
newCurve.push(curve.at(-1))
|
|
}
|
|
|
|
return newCurve
|
|
}
|