# Moving and cutting

**URL:** <https://discourse.processing.org/t/moving-and-cutting/11777>\
**Category:** Coding Questions\
**Created:** [June 2, 2019, 5:57pm UTC](https://discourse.processing.org/t/moving-and-cutting/11777 "2019-06-02T17:57:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)\
**Post date:** [June 2, 2019, 5:57pm UTC](https://discourse.processing.org/t/moving-and-cutting/11777/1 "2019-06-02T17:57:03Z")

</div>

Hey Guys,  
I’m trying to do some demo project. What I’d like to do is draw some lines and a rectangle. Those part of the lines will be invisible which are out of the rectangle. I give the end-coordinates of the lines by clicking. I’m using noLoop() because I want to draw the line just before the second end-coordinate is given.  
 ![1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cefc3e011b6020090b7573bf13053eea36c7d080.png)

```auto
  void setup() {
    size(800, 800);
    noLoop();
  }

```

I want to move the rectangle by pressing a key. Because of the noLoop() function the KeyPressed() function is not working. If I delete the noLoop() the processing keeps drawing lines as I’m moving the mouse.

```auto
void mouseClicked() {
  redraw();
} 
void keyPressed(){
if(key==CODED){
  if(keyCode==UP){
    y=y-100;
  }
  if(keyCode==DOWN){
    y=y+100;
  }
  if(keyCode==LEFT){
    x=x-70;
  }
  if(keyCode==RIGHT){
    x=x+70;
  }
  }
}

```

Is there any solution on this?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 2, 2019, 6:10pm UTC](https://discourse.processing.org/t/moving-and-cutting/11777/2 "2019-06-02T18:10:38Z")

</div>

> [@BallazX](#):
>
> Because of the **noLoop()** function the **keyPressed()** function is not working.

A sketch w/o **draw()** simply doesn’t go beyond **setup()**. 😨  
Even an empty callback **draw()** is enough to turn a sketch interactive: 😸

```auto
void draw() {
}

```

---

<div class="post-metadata">

**Author:** ![toby](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/toby/32/5434_2.png) [@toby](https://discourse.processing.org/u/toby)\
**Post date:** [June 2, 2019, 6:13pm UTC](https://discourse.processing.org/t/moving-and-cutting/11777/3 "2019-06-02T18:13:33Z")

</div>

Exactly as GoToLoop says. And you can always say something like

`if (!redrawNeeded) { return; }`

at the top of your draw() so that draw() only does some drawing when you want it to.

---

<div class="post-metadata">

**Author:** ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)\
**Post date:** [June 2, 2019, 6:24pm UTC](https://discourse.processing.org/t/moving-and-cutting/11777/4 "2019-06-02T18:24:15Z")

</div>

> [@toby](#):
>
> if (!redrawNeeded) { return; }

Actually this part of code looks like this:

```auto
  void setup() {
    size(800, 800);
    background(100,10,10);
    noLoop();    
  }  
void draw(){
      if ( (frameCount & 1) == 0 ) {
      point(mouseX, mouseY);
      return;
      } 
      strokeWeight(1);
      DrawARect(x3, y3, x4, y4); //it's a custom function which draws a rectangle
      cuting_F(pmouseX, pmouseY, mouseX, mouseY); // it cuts out the part of the lines which are outside the rect.
//and a bunch of code goes here...
}

```

😄

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 2, 2019, 6:30pm UTC](https://discourse.processing.org/t/moving-and-cutting/11777/5 "2019-06-02T18:30:43Z")

</div>

You’re lacking **redraw()** in callback **keyPressed()** then. 😬

---

<div class="post-metadata">

**Author:** ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)\
**Post date:** [June 2, 2019, 7:15pm UTC](https://discourse.processing.org/t/moving-and-cutting/11777/6 "2019-06-02T19:15:10Z")

</div>

You hit the nail on the head, thanks! 😉

How can I remove the “previous” rects?

 ![dggddgd](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/ca1a3b9fc873d26a34a401fc9a4a528a924ed330.png)

I’d to store the lines even if they are not visible now, and unhide the parts ofthem if the rectangle is moved into the right spot. Do you have any idea how to do it?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 2, 2019, 7:19pm UTC](https://discourse.processing.org/t/moving-and-cutting/11777/7 "2019-06-02T19:19:26Z")

</div>

Maybe you should consider drawing in separate PGraphics objects, so you can **clear()** 1 w/o affecting the other: 💡  
[Processing.org/reference/createGraphics\_.html](http://Processing.org/reference/createGraphics_.html)

---

<div class="post-metadata">

**Author:** ![BallazX](https://avatars.discourse-cdn.com/v4/letter/b/ecae2f/32.png) [@BallazX](https://discourse.processing.org/u/BallazX)\
**Post date:** [June 2, 2019, 7:49pm UTC](https://discourse.processing.org/t/moving-and-cutting/11777/8 "2019-06-02T19:49:03Z")

</div>

It looks like I can not use PGraphics in case of custom methods.
