¡Esta es una revisión vieja del documento!
Si queremos mover la variable X de un punto pA a otro pB en N pasos tendremos la siguiente función (siendo i el paso actual):
En Python con PyGame lo podríamos programar de la siguiente forma:
import pygame from pygame.locals import * import sys def main (): i = 0 pointA = 100 pointB = 500 x = pointA NumFrames = 100 pygame.init() screen = pygame.display.set_mode((640,480)) last_update = pygame.time.get_ticks() color = (0,0,0) while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() screen.fill(color) if (pygame.time.get_ticks() - last_update > 10): if (i> NumFrames): i = 0 (pointA, pointB) = (pointB, pointA) last_update = pygame.time.get_ticks() x = ((pointB * i) + (pointA * (NumFrames - i))) / NumFrames i = i + 1 pygame.draw.circle(screen, (0,0,255), (x, 200), 20) pygame.display.update() if __name__ == '__main__': main()
O, el mismo código, con Processing:
int pointA = 100; int pointB = 500; float x = pointA; float v = 0.0f; int i = 0; int numFrames = 100; void setup () { size (640, 480); } void draw () { background(255); if (i > numFrames) { i = 0; int tmp = pointB; pointB = pointA; pointA = tmp; } x = ((pointB * i) + pointA * (numFrames - i)) / numFrames; i ++; ellipse(x, 200, 20, 20); }
Poniendolo de otra forma tenemos que la velocidad es: y la función quedaría de la siguiente forma:
.
En python:
v = i / float(NumFrames) x = (pointA * (1 - v)) + (pointB * v)
En processing:
v = i / (float)numFrames; x = (pointA * (1 - v)) + (pointB * v);
Es una función matemática que se usa para crear un gran número de vectores con el significado de intensidad y esta intensidad puede hacer referencia a varios conceptos que requieran aleatoriedad sin perder continuidad (degradados para texturas (de madera), posición de elementos en un mapa en un juego, creación de terrenos 3d…).