@villares has created a proposed new python implementation of PVector for use with py5. I’m not a fan of camel case in python (or ruby) so I changed the signature of PVector.fromAngle
and also enclosed it as a module. But here’s my translation of morph.rb to morph.py making use of numpy along the way:-
import py5
from vector.pvector import PVector
import numpy as np
ALPHA = 45
OMEGA = 405
THETA = 9
state = False
def settings():
py5.size(200, 200)
def setup():
global circle, square, morph
circle = []
square = []
morph = []
angles = range(ALPHA, OMEGA, THETA)
py5.frame_rate(15)
for angle in angles:
circle.append(PVector.from_angle(py5.radians(angle), 50))
morph.append(PVector(0, 0))
nvalues = -np.arange(-50, 51)
pvalues = np.arange(-50, 51)
for x in np.nditer(pvalues[::10]): # top
square.append(PVector(x, -50))
for y in np.nditer(pvalues[::10]): # right side
square.append(PVector(50, y))
for x in np.nditer(nvalues[::-10]): # bottom
square.append(PVector(x, 50))
for y in np.nditer(nvalues[::-10]): # left side
square.append(PVector(-50, y))
def draw():
global state
py5.background(51)
total_distance = 0
for i in range(len(circle)):
v1 = circle[i] if state else square[i]
v2 = morph[i]
v2 = v2.lerp(v1, 0.1)
total_distance += v1.dist(v2)
if (total_distance < 0.08):
state = not(state)
py5.translate(py5.width / 2.0, py5.height / 2.0)
py5.no_fill()
py5.stroke(255)
py5.stroke_weight(4)
py5.begin_shape()
for vec in morph:
py5.vertex(vec.x, vec.y)
py5.end_shape(py5.CLOSE)
py5.run_sketch()
It took longer than expected with some of my usual grievances with python syntax bugging me again, including ternary operator and unexpected indexing of numpy
arrays using nditer
however numpy
scores on performance ground, and gets round limitations of iterating regular
python ranges (things I take for granted in ruby). One for @hx2a and @tabreturn to look at.