# What's the Best Way to Bounce A Variable Between 0 and 255

• (3 Pages)
• 1
• 2
• 3

## 33 Replies - 4493 Views - Last Post: 10 November 2016 - 09:37 AM

### #1 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• Joined: 29-October 16

# What's the Best Way to Bounce A Variable Between 0 and 255

Posted 30 October 2016 - 11:03 AM

Hey all,

So I'm trying to make a label that changes colors and I've got it fluidly changing but I'm wondering what the best way to bounce a variable off 0 and 255, I want the integer ' b ' to be decreasing when it hits 255 and increasing when it hits 0.

As it stands now it just jumps back to 255 because I'm not sure what the proper way to code this is? Can anyone give me an idea for the logic on this? Here's what I've got. *integer is assigned using static int b = 0 right after the construction of the windows form*

This is inside of a windows forms timer with a delay of 10ms

```

string increasing;
if ( b < 255 )
{ b = a + 1;
a = b;

}
if ( b > 254 )
{
b = a - 1;
a = b;

}
if ( b = 255 );
textBox1->Text = gcnew String(increasing.c_str());
label1->ForeColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(a)), static_cast<System::Int32>(static_cast<System::Byte>(0)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));

```

Is This A Good Question/Topic? 0

## Replies To: What's the Best Way to Bounce A Variable Between 0 and 255

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3825
• Posts: 13,941
• Joined: 08-August 08

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 30 October 2016 - 01:23 PM

I'd use a couple of static variables in a function made for this. One to hold the value, and one to hold the amount to increment it by.

### #3 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 30 October 2016 - 01:27 PM

A couple of ways to solve that. You could make a function that would "bounce" a value, taking a value by reference and min/max values by refs to const, then do a bit of logic in the function - if the value equals to max, change the current state to "decrementing", if it's equal to min - change the state to "incrementing", then inc/dec the value depending on current state. The "current state" would be represented by a static variable. [edit: or make it very specific to your need - in which case it can be even simpler]

However, a Bouncer class would be way more reusable. It would do pretty much the same, but could be generic and you could have plenty of independent bouncing values of different types at the same time. ; ) I just wrote a proof of concept that I can share, but I feel you should at the very least try to make it yourself. We're talking about less than 20 lines of simple code. Nothing complicated. ; )

This post has been edited by Xupicor: 30 October 2016 - 01:29 PM

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3825
• Posts: 13,941
• Joined: 08-August 08

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 30 October 2016 - 02:28 PM

Xupicor, on 30 October 2016 - 04:27 PM, said:

However, a Bouncer class would be way more reusable.

Yes. I don't know why I didn't think of it. No need for static variables, and you can bounce as many different values along different ranges and increments as you like with one class!

### #5 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• Joined: 29-October 16

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 30 October 2016 - 04:14 PM

Xupicor, on 30 October 2016 - 01:27 PM, said:

A couple of ways to solve that. You could make a function that would "bounce" a value, taking a value by reference and min/max values by refs to const, then do a bit of logic in the function - if the value equals to max, change the current state to "decrementing", if it's equal to min - change the state to "incrementing", then inc/dec the value depending on current state. The "current state" would be represented by a static variable. [edit: or make it very specific to your need - in which case it can be even simpler]

However, a Bouncer class would be way more reusable. It would do pretty much the same, but could be generic and you could have plenty of independent bouncing values of different types at the same time. ; ) I just wrote a proof of concept that I can share, but I feel you should at the very least try to make it yourself. We're talking about less than 20 lines of simple code. Nothing complicated. ; )

Thanks to both of you for replying! I think this sounds slightly less complicated, can you give me an idea for what I should google to get me started on this? I'm not really sure where to begin with that. What types of commands should I use? If you just give me a few search terms I'll do some research tomorrow and get started. Appreciate the help!

Have a great day.

### #6 Skydiver

• Code herder

Reputation: 7136
• Posts: 24,244
• Joined: 05-May 12

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 30 October 2016 - 04:30 PM

So you want the changes from 0 to 255 and back again be a triangle wave, or a sine wave?

### #7 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• Joined: 29-October 16

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 31 October 2016 - 08:19 AM

Either one would work. I'm not really that picky. If you have any ideas for concepts I should research post them, or if you have some tweaks for my code post. I'm not expecting anyone to write this for me, but on this one I'm not even sure what types of functions I should be using. I'm thinking two completely different segments of code that I can switch between for ascending and descending, just not sure which programming concepts I need to learn to achieve it.

