Published
Edited
Nov 20, 2019
1 fork
Importers
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
baseSize = width/2
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
randomu = d3.randomUniform.source(seeded)(0, 1)
Insert cell
randomn = d3.randomNormal.source(seeded)(0, 1)
Insert cell
makeTriangle = function(rotation, scale) {
// the original way i did it for ease of thinking
// let vertices = [ [0,0], [1,0], [Math.cos(60 * Math.PI/180), Math.sin(60 * Math.PI/180)] ]
// want the centroid to be on 0,0
let vertices = [
[-0.5, -Math.sin(60 * Math.PI/180)/2],
[0.5, -Math.sin(60 * Math.PI/180)/2],
[0, Math.sin(60 * Math.PI/180) / 2]
]
let centroid = geometric.polygonCentroid(vertices)
let rotated = geometric.polygonRotate(vertices, rotation, centroid)
let scaled = geometric.polygonScale(rotated, scale)
scaled.angle = rotation
scaled.scale = scale
return scaled
}
Insert cell
Insert cell
triangles1u = d3.range(1500).map(function(i) {
// it can be interesting to play with uniform vs normal, as well as single random variable vs independent
let z = randomu()
// let z2 = Math.abs(randomu())
return makeTriangle(360 * z * 7, 10 + z * 90)
// return makeTriangle(d3.randomUniform.source(seeded)(0, 360)(), d3.randomUniform.source(seeded)(10, 100)())
// return makeTriangle(d3.randomUniform.source(seeded)(0, 360)(), d3.randomNormal.source(seeded)(45, 15)())
})
Insert cell
triangles2u = d3.range(1500).map(function(i) {
return makeTriangle(d3.randomUniform.source(seeded)(0, 360)(), d3.randomUniform.source(seeded)(10, 100)())
// return makeTriangle(d3.randomUniform.source(seeded)(0, 360)(), d3.randomNormal.source(seeded)(45, 15)())
})
Insert cell
triangles1u1n = d3.range(1500).map(function(i) {
// return makeTriangle(d3.randomUniform.source(seeded)(0, 360)(), d3.randomUniform.source(seeded)(10, 100)())
return makeTriangle(d3.randomUniform.source(seeded)(0, 360)(), d3.randomNormal.source(seeded)(45, 15)())
})
Insert cell
triangles1n1u = d3.range(1500).map(function(i) {
// return makeTriangle(d3.randomUniform.source(seeded)(0, 360)(), d3.randomUniform.source(seeded)(10, 100)())
return makeTriangle(d3.randomNormal.source(seeded)(180, 60)(), d3.randomUniform.source(seeded)(10, 100)())
})
Insert cell
triangles2n = d3.range(1500).map(function(i) {
return makeTriangle(d3.randomNormal.source(seeded)(180, 360)(), d3.randomNormal.source(seeded)(45, 15)())
})
Insert cell
randomTypes = {
return {triangles1u, triangles2u, triangles1u1n, triangles1n1u, triangles2n}
}
Insert cell
triangles = randomTypes[t]
Insert cell
Insert cell
Insert cell
Insert cell
normalize = function(embeddings) {
let subs = embeddings.map(function(d) {
let mean = d3.mean(d)
return d.map(function(p,i) { return p - mean })
})
let mags = embeddings.map(function(d) {
return Math.sqrt(d3.sum(d, function(p) { return p*p }))
})
let max = d3.max(mags)
return subs.map(function(d) {
return d.map(function(p,i) { return p/max})
})
}
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
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