Mouse draw sketch

hi,

|'m trying to write a code that demonstrates a spread of infected pixels that appear from and after the user draws a line in processing (I added a picture that demonstrates the stages that code makes).

But my code is stuck. I’ve been able to start it but the picture is frozen.

Nekuda [] nekudot = new Nekuda [5000];

int total = 0;

void core() {
  stroke(255);
  if (mousePressed == true) {
    line(mouseX, mouseY, pmouseX, pmouseY);
  }
  setup();
  draw();
}

void setup () {
  size (600, 600);
  background (0);
  for (int i = 0; i< nekudot.length; i++) {
    nekudot [i] = new Nekuda ();
  }
}

void draw () {
  background (0);
  for (int i = 0; i< nekudot.length; i++) {
    if (isEmpty(nekudot[i].getX(), nekudot[i].getY()) && hasNeighbor(nekudot[i].getX(), nekudot[i].getY())) {
      nekudot[i].reset ();
    }
    nekudot[i].step ();
    nekudot[i].display ();
  }
}


boolean isEmpty(int x, int y) {
  color c = get(x, y);
  if (c == 0) {
    return true;
  } else {
    return false;
  }
}

boolean hasNeighbor(int x, int y) {
  for (int i=x-1; i<x+2; x++) {
    for (int j=y-1; j<y+2; y++) {
      if (isEmpty(i, j)) {
        return true;
      }
    }
  }
  return false;
}
class Nekuda {

   int x;
   int y;

  public Nekuda () {
    x = (int)random (0, 600);
    y = (int)random (0, 600);
  }

  private void reset () {
    x = (int)random (0, 600);
    y = (int)random (0, 600);
  }
 
  public int getX(){
    return x;
  }
 
  public int getY(){
    return y;
  }

  void display () {
    point (x, y);
    stroke (255, 0, 0);
  }

  void step () {
    int choice = int(random(4));

    if (choice == 0) {
      x++;
    } else if (choice == 1) {
      x--;
    } else if (choice == 2) {
      y++;
    } else {
      y--;
    }
  }

}

any ideas?

thanks

what is core() supposed to do??

I would distinguish between first phase: you draw with mouse, 2nd phase: virus grow

See **mouseIn ** to achieve this


Nekuda [] nekudot = new Nekuda [5000];

int total = 0;

boolean mouseIn = true; 

void setup () {
  size (600, 600);
  background (0);
  for (int i = 0; i< nekudot.length; i++) {
    nekudot [i] = new Nekuda ();
  }
}

void draw () {

  // background (0);

  if (mouseIn) {
    stroke(255);
    if (mousePressed) {
      line(mouseX, mouseY, 
        pmouseX, pmouseY);
    }
  } else {

    for (int i = 0; i< nekudot.length; i++) {
      if (isEmpty(nekudot[i].getX(), nekudot[i].getY()) && hasNeighbor(nekudot[i].getX(), nekudot[i].getY())) {
        nekudot[i].reset ();
      }
      nekudot[i].step ();
      nekudot[i].display ();
    }
  }
}

void mouseReleased () {
  mouseIn = false;
}


boolean isEmpty(int x, int y) {
  color c = get(x, y);
  if (c == 0) {
    return true;
  } else {
    return false;
  }
}

boolean hasNeighbor(int x, int y) {
  for (int i=x-1; i<x+2; x++) {
    for (int j=y-1; j<y+2; y++) {
      if (isEmpty(i, j)) {
        return true;
      }
    }
  }
  return false;
}
//====================================================================


class Nekuda {

  int x;
  int y;

  public Nekuda () {
    x = (int)random (0, 600);
    y = (int)random (0, 600);
  }

  private void reset () {
    x = (int)random (0, 600);
    y = (int)random (0, 600);
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }

  void display () {
    stroke (255, 0, 0);
    point (x, y);
  }

  void step () {
    int choice = int(random(4));

    if (choice == 0) {
      x++;
    } else if (choice == 1) {
      x--;
    } else if (choice == 2) {
      y++;
    } else {
      y--;
    }
  }
}

//
1 Like

Hi. Thanks for your time @Chrisir.

so now I can draw the line but the dots paper right after that and then the screen freeze.

צילום מסך 2020-10-09 ב-7.32.13

I need the red pixels to start spreading from the line that I wrote when each pixel infects another one as in the example I attached in the first message.

thanks.

I know

You have to find a way to achieve this.

Try to bring the points of the line into the objects

It is a policy of this forum not to provide full code solutions

I am sorry!