Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created September 14, 2024 20:52
Show Gist options
  • Save Hermann-SW/6dcc5c1358914901e38bacb4b336f798 to your computer and use it in GitHub Desktop.
Save Hermann-SW/6dcc5c1358914901e38bacb4b336f798 to your computer and use it in GitHub Desktop.
JSCAD dodecahedron with exact coordinates from Wikipedia and adjacengy lists
const jscad = require('@jscad/modeling')
const { colorize } = jscad.colors
const { sphere, cylinder } = jscad.primitives
const { rotate, translate } = jscad.transforms
const { vec3 } = jscad.maths
er=0.02
function vertex(p) { return sphere({ center: p, radius: 3*er}) }
function edge(v, w) {
d = [0, 0, 0]
x = [0, 0, 0]
vec3.subtract(d, w, v)
vec3.add(x, v, w)
vec3.scale(x, x, 0.5)
return colorize([0, 0, 1, 1],
translate(x,
rotate([0, Math.acos(d[2]/vec3.length(d)), Math.atan2(d[1], d[0])],
cylinder({radius: er, height: vec3.length(d)})
)
)
)
}
function main() {
dod=[]
out=[]
// https://en.wikipedia.org/wiki/Dodecahedron#Cartesian_coordinates
phi=(Math.sqrt(5)+1)/2
h=1/phi
for(x=-1;x<=1;x+=2)
for(y=-1;y<=1;y+=2)
for(z=-1;z<=1;z+=2)
dod.push([x,y,z])
for(x=-1;x<=1;x+=2)
for(y=-1;y<=1;y+=2){
dod.push([0,x*(1+h),y*(1-h*h)])
dod.push([x*(1+h),y*(1-h*h),0])
dod.push([y*(1-h*h),0,x*(1+h)])
}
adj=[[8,9,10],[11,9,16],[10,12,14],[12,16,17],[8,13,15],
[11,15,19],[13,14,18],[17,18,19],[0,4,11],[0,1,12],
[0,2,13],[1,5,8],[2,9,3],[4,6,10],[6,2,17],
[4,5,18],[1,3,19],[14,7,3],[6,7,15],[7,5,16]]
dod.forEach((p) => out.push(vertex(p)))
adj.forEach((v,i) =>
v.forEach((j) =>
{if (i<j)
out.push(edge(dod[i], dod[j]))
}
)
)
return [
out
]
}
module.exports = { main }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment