# Help needed please in code

**URL:** https://discourse.processing.org/t/help-needed-please-in-code/27241
**Category:** Coding Questions
**Created:** [January 20, 2021, 5:10pm UTC](https://discourse.processing.org/t/help-needed-please-in-code/27241 "2021-01-20T17:10:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)
#### Post date: [January 20, 2021, 5:10pm UTC](https://discourse.processing.org/t/help-needed-please-in-code/27241/1 "2021-01-20T17:10:36Z")

</div>

Within setup and main. I am creating a pg graphics at x,y resolution, placing a value in centre. Then randomly choosing a point. If the point = centre then x,y found.

Wth am I doing wrong??

PGraphics pg;

float col=10;

int count=0;

int xsize=1500;  
int ysize=500;  
int tempx;  
int tempy;  
int temppixel;  
float pixheight;

int pixel;

void setup(){  
size(1500, 500);  
noSmooth();  
colorMode(RGB,255);  
pg=createGraphics(xsize, ysize);  
pg.noSmooth();  
pg.beginDraw();  
pg.background(0,0,0);  
pg.loadPixels();  
pg.fill(255);  
pg.strokeCap(SQUARE);  
frameRate(1);

```
int px=xsize/2;
int py=ysize/2;
pixel=(px+(py*xsize));
pg.pixels[pixel] = color(200,200,200);

positionchange();

```

}

void draw(){  
pg.colorMode(RGB,255);  
pg.beginDraw();  
pg.updatePixels();  
image(pg,0,0,1500,500);  
}

void positionchange(){  
tempx=int(random(1,xsize));  
tempy=int(random(1,ysize));  
temppixel=(tempx+(tempy\*xsize));  
pixheight=red(pg.pixels[temppixel]);

println(temppixel);

if (pixheight==0); {  
positionchange();  
}

}

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 20, 2021, 5:17pm UTC](https://discourse.processing.org/t/help-needed-please-in-code/27241/2 "2021-01-20T17:17:17Z")

</div>

> [@IRIGIMA](#):
>
> if (pixheight==0); {  
> positionchange();  
> }

you loop it too much. Find another way to do it.

---

<div class="post-metadata">

### Author: ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)
#### Post date: [January 20, 2021, 5:22pm UTC](https://discourse.processing.org/t/help-needed-please-in-code/27241/3 "2021-01-20T17:22:56Z")

</div>

How though?? It’s sooooo simple but can’t think how ???

---

<div class="post-metadata">

### Author: ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)
#### Post date: [January 20, 2021, 5:25pm UTC](https://discourse.processing.org/t/help-needed-please-in-code/27241/4 "2021-01-20T17:25:04Z")

</div>

But it has to be looped until the centre point is found

---

<div class="post-metadata">

### Author: ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)
#### Post date: [January 20, 2021, 5:32pm UTC](https://discourse.processing.org/t/help-needed-please-in-code/27241/5 "2021-01-20T17:32:30Z")

</div>

Code in other language:

position:

randomize

x=(int(rnd(xmax)))

y=(int(rnd(ymax)))

get pixel x,y color r2,g2,b2

if r2=0 then goto position

return

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 20, 2021, 5:34pm UTC](https://discourse.processing.org/t/help-needed-please-in-code/27241/6 "2021-01-20T17:34:15Z")

</div>

you need to add a way to find if the pixel has been checked yet.  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ad46a988432c9739ebbc43a513d4d9a422f2b8f2.png)  
What I am trying to signal in this image is that you go over the previously checked squares. In results in an infinite loop

---

<div class="post-metadata">

### Author: ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)
#### Post date: [January 20, 2021, 5:54pm UTC](https://discourse.processing.org/t/help-needed-please-in-code/27241/7 "2021-01-20T17:54:56Z")

</div>

Why is something so difficult so hard within Processing!! ??

Ok.  
Using PG graphics.  
Everything is black apart from middle point.

Go to a routine that randomly finds that point and re-iterates till it’s done.

Once done, display the point found (middle) on screen.
