logoalt Hacker News

jgrahamctoday at 6:39 AM0 repliesview on HN

The code includes an ELEMENTS array with the layout of elements on the standard periodic table with H at the (1, 1) position. The tiles are laid out on the page and the code attempts to place the elements as close to the "standard" position (rectilinear) by calculating the Euclidean distance from the centroid of rhombi to the centre of the square that would be drawn for the element in the standard table.

So you've got 118 elements with ideal locations and a whole bunch of rhombi with known locations. The Euclidean distance (without bothering with the sqrt) is used as the "cost" function for the Hungarian algorithm (https://en.wikipedia.org/wiki/Hungarian_algorithm): the idea is to minimize the cost (distance from ideal) for each element.

You will sometimes see some gaps in the periodic table as this layout isn't "perfect" (in the sense of without isolated rhombi inside the table) and I did work on having a post-layout pass where these islands were identified and filled by moving adjacent rhombus assignments, but I wasn't happy with how this tended to break the layout of the periodic table (I was going for some visual "looks similar to the classic U shaped periodic table) and so decided to accept them.