I need to define an object (or a region) that is kind of "blob" shaped on a discrete gridmap. It should look something like this:
where the red region denotes the center point (these are just ideas, any blobby shape will work as long as it can be varied randomly). My idea so far was to iteratively increment the angle from starting point (=0 degrees) to 360 degrees and use trigonometry to calculate the outer points of a circle (which will result in the unit circle if the radius = 1 = const). I then used Bresenham's line algorithm (remember: we are moving on a discrete grid) to calculate the line that connects the center of the circle and the outer point I just came up with. My idea was that if I could vary the radius somewhat, I could create these blobby shapes. What I've come up with so far gives me nice shapes, they're just not really "blobby" though. Here's my code (note that x0
and y0
mark the center point of my gridmap, plotBresenham
just puts all 1s
in the regions so the gridmap can be visualized):
double radius = 10;
for(int alpha=0; alpha<360; alpha++) {
double x = cos(alpha*M_PI/180.0)*radius;
double y = sin(alpha*M_PI/180.0)*radius;
if(alpha<45) radius+=0.5;
else if(alpha<90) radius-=0.5;
else if(alpha<135) radius+=0.5;
else if(alpha<180) radius-=0.5;
else if(alpha<225) radius+=0.5;
else if(alpha<270) radius-=0.5;
else if(alpha<315) radius+=0.5;
else radius-=0.5;
plotBresenhamLine(x0,y0,x,y)
}
The result looks like this:
Sorry for the crude drawing. Programming language is C++, but I think the approach doesn't really depend on the language used. Any tips / help / guidance on how I can create shapes that resemble the ones I need more? Or even a Framework that does stuff like this for you? For me, it's just important to have the coordinates of the points within, to put them into my gridmap.
Varying the radius with the angle is the way to go. But instead of the random walk you can use a sum of several periodic functions with predetermined amplitudes and phases. This guarantees that
Pick a sine or cosine function where you multiply the angle by an integer and add a random phase. Scale each by a random (pre-determined) amplitude. Add a constant larger than the sum of all the amplitudes. Profit.
I'm not going to write this in C++ because, as you said, it won't add anything important to the algorithm. It may go like this:
amps[N]
and phases[N]
.amps[i]
and between 0 and 2π for each phases[i]
.alpha
(in radians), calculateradius = 1 + sum[i=0 to N-1] amps[i] * cos((i+1)*alpha + phases[i])
x = cos(alpha)*radius;
y = sin(alpha)*radius;
Results (from Wolfram Mathematica):
To make it somewhat more interesting, limit the k-th amplitude by some negative power of k (or of k+1, since we're indexing from zero). Here's when instead of 2N the random number is divided by pow(i+1,1.5)
in step 3, for N = 30:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With