Efficient way to draw rectangles?

I've been working on a sketch that visualises 1d cellular automata. I've been making lots of calls to rect(). I notice that when I'm making about 1200 calls in my inner loop, my frameRate drops to ~30 fps, then props proportionately from there. What is best way around this? Do I do it (proverbially) by hand with loadPixels()/updatePixels()? Is there a better way? I'm going to need far more than 1200 cells.


  • edited October 2015 Answer ✓

    Use a PImage, it's basically (technically not but you know what I mean) just a grid of rectangles (code taken from my reply on another thread):

    PImage grid;
    void setup() {
        size(800, 800, P2D);
        // Sets the teture filtering to NEAREST sampling to create the more grid like look
        // Create the grid with 40000 cells
        grid = new PImage(200, 200, ARGB);
        // Set a random color to each grid cell
        for(int y = 0; y < grid.height; ++y)
            for(int x = 0; x < grid.width; ++x)
                grid.set(x, y, color(random(255), random(255), random(255)));
    void draw() {
        // Render the grid
        image(grid, 0, 0, width, height);

    You could even use the PImage's pixels array for your cellular automata.

Sign In or Register to comment.