Published
Edited
Sep 5, 2018
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function complicateLine(ztolerance, x1, y1, x2, y2) {
var constructionOrder = 1
var cl = function(i, l, r) {
// initialize the default return value for when there are no new midpoints
let ps = [l, r]
let x1 = l[0], y1 = l[1], x2 = r[0], y2 = r[1]
// if the segment is longer than the tolerance, recursively add midpoints
// https://en.wikipedia.org/wiki/Euclidean_distance#Two_dimensions
if(Math.sqrt(Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2)) > ztolerance) {
// https://en.wikipedia.org/wiki/Midpoint#Formulas
let mp = [(x1+x2)/2, (y1+y2)/2]
// include the constructon order for visualization
mp.push(constructionOrder+=1)
// process the left and right segments
let lm = cl(i, l, mp)
let rm = cl(i, mp, r)
// join the two arrays and remove the overlapping midpoint
ps = lm.concat(rm.slice(1))
}
return ps
}
return cl(0, [x1, y1, 0], [x2, y2, 1])
}
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more