I have an interesting math and coding question. I had no idea how to explain this in just the title.
I have int iValue = 0;
and every second, iValue
increases by one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 . . .
You get the idea.
So I want to get int newValue = 0
to increase, and then decrease, equally, when iValue
increases. Let me show you an example so you can understand better.
In this example,
iValue
will start at 0, and will increase until it hits 21. WheniValue
hits 21, it resets to 0, and continues to increase to 21… This goes on while the program is running.
iValue: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 0 1 2 3 . . .
Now,
newValue
will rise and fall asiValue
increases.newValue
’s min will be 0, and max 3. Let me show you bothiValue
andnewValue
together:
iValue: --1--2--3--4--5--6--7--8--9--10--11--12--13--14--15--16--17--18--19--20--21
newValue: 0--0--0--1--1--2--2--3--3---3---3---3---3---3---2---2---1---1---0---0---0
Alright, I hope the above example helped make sense. You may notice a few things though:
- Not all the numbers were represented equally
- 3s and 0s got more values and next to each other (because it repeats)
I will need to have enough flexibility to “squeeze” certain values tighter together, and to “spread” others out more over more time. “Squeeze” in the above example means show fewer values like the 1s and 2s. “Spread” in the above example means to cover more of a value like the 3s and 0s.
If none of that helped explain it, here is the real-life-example why I need something like this:
I am creating a day/night cycle on a planet. Now I say planet, because not all planets have the same day/night cycle. So I need a function (or several functions) that will help me be flexible enough between how long the day or night lasts, as well as how long the transition lasts between day/night (which is the “squeezed” values in my other example).
So that is my problem and the goal I hope to achieve in the end. I am sure if I thought more about it and if I had more math knowledge, I could explain it better.
Do you have any suggestions on how I could do this?