Problem with conditional structure with « if » into loop

Hi,

Sorry for my english, i’m french.

I’d like to check each pixel from image by step of 5 and write coordinates into an array of each pixel whose value is under 150.

I used a loops to check pixels and a « if » to verify its values but it doesn’t work.

Do someone know why ?

Here is my code.

let bugs = ;
let xpos = ;
let ypos = ;

let pix;
let img;
let ry = 0;
let rx = 0;
let ba;
function preload() {
img = loadImage(‘assets/pomme.jpg’);
}

function ecriture(){
for (let rx = 0 ; rx < 225 ; rx = rx+5){
if (img.get(rx, ry) < 150){
xpos.push(rx);
ypos.push(ry);
ellipse(rx, ry, 2, 2);
}
}
}

function setup() {
createCanvas(225, 225);
image(img, 0, 0);

for (let ry = 0 ; ry < 225 ; ry = ry+5){

    ecriture();
    }
      
 textSize(45);   
 text(ypos[55], 100, 130);


for (let i = 0; i < 100; i++){
    bugs.push(new Jitter(xpos[i], ypos[i]));
}

}

function draw() {

for (let i = 0; i < bugs.length; i++){
    bugs[i].display();
}

}

class Jitter {
constructor (x, y) {
this.x = 50;
this.y = 50;
}

display(){
fill (0);
ellipse(this.x, this.y, 5, 5);
}
}

thank you

1 Like

please paste your code into the

</> "preformatted text" Code tag 

-a- i think
rx ry are global vars and used in setup and a called function “ecriture()”
so the 2 FOR loops should not use a local var LET instruction

-b- you compare of the img.get(x,y) ( what is a pixel color info ) with 150 ?
try like
https://p5js.org/reference/#group-Color

brightness(img.get(rx, ry))

see
https://editor.p5js.org/kll/sketches/YMhmZzAVq

2 Likes

Thank you very much kll.
Your answer helps me a lot to understand coding.

1 Like