Fastest way to draw a rectangle with holes?

Hey everyone

I need to figure out the fastest way to draw a normal rectangle with a bunch of holes in it. The holes are shaped as circles, so whatever I draw below the rectangle will show through the holes.

It sounds pretty easy, but it’s giving me a lot of problems.


have you considered using a texture? In photoshop (or your image editing app of choice) save a 32bit PNG (i.e. with alpha) of a solid color with holes. Preferably keep the color of the texture white so you can color it in code if you want.
Then just enable blending and draw the image.

class testApp {  
   ofImage myImage;  
void testApp::setup() {  
void testApp::draw() {  
   myimage.draw(50, 50);  

Thanks for the suggestion, but I need to be able to create it dynamically. The holes will have to be created where the user has clicked, so I need to do everything programmatically unfortunately.

Hi Ronze,

What type of holes do you want to create?? … form , width, height?

I hope your answer.

Edit: oops sorry, ellipses bad reading…

I was thinking… and iif the “holes” were rectangles you can contruct yours big rectangle using a matrix [][] of booleans that determines the transparency of your rect (hole), and detecting click.

Something like this:

int nSide = 20;   
bool [nSide][nSide] holes;  
int xPos = 0;  
int yPos = 0;  
int mod = 10; //width and heigth  
ofEnableAlphaBlending();	// turn on alpha blending  
for(int yPos = 0 ; yPos < nSide ; yPos ++){  
    for(int xPos = 0 ; xPos < nSide ; xPos ++){  
        if(holes[xPos][yPos] == false ){  
           ofSetColor(255,0,0,255);	// red  
             ofSetColor(255,0,0,0);	// red  
        ofRect (xPos*mod, yPos*mod, mod, mod);  


If your intention is circular or rounded holes, maybe its more complicated ( i dont know). but i promise that i will think in that.

I hope this can help you really.

Good Luck


stencil buffer

in geometry processing this would be called a boolean operation.

basically, you need some code that can do a boolean subtraction of a circle (or an arbitrary convex shape) from an arbitrary wireframe mesh and return a new wireframe mesh with a hole where the circle was. there should be some code for this floating around somewhere, if you search google for a while. you might have to use some 3D code.

here’s what I would do to make it dynamic:

have a balck & white image with alpha channel, iterate through each pixel and calculate the distance from each hole’s center to the current pixel. if the distance is less than the radius of your hole, make it the alpha channel zero, if it is greater, make it 255. This should be able to handle all holes in one pass.