views:

74

answers:

1

Hi,

If I have a QuadCurve like this (+ = node):

+         +
 \      ./
   +--⁻⁻

And I fill it in Java 2D the result is something like this: (x = colored)

+xxxxxxxxx+
 \xxxxxx./
   +--⁻⁻

But I want to color the other side:

+         +
x\      ./x
xxx +--⁻⁻xx
xxxxxxxxxxx

This succeeds by drawing a rectangle around the curve in the color I want to color the other side and then fill the curve with the background color.

But this isn't good enough to fill a convex rounded (based on QuadCurves) polygon. In case of some coordinates for the rectangles (as explained in the trick I used) overlap other pieces of the polygon. Here are two images (the green area is my polygon):

alt text alt text

So, the question is simple: "How can I color a shape build of curves?"
But to the answer will not be simple I think...

Any advice would be VERY VERY appreciated.
Thanks in advance.

Maybe I'm going to make a bounty for this question if I don't get an answer

+1  A: 

Pick a point known to be inside the Polygon.

Know the "boundary color" (in this case, black).

recurrsiveFill(Pixel p, Color fill, Color bound) {
    p.setColor(fill);
    if(p.left.color  != bound && p.left.color != fill) 
        recurrsiveFill(p.left , fill, bound);
    if(p.right.color != bound && p.right.color != fill) 
        recurrsiveFill(p.right, fill, bound);
    if(p.up.color    != boun d&& p.up.color    != fill) 
        recurrsiveFill(p.up,    fill, bound);
    if(p.down.color  != bound && p.down.color  != fill) 
        recurrsiveFill(p.down,  fill, bound);
}

You can adapt this as necessary to suit your specific needs.

This works for any fill for a completely bounded shape. You'll also want to incorporate special conditions (edges of the picture, for example).

glowcoder
That is indeed a possible way! But I need this for in a game. So, I'm wondering if this is fast enough to render the game at 40 frames a second.
Martijn Courteaux
Be sure I will test this!
Martijn Courteaux
Well Martijn, unless I made some kind of a rookie mistake in it (which is possible, I didn't go through it extensively) it should be O(n) in terms of number of pixels you need to color, which I don't think you'll get better than. There might be more involved if you're doing a range of colors as the boundary. . . . . Another thing to bear in mind is why you're doing it this way, and if perhaps there's a better way to do things. Is there a way you could pre-render these curves and load from a saved image? Optimizations like that are best!
glowcoder