Backwards drift in addressing pixels[] array

Hi everyone,

Haven't done any programming in a while, and i'm trying to create an expanding cloud based on any pixel's proximity to a pixel which is black in a certain radius. The effect is sort of what I'm after, but it drifts backwards and doesn't seem able to test forward from black pixels? It should disperse uniformly in all directions. See image attached. What am I doing wrong?

Screen Shot 2013-12-16 at 00.57.25

Screen Shot 2013-12-16 at 00.57.19

int pixelCount;
color testTone;

void setup() {
    // size(1280,720);
    size(800,800);
    background(255);
    rectMode(CENTER);
    fill(255);
    stroke(0);
    strokeWeight(5);
    rect(width/2, height/2, 100, 100);
    pixelCount = width*height;
    testTone = color(0);

}

void draw() {
    loadPixels();
    buildPixels();
    updatePixels();
    rect(width/2, height/2, 100, 100);
    // exit();
}

void buildPixels() {
    color[] builtPixels = new color[pixelCount];

    for(int x=0;x<width;x++) {

        for(int y=0;y<height;y++) {

            int loc = x+y*width;

            if(testPixel(x, y, loc, int(random(5)+1))==true) {
                builtPixels[loc] = testTone;
            } else {
                builtPixels[loc] = color(255);
            }

        }

    }
    for(int i=0;i<pixelCount;i++) {
        pixels[i] = builtPixels[i];
    }
}

boolean testPixel(int x, int y,int loc, int range) {
    int testToneDetectionCount = 0;
    boolean testToneDetected = false;

    //is there a neighbouring filled pixel?
    int offset = range/2;
    for(int i=0;i<range;i++) {
        for(int j=0;j<range;j++) {
            int xTest = x + i*offset;
            int yTest = y + j*offset;
            int testLoc = xTest + yTest*width;
            testLoc = constrain(testLoc,0,pixelCount-1);

            if(pixels[testLoc]==testTone) {
                testToneDetectionCount++;
            }
        }
    }
    return boolean(testToneDetectionCount);
}
Tagged:

Answers

  • Sorry everybody, I'm just stupid and it was a simple thing to fix. Is it possible to delete a post?

  • Rule of thumb: Never delete a post on a forum. Others might need the information. You never know...

    So instead, I know I didn't answer your question in any way, but just mark this post as the answer.

  • Also, to make a post effective, submit the solution as well.

    And to answer your question directly, only the moderators can delete posts, but it's usually because of spam. So no.

  • edited December 2013

    Sorry, couldn't resist to try this out. :)

    int builtPixels[];
    int stepValue = 1; // Try higher values for more interesting effects
    
    void setup() {
        size(800, 800);
        background(#ffffff);
        rectMode(CENTER);
        fill(#ffffff);
        stroke(0);
        strokeWeight(5);
        builtPixels = new int[width * height];
    }
     
    void draw() {
        loadPixels();
        buildPixels();
        updatePixels();
        rect(width / 2, height / 2, 100, 100);
    }
     
    void buildPixels() {
        for(int x = 0; x < width; x++)
            for(int y = 0; y < height; y++)
                builtPixels[x + y * width] = testPixel(x, y, (int)random(3)) ? #000000 : #ffffff;
        for(int i = 0; i < pixels.length; i++)
            pixels[i] = builtPixels[i];
    }
    
    boolean testPixel(int posX, int posY, int radius) {
        boolean testToneDetected = false;
        radius = radius * stepValue;
        for(int x = -radius; x <= radius; x += stepValue)
            for(int y = -radius; y <= radius; y += stepValue)
                if(pixels[constrain(posX + x * abs(x) + (posY + y * abs(y)) * width, 0, pixels.length - 1)] == #000000)
                    testToneDetected = true;
        return testToneDetected;
    }
    
Sign In or Register to comment.