This post has been edited by Skydiver: 01 November 2016 - 09:17 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

### #8 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 31 October 2016 - 08:46 AM

The simple yet not that robust solution we discussed above, using a function, requires knowledge of passing parameters to function by copy and by reference, a static flag, step or value* variable, knowledge of in/decrementing a variable or assigning and addition, and of if statement.

__
*) less general, no pass-by-ref needed

### #9 CTphpnwb

• D.I.C Lover

Reputation: 3825
• Posts: 13,941
• Joined: 08-August 08

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 31 October 2016 - 09:29 AM

If you don't care, then the easiest it to increment by 1 each time it's called until you reach the maximum, then increment by -1 after that until it reaches the minimum. Just to be clear, you don't want a constant as your increment value!

### #10 jjl

• Engineer

Reputation: 1271
• Posts: 4,998
• Joined: 09-June 09

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 31 October 2016 - 05:00 PM

Sinusoidal would look smoother near the points of the bounce.

Sin goes from -1 to 1. You need to scale it to oscillate between 0 to 1. To do this, you apply a DC offset of 1, then simply scale it by dividing by 2.
```// Loop over radians forever (ish)

// Normalize sin wave between 0 - 255
double value = 255 * (sin(rad) + 1) / 2.0;
}

```

This post has been edited by jjl: 31 October 2016 - 05:02 PM

### #11 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• Joined: 29-October 16

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 01 November 2016 - 06:35 AM

Thanks for the responses. As for the method of developing this I was thinking about using two seperate timers then having only one activated at a time, perhaps have one be disabled at 255 and the other be disabled at 0. The increasing timer would be disabled at 255 and enabled again at 0 and vice versa for the decreasing. Do you guys think this would work? Is this not a good way to do it? Appreciate the help

jjl, on 31 October 2016 - 05:00 PM, said:

Sinusoidal would look smoother near the points of the bounce.

Sin goes from -1 to 1. You need to scale it to oscillate between 0 to 1. To do this, you apply a DC offset of 1, then simply scale it by dividing by 2.
```// Loop over radians forever (ish)

// Normalize sin wave between 0 - 255
double value = 255 * (sin(rad) + 1) / 2.0;
}

```

I will implement this once I get the value bouncing back and forth, but this is just to cause the bounce to be in a sin fashion right? It doesn't actually bounce the value does it?

### #12 Skydiver

• Code herder

Reputation: 7136
• Posts: 24,244
• Joined: 05-May 12

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 01 November 2016 - 06:40 AM

All you need is a single timer event that tells the value to be updated. The class (or function with statics/globals) then simply updates the value.

Yes, that sine code will bounce the value, and bounce it very smoothly. This is why I originally asked if you were looking for a sawtooth or a sine curve.

### #13 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• Joined: 29-October 16

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 01 November 2016 - 08:58 AM

Nice!!! Ty !!!! I'll give it a shot here after lunch. />

This post has been edited by Skydiver: 01 November 2016 - 09:16 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

### #14 baavgai

• Dreaming Coder

Reputation: 7501
• Posts: 15,544
• Joined: 16-October 07

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 01 November 2016 - 10:26 AM

Just a quick proof of concept. Almost certainly overkill, but C++ offers many amusing ways to get here.

```#include <iostream>

class Val {
public:
Val(int max);
Val(int min, int max);
Val &operator+=(int);
operator int() const;
Val &operator-();
private:
int value, min, max;
bool delta;
};

int main() {
Val x(2, 9);
for (int i = 1; i <= 10; i++) {
std::cout << i << ' ' << x << std::endl;
x += 2;
}
return 0;
}

```

Results:
```1 2
2 4
3 6
4 8
5 9
6 7
7 5
8 3
9 2
10 4

```

The cool part about this particular beast is that it will get treated as an int as need. I avoided the myriad of other overloads you might do with this. Though, I did find myself thinking of your standard, gaming, 2D vector where operations like * -1 will send you off elsewhere. This is almost the 1D version.

### #15 hexagod

• 😂😂😂

Reputation: 31
• Posts: 577
• Joined: 29-October 16

## Re: What's the Best Way to Bounce A Variable Between 0 and 255

Posted 01 November 2016 - 11:17 AM

Awesome! thanks so much! I'll try and synthesize your code and Skydiver's to see what I can come up with !!! Appreciate this very much! You guys rock.

This post has been edited by Skydiver: 01 November 2016 - 09:16 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.