views:

414

answers:

2

Flex 3, ActionScript 3, Flash player 9.

I have a picture in a BitmapData object. And an array of points. I nead to erase the part of the picture inside a polygon specified by the points. In other words, draw a polygon specified by the points and fill it with transparency.

Any ideas on how it can be done?

+1  A: 

For a rectangle, you can use fillRect. For a polygon you are gonna have to draw the polygon in a totally different color (than other colors in the bitmap) and use floodFill - but I don't know how to draw a polygon. There is no method in bitmap data class to draw lines. Another option would be to write your own logic to find pixels inside the polygon and use setPixel32 method to set their alphas to zero.

This wikipedia page describes algorithms to find if a point is inside a given polygon. You might find it useful.

Amarghosh
Are there any other options? Like masking the polygon. I guess it is possible, just don't know how
artemb
+4  A: 

Got it working with the following code:

        var shape:Shape = new Shape();
        shape.graphics.beginFill(0x000000, 1); // solid black
        shape.graphics.moveTo(points[0].x, points[0].y);

        points.forEach(function (p:Point, i:int, a:Array):void {
                shape.graphics.lineTo(p.x, p.y);
            });
        shape.graphics.endFill();
        data.draw(shape, null, null, "erase");
artemb
+1 That's a good one. Consider accepting your own answer so that this question appears answered in the listings.
Amarghosh
Ooohhh... nice. I didn't know you could do that :-p
widgisoft