This one has be me stumped. This is more of a Math/logic issue then a specific JS issue, but it's JS that I am working in and and some point I will need to convert the resulting logic to code....
I am trying to create X number of Non-overlapping rectangles and/or squares of random sizes on the canvas to achieve something like this example image:
(the number of boxes I would want to create could range anywhere from 10 to 100, give or take. Obviously the more boxes needed, the smaller they would all have to be)
I have a JS Fiddle where I have been trying different ideas, but the logic of this issue just keeps getting away from me...
What I DONT want is the classic Fibonacci spiral box pattern. I was hoping randomness would take care of that to some degree, but I also had the idea I could search the array of stored lines each time to find the longest line and start from a random point on that line.
I am on the current path of attempting to cut the canvas in half at a random point then add that line to an array. Then draw another lines based on the coordinates of the first line and then store that line also, and so on and so on... I'm storing the coordinates in an array of objects like this:
function storeLine(startX, startY, endX, endY, array) {
array.push({
start : {
x: startX,
y: startY
},
end : {
x: endX,
y: endY
}
});
}
But I soon hit issues with the fact that the very first line I draw across the whole x axis will ALWAYS be the longest line and I just ended up with lots of thin boxes.
In a perfect world my end result would take in variables like, Total number of boxes and Min x/y ratio so I could (somehow>) have an option to lean towards more portrait or more landscape boxes, so that I could adjust and keep regenerating till I had a result that I liked.
Anyway, I'm stuck about how to proceed, or even if I'm on the right path. If anyone has an idea about to continue down my current path or a better way to go about this, I would be forever in your debt!
Note: after checking my question, I had the thought that the Fibonacci box pattern would be OK as a starting point, but I would somehow still need to make the larger initial boxes also be divided up so that I dont just keep getting smaller and smaller boxes when I want a larger total number of boxes... anyway, just a random thought, if it gives someone else a spark of an idea.
Additional thought: A Voronoi pattern would be amazing too, but my math skills are just not up to even knowing where to start