Get returning incorrect pixel color

#1

get returning incorrect pixel color.

See the image,

There is a big green rectangle behind those circles.
when I use get(x,y) in one of those circles, in the picture you can see the black dot, it is the location where I used get(x,y) but it returns me green color. But as one can see it should either return white or black color and not green.

I checked figraham answer on this post:

But I get the same problem as the op on that post.

1 Like

#2

may we see the code, please in

</> code tag

or link to https://editor.p5js.org/

0 Likes

#3

Working Code
https://pastebin.com/DZaFZDcA

0 Likes

#4

This pastbin has been removed.

You can simply post your code to the forum.

My first guess is that you are not using loadPixels / updatePixels, so your canvas values haven’t been updated before you check them.

https://p5js.org/reference/#/p5/loadPixels
https://p5js.org/reference/#/p5/updatePixels

0 Likes

#5

Here is the code that I wrote, and

k = 400;
r = 50
p1 = [100, 100]
v1 = [0, 0]
w1 = [400, 0]
a = null

function setup() {
    createCanvas(k, k);
    background(20, 111, 32);
    myrecurse()
}

function myrecurse() {
    for (var gg = 0; gg < 10; gg++) {
        fill(105);
        rect(0,0,k,k);
        fill(255);
        drawcircle()

        flag = sweepline()
        if (flag) {

            return "invalid"
        }
    }
    return "valid"
}


function drawcircle() {
    for (var m = 0; m < 500; m++)
        circle(random(-4, k), random(-4, k), r);

}

function sweepline() {
//  loadPixels(); // tried this too
    for (var j = 20; j < k; j++) {
        for (var i = 0; i < k; i++) {

            if (get(i, j)[0] == 105) { // I feel this if statement should never be true. But strangely it is, why?

                print(get(i,j))
                fill(51);
                circle(i, j, 5)

                return true
            }
        }
    }
//updatePixels();
    return false

}

0 Likes

#6

from what i see, the result is correct in many ways,
-a- if your for loop ( draw and check )
the check will be on something you not see
so better first draw and then check.
and you draw the rect with that color you check later on,
and it finds it.
-b- if you start to program it makes no sense to make
500 objects in for loops, why not start with 5, but use a variable name like

let many = 5;

-c- the whole for gg loop inside draw has what idea?
if you want do a animation? you should not do it inside ONE draw ( frame ),
disable that first
-d- but now comes the tricky part
the renderer makes from the rectangles
( filled 105 ) same as [105,105,105,255] you test on RED,
a borderline and that will create pixels with any shade of red (grey)
so i only got a clean result if i used in setup
noStroke();

i play
https://editor.p5js.org/kll/sketches/FkcRw_-DK
but currently can not test ( stucks at "read cdn.rawgit.com )
i / my account / break p5js.com again?

0 Likes

#7

It is a part of bigger application, each and every steps are required, hence it is there,
gg should continue, until the whole square is covered with circles. In actuality it should be ‘continue’ instead of return but for debugging purpose I have modified it to return.

I can’t start with 5, because the whole goal is to cover the entire square with random circles, If I choose 5 circles then it is highly likely that the square will not be covered.

Okay, noStroke() did the trick, but 400x 400 pixel is taking around 2-3 minutes.
Is there any faster method to check if a color is present in the canvas or not?

Edit:
loadPixels() and pixel array is much faster.
Thank You All.

0 Likes

#8

sorry, again, that is not what your code does!!
inside the FOR gg loop you overwrite last gg loop with the rectangle
( what’s nearly same like background() )
and 500 cicles again!

and then check the color red(pix)=105 of all pix under line 20,
alarm make a circle and exit check on true
and go draw next gg loop!
so you will not see that situation ( and the pointer circle ) what the alarm has caused! unless it happens in the last gg loop.

i recommend a print on alarm, what states
gg, i,j, r,g,b

0 Likes