Essentially what I'm trying to do is, given a black surface and an arbitrary list of starting points (and colors), create randomized regions of the surface in the given colors which contain the seed points. So if I gave (100,100,c_red) and (200, 150, c_blue), then it would create a surface with only two regions, both randomized: one red region containing at least (100, 100) and one blue region containing at least (200, 150).
My First Attempt
Originally, I just tried a Breadth-First Search methodology, where the seed positions were initially added to a queue, then all black pixels around it were marked with the current pixel's color and added to the queue, repeating until the queue was empty (and thus no pixels are black). This worked to create the regions, but there was no randomness. It just grew diamonds, basically, in order from one region to the next to the next and back to the first. That's to be expected, of course. I thought it would be easy to then modify this and add in some randomness...but it turns out, it's not. I couldn't figure out how to add randomness while still guaranteeing that every black pixel is visited at least once and that won't result in an infinite loop (i.e. restricting that to "every pixel is visited exactly once").
If you can help from here, then please do; Lord knows it'll be easier for me to implement this!
A Different Approach
My friend is a computer science/mathematics dual major. I figured if anyone could help, he could. When I asked about the above problem, he told me he thinks it's impossible to add randomness to the BFS without allowing the possibility of unfilled holes. So instead, he suggested a different method.
Instead of starting at the seeds and filling each pixel, he said, it would be better to simply divide the surface into N regions (where there are N seeds), each containing exactly one seed. This way, the focus is on generating the edges of the region and not filling from the inside.
He pointed me on a path that eventually led me to a mathematical problem (at which point he left, since it was 1:30AM). I haven't been able to figure it out since.
Basically, I have a set of arbitrary but constant seed points (set P). I also have a set of randomly chosen vertices (set V).
Note for below: I use |A| as the symbol for "the size of set A".
I first must guarantee that |V| >= 2 + |P|/2 (this is easy, since the vertices are procedurally generated and |P| is known).
I then need to connect pairs of the vertices in V with edges (set E) in such a way that the following three conditions hold true:
1) No two edges intersect,
2) |E| = |P| + |V| - 2, and
3) Each region created by the edges contains exactly one point from set P.
The restriction on |V|, as well as the first two numbered conditions above, should guarantee that this results in exactly |P| contiguous regions.
How can I guarantee those three properties for any arbitrary (compatible) V and P while choosing edges as randomly as possible?
I first thought of brute-forcing condition 1 by choosing a uniformly random edge and checking if it intersects a previously-chosen edge, trying another random edge if it does. That seems terribly inefficient. I then thought of testing all edges for intersections with previous edges, discarding the ones that intersect, and randomly choosing from the remaining set. This seems more efficient (though still not great), but it could be the case that a previous edge worked but also prevented another edge from being chosen later, which could break condition 2.
And don't even get me started on condition 3. I don't even know where to begin that.
So can anyone point me in the right direction?
-IMP
*EDIT* Oh, and also, the way points are stored doesn't matter. As of now, I'm keeping them in a queue or list in pairs so that, say, if a queue holds points (10, 20) and (35, 40), the queue contents would be {10, 20, 35, 40}. But I can change that to nested lists or such if it helps. The representation is immaterial to the problem, so don't worry about changing that to make things easier.
Edited by IceMetalPunk, 16 February 2012 - 09:41 PM.












