# What is wrong with this simple code

**URL:** <https://discourse.processing.org/t/what-is-wrong-with-this-simple-code/34289>\
**Category:** Beginners\
**Created:** [December 25, 2021, 5:55pm UTC](https://discourse.processing.org/t/what-is-wrong-with-this-simple-code/34289 "2021-12-25T17:55:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![parichko](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@parichko](https://discourse.processing.org/u/parichko)\
**Post date:** [December 25, 2021, 5:55pm UTC](https://discourse.processing.org/t/what-is-wrong-with-this-simple-code/34289/1 "2021-12-25T17:55:54Z")

</div>

When the ball goes to the down right corner it freezes. I tried everything. What is wrong with that code.

```auto
let x = 50
let y = 50
let s1 = 1
let s2 = 1
function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  x = x + s1
  y = y + s2
  circle(x,y,100)
  if(x>50 && y > 50){
    s1 = 0
    s2 = 1
  }
  if(x> 50 && y > 350){ 
  s1 = 1
  s2 = 0
  }
  if(x > 350 && y > 350){
     s1 =0
    s2 = -1
     }
}

```

---

<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:** [December 25, 2021, 5:59pm UTC](https://discourse.processing.org/t/what-is-wrong-with-this-simple-code/34289/2 "2021-12-25T17:59:26Z")

</div>

What is your goal?

You always use \>

Maybe in some places you might want \< ?

here is my way

```auto

int x = 50;
int y = 50;
int s1 = 1;
int s2 = 1;

void setup() {
  size(400, 400);
}

void draw() {
  background(220);

  x = x + s1;
  y = y + s2;

  ellipse(x, y, 100, 100);

  if (x>50 && y > 50) {

    s1 = 0;
    s2 = 1;
  }

  if (x> 50 && y > 350) {
    print("c1");
    s1 = 1;
    s2 = 0;
  }

  if (x > 350 ) { // && y > 350) {
    s1 =0;
    s2 = -1;
  }//if

  if (y<50) {
    s1=-1;
    s2=0;
  }
}//func 

```

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 26, 2021, 12:40pm UTC](https://discourse.processing.org/t/what-is-wrong-with-this-simple-code/34289/3 "2021-12-26T12:40:55Z")

</div>

Thanks, @Chrisir.

I had to add this block to the end of the `draw()` function to keep the ellipse within bounds, after it has completed its journey, and arrived back in the upper left corner:

```auto
  if (x<50) {
    s1=1;
    s2=1;
  }

```

Here it is, translated to p5.js, with that block added:

```auto
let x = 50;
let y = 50;
let s1 = 1;
let s2 = 1;

function setup() {
  createCanvas(400, 400);
} // end setup func 

function draw() {
  background(220);
  x = x + s1;
  y = y + s2;
  ellipse(x, y, 100, 100);

  if (x > 50 && y > 50) {
    s1 = 0;
    s2 = 1;
  }

  if (x > 50 && y > 350) {
    print("c1");
    s1 = 1;
    s2 = 0;
  }

  if (x > 350 ) { // && y > 350) {
    s1 = 0;
    s2 = -1;
  }

  if (y < 50) {
    s1 = -1;
    s2 = 0;
  }
  
  if (x < 50) {
    s1 = 1;
    s2 = 1;
  }
} // end draw func 

```

_EDITED 2x on December 26, 2021 to make adjustments to the code._

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [December 26, 2021, 1:35pm UTC](https://discourse.processing.org/t/what-is-wrong-with-this-simple-code/34289/4 "2021-12-26T13:35:01Z")

</div>

Below is a refinement of the p5.js code that provides flexibility for changing the size of the canvas and the ellipse. The code could be refactored further to reorganize the conditional blocks.

```auto
let diam;
let x, y, dx, dy;

function setup() {
  createCanvas(400, 400);
  diam = 80;
  x = diam / 2;
  y = diam / 2;
  dx = 0;
  dy = 1;
} // end setup func 

function draw() {
  background(220);
  x = x + dx;
  y = y + dy;

  ellipse(x, y, diam, diam);

  if (x <= diam / 2 && y <= diam / 2) { // upper left
    dx = 0;
    dy = 1;
  }

  if (x <= diam / 2 && y >= height - diam / 2) { // lower left
    dx = 1;
    dy = 0;
  }

  if (x >= width - diam / 2 && y >= height - diam / 2){ // lower right
    dx = 0;
    dy = -1;
  }

  if (y <= diam / 2 && x >= width - diam / 2) { // upper right
    dx = -1;
    dy = 0;
  }
} // end draw func

```

---

<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:** [December 26, 2021, 5:55pm UTC](https://discourse.processing.org/t/what-is-wrong-with-this-simple-code/34289/5 "2021-12-26T17:55:37Z")

</div>

with arrays - in java flavor

```auto

//

final int phaseDown = 0; 
final int phaseRight = 1;
final int phaseUp = 2;
final int phaseLeft = 3;
int currentPhase = phaseDown; 

final int diam = 80;

PVector[] listPVectorSpeed ={
  new PVector(0, 1), 
  new PVector(1, 0), 
  new PVector(0, -1), 
  new PVector(-1, 0)
};

PVector[] listPVectorCorners = new PVector[4];

float x = diam/2;
float y = diam/2;

// ------------------------------------------------------

void setup() {
  size(400, 400);

  listPVectorCorners [0] = new PVector(diam / 2, height - diam / 2);  
  listPVectorCorners [1] = new PVector(width - diam / 2, height - diam / 2); 
  listPVectorCorners [2] = new PVector(width - diam / 2, diam / 2);
  listPVectorCorners [3] = new PVector(diam / 2, diam / 2);
}

void draw() {
  background(220);

  moveAndRender(); 
  updateCurrentPhase();
  text(currentPhase, 19, 19);
} //func

// ------------------------------------------------------

void moveAndRender() {
  x = x + listPVectorSpeed[currentPhase].x;
  y = y + listPVectorSpeed[currentPhase].y;

  ellipse(x, y, diam, diam);
}

void updateCurrentPhase() {
  PVector pv = listPVectorCorners[currentPhase].copy();
  if (dist(pv.x, pv.y, x, y)<12) {
    x=pv.x; 
    y=pv.y;
    currentPhase++;
    if (currentPhase>3) {
      currentPhase=0;
    }
  }
}
//

```
