PG Graphics not working - bet you can't work this out!

I want to create pg graphics size x,y in black.
In middle I want a non black point.

Randomly searching through all pg graphics, I want to find this point in a routine iterating over and over.
Once found - return that random point as the matched x,y coordinate values that were initially chosen (mid of pg graphics)

Soooo easy with any language !!!
But cannot do it in Processing.
Why not ??

Please help!!

Do you mean something like this?

PGraphics pg;

void setup() {
  size(50, 50);
  pg = createGraphics(width, height);
}

void draw() {
  int rand_x;
  int rand_y;
  pg.beginDraw();
  pg.background(0);
  pg.stroke(255);
  pg.point(pg.width / 2, pg.height / 2);
  pg.endDraw();
  image(pg, 0, 0);
  rand_x = (int)random(pg.width);
  rand_y = (int)random(pg.height);
  if (red(pg.get(rand_x, rand_y)) > 0) {
    noLoop();
    background(127);
    text(rand_x + ", " + rand_y, 5, height - 5);
  }
}

It may take a while for it to find the point, but eventually it will.

Modify as needed; please let us know whether it does what you want.

Can’t I just find the point outside void draw??

You set up a pg screen size. Say 100 by 100
In black.
In middle you put a point at 50,50 in white.

Set up done.

Now you randomly select x,y points until you find that point which is not black. The mid point.

Then you return that point as x,y coordinates.

Want this as a sub routine outside draw.

Really is simple but don’t understand how to use it in set up or draw. ???

Yes, you can do it that way by writing a function that takes a PGraphics argument, looks for the point, then returns the coordinates.

You can also do it that way. Either way you do it, call noLoop() within draw() prior to performing the search, if you choose to include the search within an explicit while loop.

Which do you prefer, to look for the point within draw(), or within an external function?

Actually still not working.

This is what I got so far::

PGraphics pg;

float col=10;
int count=0;

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

int pixel;

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

// put a point in centre of pg graphics
int px=xsize/2;
int py=ysize/2;
pixel=(px+(py*xsize));
pg.pixels[pixel] = color(200,200,200);
pg.updatePixels();
image(pg,0,0,1500,500);

noLoop();
position();    

}

// get a point that is not 0
void position() {
px=int((random(1,xsize-1)));
py=int((random(1,ysize-1)));
pixel=(px+(py*xsize));
float rr=(red(pg.pixels[pixel]));

if(rr==0) {
position();
}
draw();
}

// main loop

void draw() {
noLoop();
println(px);
}

I’ll get there one day!!
Just can’t understand why so difficult in Processing and not any other languages.

Here it is in basic sub routine: position being the sub routine.

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

Can anyone do this in Processing because I have tried everything.
Something sooooo simple can’t be done.

It’s as easy as 2+2 !
???

Is this what you are trying to do?

PGraphics pg;
void setup() {
  size(50, 50);
  pg = createGraphics(width, height);
  pg.beginDraw();
  pg.background(0);
  pg.stroke(255);
  pg.point(pg.width / 2, pg.height / 2);
  pg.endDraw();
  image(pg, 0, 0);
  noLoop();
}

void draw() {
  int rand_x;
  int rand_y;
  while(true) {
    rand_x = (int)random(pg.width);
    rand_y = (int)random(pg.height);
    if (red(pg.get(rand_x, rand_y)) > 0) {
      background(127);
      text(rand_x + ", " + rand_y, 5, height - 5);
      break;
    }
  }
}

First, it draws a black square with a white dot in the middle.

It loops, choosing random coordinates until it finds the point.

Finally, it displays the coordinates of the white point on a gray background.

Screen capture of final result:

Screen Shot 2021-01-20 at 6.04.08 PM

Edited on January 20, 2021 to display the result on a gray background.

1 Like

You should not call draw

We could do this:

PGraphics pg;
void setup() {
  size(400, 400);
  pg = createGraphics(width, height);
  pg.beginDraw();
  pg.background(0);
  pg.stroke(255);
  pg.point(pg.width / 2, pg.height / 2);
  pg.endDraw();
  image(pg, 0, 0);

  int rand_x;
  int rand_y;
  while(true) {
    rand_x = (int)random(pg.width);
    rand_y = (int)random(pg.height);
    if (red(pg.get(rand_x, rand_y)) > 0) {
      background(127);
      text(rand_x + ", " + rand_y, 5, height - 5);
      break;
    }
  }
}

It’s fast, and shows the result even before the black background with the white dot in the middle appears.

Edit (January 21, 2021):

This will also work, with no setup() or draw() functions:

size(100, 100);
PGraphics pg = createGraphics(width, height);
pg.beginDraw();
pg.background(0);
pg.stroke(255);
pg.point(pg.width / 2, pg.height / 2);
pg.endDraw();
image(pg, 0, 0);
int rand_x;
int rand_y;
while(true) {
  rand_x = (int)random(pg.width);
  rand_y = (int)random(pg.height);
  if (red(pg.get(rand_x, rand_y)) > 0) {
    text(rand_x + ", " + rand_y, 5, height - 5);
    break;
  }
}

Screen Shot 2021-01-21 at 7.26.51 AM

@IRIGIMA, why is it that you want to use PGraphics?

here is my version

recursion is not good for this imho

PGraphics pg;

float col=10;
int count=0;

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

int pixel;

int rr; 

void setup() {
  size(1600, 600);

  background(100); 
  noSmooth();
  // colorMode(RGB, 255);
  pg=createGraphics(xsize, ysize);
  // pg.colorMode(RGB, 255);
  pg.noSmooth();
  pg.beginDraw();
  pg.background(0, 0, 0);
  pg.loadPixels();
  pg.fill(255);
  pg.strokeCap(SQUARE);

  // put a point in centre of pg graphics
  int px=xsize/2;
  int py=ysize/2;
  pixel=(px+(py*xsize));
  pg.pixels[pixel] = color(200, 200, 200);
  pg.updatePixels();

  image(pg, 0, 0);

  pg.loadPixels();

  println("start position"); 
  position();
}

// get a point that is not 0
void position() {
  while (true) {
    px=int((random(1, xsize-1)));
    py=int((random(1, ysize-1)));
    pixel=(px+(py*xsize));
    rr =int(red(pg.pixels[pixel]));

    // found? 
    if (rr!=0) {
      // Yes, found 
      println(px);
      return;
    }
  }//while
}

// --------------------------------------------------------------------------------
// main loop

void draw() {

  noFill();
  stroke(255, 0, 0); 
  ellipse(px, py, 19, 19);
  // noLoop();
}
//

2 Likes

Thank you all for your help!
Although I use Processing daily for my work - I totally forgot how it’s structured and converting real complex code can be a pain. (This was just a very small part of it)

The initial problem was not a direct problem, but similar to what I needed. So thanks for input. Appreciated.

If interested in what I’m up to - have a look on Reddit.
r/Irigima

Irigima

1 Like