Published
Edited
Apr 1, 2021
1 star
Insert cell
md`# Rad Lines`
Insert cell
degreesToRadians = degrees => {
return degrees * (Math.PI / 180);
}
Insert cell
createPolygon = (x, y, numOfSides, radius, minAngle) => {
let arr = [];
let xo;
let yo;
for (let i = 0; i < numOfSides; i++) {
let t =
(degreesToRadians(360) / numOfSides) * i + minAngle * (Math.PI / 180);
xo = x + radius * Math.sin(t);
yo = y + radius * Math.cos(t);
arr.push([xo, yo]);
}
return arr;
}
Insert cell
quantity = 20
Insert cell
xPositionFormula = '300'
Insert cell
yPositionFormula = '300'
Insert cell
scaleFormula = 'i+i*7'
Insert cell
rotationFormula = '10 * sin(i * pi / 9)'
Insert cell
minAngle = 0
Insert cell
sides = 6
Insert cell
minRadius = 50
Insert cell
roundness = 1
Insert cell
svg = {
const svg = html`<svg id = "svg" width="600" height="600"></svg>`;

const container = d3.select(svg);

const curve = d3.line().curve(d3.curveCardinalClosed.tension(roundness));
container
.selectAll('path')
.data(data)
.join('path')
.attr("stroke", "black")
.attr("fill", "none")
.attr("d", d => {
return curve(d);
});

return svg;
}
Insert cell
data = generatePolygonData(
seed,
quantity,
xPositionFormula,
yPositionFormula,
sides,
minRadius,
scaleFormula,
minAngle,
rotationFormula
)
Insert cell
generatePolygonData = (
seed,
quantity,
xPositionFormula,
yPositionFormula,
sides,
minRadius,
scaleFormula,
minAngle,
rotationFormula
) => {
let polygons = [];
math.config({ randomSeed: seed });
let originalPoints = [];
for (let i = 0; i < quantity; i++) {
try {
polygons.push(
createPolygon(
math.evaluate(xPositionFormula, { i: i }),
math.evaluate(yPositionFormula, { i: i }),
sides,
minRadius + math.evaluate(scaleFormula, { i: i }),
minAngle + math.evaluate(rotationFormula, { i: i})
)
);
} catch (e) {
console.log(e);
}
}
return polygons;
}
Insert cell
math = require('mathjs')
Insert cell
seed = 0.4212687683098432008
Insert cell
d3 = require('d3')
Insert cell
random = require("d3-random@>=2.2")
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