Public
Edited
Oct 2, 2024
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
graph = {
const svg = d3.create('svg')
.attr("width", vWidth)
.attr("height", vHeight)
.attr("viewBox", [0, 0, vWidth, vHeight])
const delaunay = new d3.Delaunay(points)
const voronoi = delaunay.voronoi([0, 0, vWidth, vHeight])

//debug cell
svg.append('path')
.attr('fill', 'lightblue')
.attr('d', voronoi.renderCell(debugCellA))
svg.append('path')
.attr('fill', 'yellow')
.attr('d', voronoi.renderCell(debugCellB))
const divisions = svg.append('path')
.attr('stroke', 'gray')
.attr('stroke-width', '2')
.attr('d', voronoi.render())
const vorNeighbors = []
const notVorNeighbors = []

// filter links by iterate neighbors of every delaunay point
for (let i = 0; i< points.length/2; i++){
const vNeighbors = [...voronoi.neighbors(i)]
for (let neighbor of delaunay.neighbors(i)){
if (vNeighbors.indexOf(neighbor) < 0){
// delaunay-only
notVorNeighbors.push({
source: i,
target: neighbor
})
}else{
vorNeighbors.push({
source: i,
target: neighbor
})
}
}
}

const notVorLinks = svg.append('g')
.selectAll('line')
.data(notVorNeighbors)
.join('line')
.attr('stroke', 'red') // delaunay-only are red
.attr('x1', d=>delaunay.points[d.source*2])
.attr('y1', d=>delaunay.points[d.source*2+1])
.attr('x2', d=>delaunay.points[d.target*2])
.attr('y2', d=>delaunay.points[d.target*2+1])
const vorLinks = svg.append('g')
.selectAll('line')
.data(vorNeighbors)
.join('line')
.attr('stroke', 'green') // green
.attr('x1', d=>delaunay.points[d.source*2])
.attr('y1', d=>delaunay.points[d.source*2+1])
.attr('x2', d=>delaunay.points[d.target*2])
.attr('y2', d=>delaunay.points[d.target*2+1])

const dots = svg.append('path')
.attr('d', delaunay.renderPoints())
const texts = svg.append('g')
for (let i = 0; i< points.length/2; i++){
texts.append('text')
.attr('x', delaunay.points[i*2]+5)
.attr('y', delaunay.points[i*2+1])
.text(i)
}
return {
node: svg.node(),
delaunay,
voronoi,
}
}
Insert cell
mirroredPoints = (x, y)=>{
return [
x, y,
vWidth-x, y,
x, vHeight-y,
vWidth-x, vHeight-y,
]
}
Insert cell
points = [
...mirroredPoints(vWidth * r1, vHeight * r1),
...mirroredPoints(vWidth * r2, vHeight * r2),
vWidth/2, vHeight/2
]
Insert cell
vWidth = size
Insert cell
vHeight = size
Insert cell
d3 = fix ? require("d3@7", await FileAttachment("d3-delaunay.min.js").url()) : require("d3@7.8.2")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more