# How to make this rotating square reverse direction

**URL:** <https://discourse.processing.org/t/how-to-make-this-rotating-square-reverse-direction/35957>\
**Category:** Beginners\
**Created:** [March 26, 2022, 8:59pm UTC](https://discourse.processing.org/t/how-to-make-this-rotating-square-reverse-direction/35957 "2022-03-26T20:59:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hasse](https://avatars.discourse-cdn.com/v4/letter/h/4af34b/32.png) [@Hasse](https://discourse.processing.org/u/Hasse)\
**Post date:** [March 26, 2022, 8:59pm UTC](https://discourse.processing.org/t/how-to-make-this-rotating-square-reverse-direction/35957/1 "2022-03-26T20:59:59Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hello! I’m quite new to this so this is probably really easy but I’ve made this rotating square that I want to, after having completed a certain percentage of the rotation, reverse direction and “swing back”. Then I want it to reverse direction again so it moves in the same direction again. Kind of like a pendulum (that’s not affected by gravity!).

I’ve tried to use if loops but I think I’m getting my maths confused because I told it to if \<5, move forward and if\>5 move backward, but of course, this means that it just freezes in place.

This is the code I have right now.

Thank you!

```auto
//moiree

int a = 0;

void setup(){
  size(800, 800, P2D);
  background(50);
}

void draw(){
  background(50);

pushMatrix();

rotate(radians(a));

// if (a>5){
// a=5;
// a=a-0.05;
// };
	
	if (a<5){
a=a+0.05;
	}

  
for(float q=30; q<width -30; q=q+7){
  stroke(255);
  strokeWeight(2);
  line(50,q, width-50, q);
};
popMatrix();

pushMatrix();
for(float c=30; c<width -30; c=c+7){
  stroke(255);
  strokeWeight(2);
  line(50,c, width-50, c);
};
popMatrix();

print(a);

  

}

```

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [March 26, 2022, 11:01pm UTC](https://discourse.processing.org/t/how-to-make-this-rotating-square-reverse-direction/35957/2 "2022-03-26T23:01:01Z")

</div>

hi,  
at each frame you evaluate a  
and a is the current rotation, this information alone can’t tell if you need to rotate clockwise or anti-clockwise (it depends from wich direction you come from)  
it means you need another information , aka another variable  
many ways to do that , for eg:  
you can use a boolean, eg boolean clockwise=true;  
in pseudo code it will be:  
if clockwise a=a+0.5 else a=a-0.5  
then you need to define the rules when rotation is clockwise or reverse  
if a\>5 clockwise= false;  
if a \<-5 clockwise= true;

tell me if i 'm not clear
