Skip to content

Commit

Permalink
tighter code
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jun 10, 2024
1 parent ec7320d commit 4dcb2db
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 72 deletions.
80 changes: 32 additions & 48 deletions src/deltoidal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ import voronoi from "./polyhedral/voronoi.js";
import { geoCentroid, geoInterpolate } from "d3-geo";

export default function () {
var theta = atan(0.5) * degrees;
const theta = atan(0.5) * degrees;

// construction inspired by
// https://en.wikipedia.org/wiki/Regular_icosahedron#Spherical_coordinates
var vertices = [
[0, 90],
[0, -90],
].concat(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(function (i) {
var phi = ((i * 36 + 180) % 360) - 180;
return [phi, i & 1 ? theta : -theta];
})
const vertices = [[0, 90], [0, -90]].concat(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((i) => [
((i * 36 + 180) % 360) - 180,
i & 1 ? theta : -theta
])
);

// icosahedron
var polyhedron = [
const polyhedron = [
[0, 3, 11],
[0, 5, 3],
[0, 7, 5],
Expand All @@ -47,51 +44,38 @@ export default function () {
[1, 6, 8],
[1, 8, 10],
[1, 10, 2], // South
].map(function (face) {
var t = face.map(function (i) {
return vertices[i];
});
].map((face) => {
const t = face.map((i) => vertices[i]);
// create 3 polygons from these using centroid and midpoints
var f1 = [
t[0],
geoInterpolate(t[0], t[1])(0.5),
geoCentroid({ type: "MultiPoint", coordinates: t }),
geoInterpolate(t[0], t[2])(0.5),
const a0 = geoInterpolate(t[1], t[2])(0.5);
const a1 = geoInterpolate(t[0], t[2])(0.5);
const a2 = geoInterpolate(t[0], t[1])(0.5);
const c = geoCentroid({ type: "MultiPoint", coordinates: t });
return [
[t[0], a2, c, a1],
[t[1], a0, c, a2],
[t[2], a1, c, a0]
];
var f2 = [
t[1],
geoInterpolate(t[1], t[2])(0.5),
geoCentroid({ type: "MultiPoint", coordinates: t }),
geoInterpolate(t[1], t[0])(0.5),
];
var f3 = [
t[2],
geoInterpolate(t[2], t[0])(0.5),
geoCentroid({ type: "MultiPoint", coordinates: t }),
geoInterpolate(t[2], t[1])(0.5),
];
return [f1, f2, f3];
});

var polygons = {
const polygons = {
type: "FeatureCollection",
features: polyhedron.flat().map(function (face) {
return {
properties: {
sitecoordinates: geoCentroid({
type: "MultiPoint",
coordinates: face,
}),
},
geometry: {
type: "Polygon",
coordinates: [[...face, face[0]]],
},
};
}),
features: polyhedron.flat().map((face) => ({
type: "Feature",
properties: {
sitecoordinates: geoCentroid({
type: "MultiPoint",
coordinates: face,
}),
},
geometry: {
type: "Polygon",
coordinates: [[...face, face[0]]],
},
}))
};

var parents = [
const parents = [
-1, // 0
2, // 1
0, // 2
Expand Down
43 changes: 19 additions & 24 deletions src/rhombic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import voronoi from "./polyhedral/voronoi.js";
import { geoCentroid } from "d3-geo";

export default function () {
var phi1 = atan(Math.SQRT1_2) * degrees;
var vertices = [
const phi1 = atan(Math.SQRT1_2) * degrees;
const vertices = [
[0, 90], // 0
[0, phi1], // 1
[90, phi1], // 2
Expand All @@ -29,7 +29,7 @@ export default function () {
];

// rhombic dodecahedron
var polyhedron = [
const polyhedron = [
[0, 1, 8, 4],
[0, 2, 5, 1],
[0, 3, 6, 2],
Expand All @@ -44,31 +44,26 @@ export default function () {
[5, 10, 13, 9],
[6, 11, 13, 10],
[7, 12, 13, 11],
].map(function (face) {
return face.map(function (i) {
return vertices[i];
});
});
].map((face) => face.map((i) => vertices[i]));

var polygons = {
const polygons = {
type: "FeatureCollection",
features: polyhedron.map(function (face) {
return {
properties: {
sitecoordinates: geoCentroid({
type: "MultiPoint",
coordinates: face,
}),
},
geometry: {
type: "Polygon",
coordinates: [[...face, face[0]]],
},
};
}),
features: polyhedron.map((face) => ({
type: "Feature",
properties: {
sitecoordinates: geoCentroid({
type: "MultiPoint",
coordinates: face,
}),
},
geometry: {
type: "Polygon",
coordinates: [[...face, face[0]]],
},
}))
};

var parents = [
const parents = [
-1, // 0
0, // 1
6, // 2
Expand Down

0 comments on commit 4dcb2db

Please sign in to comment.