Closed Naragorn closed 6 years ago
Hi!
The two main parts should be this:
… which calculates the number of columns and rows, so that they're roughly equal
and
… where for each node (i.e. point on the grid) its col
and row
are calculated.
If you need more details, please let me know!
For
… where for each node (i.e. point on the grid) its
col
androw
are calculated.
Is this the same for the rectangles? I only need the rectangles, and not the points
What and how you draw at each point is up to you. This calculation only gives you the coordinates within the grid. For example:
const nodes = [{}, {}, {}, {}];
const n = nodes.length; // 4 in this case
// Figure out how many columns and rows we need
const cols = Math.ceil(Math.sqrt(nodes.length)); // => 2
const rows = Math.ceil(n / cols); // => 2
// Assign col/row to each node
nodes.forEach((node, i) => {
node.col = i % cols;
node.row = Math.floor(i / cols);
})
// Nodes now should be
// [{col: 0, row: 0}, {col: 1, row: 0}, {col: 0, row: 1}, {col: 1, row: 1}]
Perfect I just finished my task thanks to you!
Many thanks and kind regards!
I exactly need your algorithm that places rectangles in a grid fashion for my application. I suspect it is in _function distributeEqually(nodes)
However I don't understand your code yet and have no experience with d3.
Can you give me any logic or semantic on how the algorithm works that places rectangles like that?