# Mouse draw sketch

**URL:** <https://discourse.processing.org/t/mouse-draw-sketch/24449>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 8, 2020, 8:25pm UTC](https://discourse.processing.org/t/mouse-draw-sketch/24449 "2020-10-08T20:25:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe\_processing](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@joe\_processing](https://discourse.processing.org/u/joe_processing)\
**Post date:** [October 8, 2020, 8:25pm UTC](https://discourse.processing.org/t/mouse-draw-sketch/24449/1 "2020-10-08T20:25:13Z")

</div>

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.

```auto
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;
}

```

```auto
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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 8, 2020, 8:46pm UTC](https://discourse.processing.org/t/mouse-draw-sketch/24449/2 "2020-10-08T20:46:35Z")

</div>

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

```auto

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--;
    }
  }
}

//

```

---

<div class="post-metadata">

**Author:** ![joe\_processing](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@joe\_processing](https://discourse.processing.org/u/joe_processing)\
**Post date:** [October 9, 2020, 4:32am UTC](https://discourse.processing.org/t/mouse-draw-sketch/24449/3 "2020-10-09T04:32:50Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6ecbdddfbaec173740e073ae1095b3e6fd0ee6d9.png)

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 9, 2020, 5:10am UTC](https://discourse.processing.org/t/mouse-draw-sketch/24449/4 "2020-10-09T05:10:58Z")

</div>

I know

You have to find a way to achieve this.

Try to bring the points of the line into the objects

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 11, 2020, 7:01am UTC](https://discourse.processing.org/t/mouse-draw-sketch/24449/5 "2020-10-11T07:01:28Z")

</div>

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

I am sorry!
