R
R
retyui2017-09-02 17:08:30
Algorithms
retyui, 2017-09-02 17:08:30

Cycle from one to zero and back?

I make an animation when the element moves to one side and repels from it and returns, etc. infinite...
I can't implement the change of values ​​on the border,

[1, 0.9, 0.8, ..., 0.2, 0.1, 0, 0.1, 0.2, ...1..0....

Answer the question

In order to leave comments, you need to log in

5 answer(s)
P
Philipp, 2017-09-02
@retyui

Brought you the metacode.

start = 0 // откуда вы начинаете
stop = 1 // куда вы движетесь

step = 0.1 // шаг, с которым вы движетесь

position = 0 // ваше текущее положение

while (true) { // чтобы анимация всегда повторялась, цикл должен быть вечным
  position = position + step // приращиваем наш шаг

  if (position == start || position == stop) { 
               // как только мы достигаем границы
               // меняем знак шага, то есть меняем направление движения
    step = -step
  }
}

This example has only one drawback.
The start point must always be less than the end point.
If you have the opposite, change the sign of the step at the beginning.

R
res2001, 2017-09-02
@res2001

Discover the operation of taking the remainder of the division (mod), with the help of this operation it is achieved twice. Example .

M
mr_serg77, 2017-09-02
@mr_serg77

The first thought is this:

while(true){
showToOne();
showToZero();
}

void showToOne(){
//
}

void showToZero(){
//
}

or you can add recursive calls.

V
Vladimir Olohtonov, 2017-09-02
@sgjurano

while True:
for i in range(0, 1.1, 0.1):
print(i)
for i in range(1, -0.1, -0.1):
print(i)

G
Griboks, 2017-09-02
@Griboks

while(true) y=0,5+0,5*F(x++);
where F(t) - any periodic function that defines the animation,
0.5+0.5*F(t) - offset for the range [0;1].
For example, pendulum swings have a sinusoidal animation F(t) = Sin(t);
uniform shift per step - triangular shape of the animation curve.
List of the most common forms of curved animations (transitions): https://ru.wikipedia.org/wiki/waveform .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question