Published
Edited
Jul 29, 2020
Importers
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
num = 100
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// width = 600;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
pointsToMesh = function(layer) {
let tx = -0;
let ty = -0;
let scale = .01;

const material = new THREE.MeshStandardMaterial({
color: 0xff3333
// opacity: 0.4
});
// just as with textures, we need to put colors into linear color space
material.color.convertSRGBToLinear();

var extrudeSettings = {
steps: 4,
depth: .1,
bevelEnabled: false
// bevelThickness: 1,
// bevelSize: .1,
// bevelOffset: 0,
// bevelSegments: 1
};

let meshes = layer.map((points, i) => {
let shape = new THREE.Shape();
extrudeSettings.depth =
Math.abs(Math.sin(((Math.PI * i) / layer.length) * 8)) * 0.4 + 0.05;
// console.log(tx + points[0].x * scale);
shape.moveTo(tx + points[0].x * scale, ty + points[0].y * scale);
points.forEach(d => {
let x = tx + d.x * scale;
let y = ty + d.y * scale;
shape.lineTo(x, y);
});
// shape.lineTo(tx + points[0].nx * scale, ty + points[0].ny * scale);
let geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
let mesh = new THREE.Mesh(geometry, material);
return mesh;
});
return meshes;
}
Insert cell
Insert cell
hexGridPaths = d3
.create("svg")
.selectAll("path")
.data(hexGrids)
.enter()
.append("path")
.attr("d", d => getHexGridPath(d, scale))
.nodes()
Insert cell
function getHexGridPath(vertices, scale) {
var baseString = vertices.map(vertex => {
const x = scale*vertex[0];
const y = scale*vertex[1];
const ring = vertex[2];
const sextant = vertex[3];
let circle = getPointsAroundACircle(getPointObject([x,y]),scale/Math.sqrt(3), 6, Math.PI / 6)
circle.push(circle[0])
// little trick to deduplicate edges in the lattice
if(ring % 2 === 1) {
circle = circle.filter((c,i) => (i === (sextant + 1) % 6) || i === ((sextant+ 2) % 6))
}
const stringy = circle
.map(coord => `L ${coord.x} ${coord.y}`)
.join('')
.substring(1);

return 'M' + stringy;
}).join('');
return baseString;
}
Insert cell
getHexGridPath(hexGrids[0], scale)
Insert cell
getHexGridCircles(hexGrids[0], scale)
Insert cell
function getHexGridCircles(vertices, scale) {
var baseString = vertices.map(vertex => {
const x = scale * vertex[0];
const y = scale * vertex[1];
const ring = vertex[2];
const sextant = vertex[3];
let circle = getPointsAroundACircle(
getPointObject([x, y]),
(scale / Math.sqrt(3)) * .7,
6,
Math.PI / 6
);
circle.push(circle[0]);
// little trick to deduplicate edges in the lattice
// if (ring % 2 === 1) {
// circle = circle.filter(
// (c, i) => i === (sextant + 1) % 6 || i === (sextant + 2) % 6
// );
// }
return circle;
});
return baseString;
}
Insert cell
Insert cell
function getPointsAroundACircle(circleCenterObject, circleRadius, numOfPoints, phase) {
var points = [];
for(let i = 0; i < numOfPoints; i++ ) {
var pointX = circleCenterObject.x + circleRadius * Math.cos(phase + i * 2 * Math.PI / numOfPoints);
var pointY = circleCenterObject.y + circleRadius * Math.sin(phase + i * 2 * Math.PI / numOfPoints);
var point = addPolarCoords(getPointObject([pointX, pointY]));
points.push(point)
}
return points;
}
Insert cell
Insert cell
Insert cell
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