Loop for same variable but opposite directions

Hi everyone,

I have been using two loops to generate generate variables one of them going up from 0 - 360 and the other going in the opposite direction from 360 - 0. The code I have been using for this looks like this:

      b = 0 + bSpeed;
      if (b > 360) {
        b = 359 - bSpeed;
      }
    
      b2 = 360 - bSpeed;
      if (b2 < 0) {
        b2 = 1 + bSpeed;
      }

Is there a simpler way to do this?

Hello,

Example:

int b = 0;
int dir = 1;
int bSpeed = 1;

void setup()
  {
  size(100, 100);
  frameRate(5); // Slow down for testing
  }

void draw() 
	{
  if(b>10) dir*= -1;
  if(b<0) dir*= -1;
    
  b += bSpeed*dir;
  
  println(b, bSpeed, dir);  // Show in console
	}

:)

1 Like

Thank you brother this is exactly what I was looking for.

1 Like