From ae2b6ef48b89e36a1a7c54bad9b7404e88216b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?charlotte=20=F0=9F=8C=B8?= Date: Tue, 1 Dec 2026 12:28:50 -0800 Subject: [PATCH 1/2] Vector fixes. --- .../examples/Demos/Graphics/particles.py | 4 +- .../examples/Demos/Graphics/patch.py | 12 +- .../examples/Demos/Graphics/ribbons.py | 42 ++-- .../examples/Demos/Graphics/trefoil.py | 10 +- .../examples/Demos/Tests/multiple_windows.py | 6 +- .../01_P/P_2_1_5_04/P_2_1_5_04.py | 6 +- .../01_P/P_2_2_6_01/P_2_2_6_01.py | 4 +- .../P_2_2_6_01_class_version.py | 4 +- .../01_P/P_2_2_6_02/P_2_2_6_02.py | 4 +- .../01_P/P_2_2_6_03/P_2_2_6_03.py | 8 +- .../01_P/P_2_2_6_04/P_2_2_6_04.py | 6 +- .../01_P/P_2_2_6_05/P_2_2_6_05.py | 8 +- .../01_P/P_3_2_4_01/P_3_2_4_01.py | 6 +- .../02_M/M_1_5_02_TOOL/M_1_5_02_TOOL.py | 8 +- .../02_M/M_1_5_03_TOOL/M_1_5_03_TOOL.py | 4 +- .../02_M/M_1_5_04_TOOL/M_1_5_04_TOOL.py | 4 +- .../02_M/M_1_6_01_TOOL/M_1_6_01_TOOL.py | 12 +- .../02_M/M_1_6_02_TOOL/M_1_6_02_TOOL.py | 12 +- .../02_M/M_2_4_01/M_2_4_01.py | 2 +- .../02_M/M_4_2_01/M_4_2_01.py | 6 +- .../02_M/M_4_2_02/M_4_2_02.py | 6 +- .../02_M/M_4_2_03/M_4_2_03.py | 6 +- .../02_M/M_4_2_04/M_4_2_04.py | 6 +- .../02_M/M_4_3_01_TOOL/M_4_3_01_TOOL.py | 8 +- .../02_M/M_4_4_01_TOOL/M_4_4_01_TOOL.py | 8 +- .../02_M/M_6_1_01/M_6_1_01.py | 6 +- .../02_M/M_6_1_02/M_6_1_02.py | 20 +- .../02_M/M_6_1_03/M_6_1_03.py | 20 +- .../02_M/M_6_3_01/M_6_3_01.py | 24 +- .../02_M/M_6_4_01_TOOL/M_6_4_01_TOOL.py | 24 +- .../Generative Design/03_A/Cover/Cover.py | 24 +- .../Force_Directed_Tags.py | 20 +- .../Create Shapes/particle_system_pshape.py | 8 +- .../Curves/arc_length_parametrization.py | 4 +- .../Topics/Fractals and L-Systems/koch.py | 16 +- .../examples/Topics/Geometry/icosahedra.py | 2 +- .../Topics/Geometry/shape_transform.py | 2 +- .../examples/Topics/Geometry/space_junk.py | 2 +- .../examples/Topics/Geometry/toroid.py | 2 +- .../examples/Topics/Geometry/vertices.py | 2 +- .../Topics/Motion/circle_collision.py | 14 +- .../Topics/Motion/cubes_within_cube.py | 4 +- .../examples/Topics/Motion/morph.py | 5 +- .../examples/Topics/Motion/reflection_1.py | 13 +- .../examples/Topics/Motion/reflection_2.py | 4 +- .../examples/Topics/Simulate/flocking.py | 46 ++-- .../Topics/Simulate/forces_with_vectors.py | 12 +- .../Simulate/gravitational_attraction_3d.py | 10 +- .../Simulate/multiple_particle_systems.py | 4 +- .../Topics/Simulate/simple_particle_system.py | 4 +- .../Topics/Simulate/smoke_particle_system.py | 8 +- .../Vectors/acceleration_with_vectors.py | 8 +- .../examples/Topics/Vectors/bouncing_ball.py | 5 +- .../examples/Topics/Vectors/vector_math.py | 6 +- crates/processing_pyo3/examples/flocking.py | 46 ++-- crates/processing_pyo3/src/color.rs | 13 +- crates/processing_pyo3/src/math.rs | 223 ++---------------- 57 files changed, 316 insertions(+), 487 deletions(-) diff --git a/crates/processing_pyo3/examples/Demos/Graphics/particles.py b/crates/processing_pyo3/examples/Demos/Graphics/particles.py index 9211c83f..e6548551 100644 --- a/crates/processing_pyo3/examples/Demos/Graphics/particles.py +++ b/crates/processing_pyo3/examples/Demos/Graphics/particles.py @@ -45,7 +45,7 @@ def rebirth(self, x, y): a = random(TWO_PI) speed = random(0.5, 4) self.velocity = Vec2(cos(a), sin(a)) - self.velocity.mult(speed) + self.velocity *= speed self.lifespan = 255 self.x = x self.y = y @@ -58,7 +58,7 @@ def is_dead(self): def update(self): self.lifespan = self.lifespan - 1 - self.velocity.add(self.gravity) + self.velocity += self.gravity self.x += self.velocity.x self.y += self.velocity.y diff --git a/crates/processing_pyo3/examples/Demos/Graphics/patch.py b/crates/processing_pyo3/examples/Demos/Graphics/patch.py index 71d20563..e38c73ea 100644 --- a/crates/processing_pyo3/examples/Demos/Graphics/patch.py +++ b/crates/processing_pyo3/examples/Demos/Graphics/patch.py @@ -66,8 +66,8 @@ def build(): for j in range(RESJ): muj = j / (RESJ - 1) out = Vec3() - uitang.set(0, 0, 0) - ujtang.set(0, 0, 0) + uitang = Vec3(0, 0, 0) + ujtang = Vec3(0, 0, 0) for ki in range(ni + 1): bi = bezier_blend(ki, mui, ni) dbi = d_bezier_blend(ki, mui, ni) @@ -85,12 +85,12 @@ def build(): ujtang.x += inp[ki][kj].x * bi * dbj ujtang.y += inp[ki][kj].y * bi * dbj ujtang.z += inp[ki][kj].z * bi * dbj - out.add(Vec3(-ni / 2, -nj / 2, 0)) - out.mult(100) + out += Vec3(-ni / 2, -nj / 2, 0) + out *= 100 out_row.append(out) - uitang.normalize() - ujtang.normalize() + uitang = uitang.normalized() + ujtang = ujtang.normalized() norm_row.append(uitang.cross(ujtang)) outp.append(out_row) normp.append(norm_row) diff --git a/crates/processing_pyo3/examples/Demos/Graphics/ribbons.py b/crates/processing_pyo3/examples/Demos/Graphics/ribbons.py index 5025c077..acf2a469 100644 --- a/crates/processing_pyo3/examples/Demos/Graphics/ribbons.py +++ b/crates/processing_pyo3/examples/Demos/Graphics/ribbons.py @@ -87,7 +87,7 @@ def __init__(self, center_x, center_y, radius): def mouse_pressed(self): self.v_down = self.mouse_to_sphere(mouse_x, mouse_y) - self.q_down.set(self.q_now) + self.q_down = self.q_now.copy() self.q_drag.reset() def mouse_dragged(self): @@ -110,7 +110,7 @@ def mouse_to_sphere(self, x, y): m = v.x * v.x + v.y * v.y if m > 1.0: - v.normalize() + v = v.normalized() else: v.z = sqrt(1.0 - m) @@ -118,7 +118,7 @@ def mouse_to_sphere(self, x, y): def constrain_vector(self, vector, axis): res = vector - axis * axis.dot(vector) - res.normalize() + res = res.normalized() return res def apply_quat_2_matrix(self, q): @@ -289,7 +289,9 @@ def set_c_point(self, n, p): # Gets n-th control point. def get_c_point(self, n, p): - p.set(self.bspline_c_points[n][0], self.bspline_c_points[n][1], self.bspline_c_points[n][2]) + p.x = self.bspline_c_points[n][0] + p.y = self.bspline_c_points[n][1] + p.z = self.bspline_c_points[n][2] # Replaces the current B-spline control points(0, 1, 2) with(1, 2, 3). This # is used when a new spline is to be joined to the recently drawn. @@ -315,11 +317,15 @@ def update_matrix3(self): def feval(self, t, p): self.eval_point(t) - p.set(self.pt[0], self.pt[1], self.pt[2]) + p.x = self.pt[0] + p.y = self.pt[1] + p.z = self.pt[2] def deval(self, t, d): self.eval_tangent(t) - d.set(self.tg[0], self.tg[1], self.tg[2]) + d.x = self.tg[0] + d.y = self.tg[1] + d.z = self.tg[2] def feval_x(self, t): self.eval_point(t) @@ -652,17 +658,17 @@ def generate_flat_ribbon(vertices, normals): # The normal is calculated. normal1 = transversal.cross(tangent) - normal1.normalize() + normal1 = normal1.normalized() for ui in range(1, 11): if ui % uspacing == 0: u = 0.1 * ui # The geometry of the previous iteration is saved. - sid1_point0.set(sid1_point1) - cent_point0.set(cent_point1) - sid2_point0.set(sid2_point1) - normal0.set(normal1) + sid1_point0 = sid1_point1.copy() + cent_point0 = cent_point1.copy() + sid2_point0 = sid2_point1.copy() + normal0 = normal1.copy() # The new geometry is generated. spline_side1.feval(u, sid1_point1) @@ -675,7 +681,7 @@ def generate_flat_ribbon(vertices, normals): transversal = sid1_point1 - sid2_point1 # The normal is calculated. normal1 = transversal.cross(tangent) - normal1.normalize() + normal1 = normal1.normalized() # The (Sid1Point0, Sid1Point1, CentPoint1) triangle is added. vertices.append(cent_point1.copy()) @@ -733,6 +739,7 @@ def shift_control_points(): # Adds a new control point to the arrays CPCenter, CPRight and CPLeft def add_control_points(ca0, ox0, ca1, ss, handness): + global flip_test_v a = ca1 - ca0 b = ox0 - ca0 @@ -744,13 +751,13 @@ def add_control_points(ca0, ox0, ca1, ss, handness): d = c.cross(a) # Normalizing vectors. - c.normalize() - d.normalize() + c = c.normalized() + d = d.normalized() # Flipping test (to avoid self crossing in the strands). if ss != HELIX and 90.0 < degrees(Vec3.angle_between(flip_test_v, d)): # Flip detected. The plane vector is inverted. - d.mult(-1.0) + d *= -1.0 # The central control point is constructed. cpt0 = linear_comb(0.5, ca0, 0.5, ca1) @@ -772,17 +779,18 @@ def add_control_points(ca0, ox0, ca1, ss, handness): spline_side2.set_c_point(3, cpt2) # Saving the plane vector (for the flipping test in the next call). - flip_test_v.set(d) + flip_test_v = d.copy() def construct_control_points(residues, res, ss, handness): + global flip_test_v p1 = Vec3() p2 = Vec3() p3 = Vec3() if res == 0: # The control points 2 and 3 are created. - flip_test_v.set(0, 0, 0) + flip_test_v = Vec3(0, 0, 0) res0 = residues[res] res1 = residues[res + 1] diff --git a/crates/processing_pyo3/examples/Demos/Graphics/trefoil.py b/crates/processing_pyo3/examples/Demos/Graphics/trefoil.py index a2b2e7d0..640438b4 100644 --- a/crates/processing_pyo3/examples/Demos/Graphics/trefoil.py +++ b/crates/processing_pyo3/examples/Demos/Graphics/trefoil.py @@ -108,11 +108,11 @@ def eval_normal(u, v): p = eval_point(u, v) tang_u = eval_point(u + 0.01, v) tang_v = eval_point(u, v + 0.01) - tang_u.sub(p) - tang_v.sub(p) + tang_u -= p + tang_v -= p norm_uv = tang_v.cross(tang_u) - norm_uv.normalize() + norm_uv = norm_uv.normalized() return norm_uv @@ -137,9 +137,9 @@ def eval_point(u, v): dv.z = 1.5 * c * cos(1.5 * t) q = dv - q.normalize() + q = q.normalized() qvn = Vec3(q.y, -q.x, 0) - qvn.normalize() + qvn = qvn.normalized() ww = q.cross(qvn) pt = Vec3() diff --git a/crates/processing_pyo3/examples/Demos/Tests/multiple_windows.py b/crates/processing_pyo3/examples/Demos/Tests/multiple_windows.py index 5d2cd567..fe28eb1f 100644 --- a/crates/processing_pyo3/examples/Demos/Tests/multiple_windows.py +++ b/crates/processing_pyo3/examples/Demos/Tests/multiple_windows.py @@ -117,7 +117,7 @@ def p_mouse_y(self): def mouse_pressed(self): self.v_down = self.mouse_to_sphere(self.p_mouse_x(), self.p_mouse_y()) - self.q_down.set(self.q_now) + self.q_down = self.q_now.copy() self.q_drag.reset() def mouse_dragged(self): @@ -148,7 +148,7 @@ def mouse_to_sphere(self, x, y): m = v.x * v.x + v.y * v.y if m > 1.0: - v.normalize() + v = v.normalized() else: v.z = sqrt(1.0 - m) @@ -156,7 +156,7 @@ def mouse_to_sphere(self, x, y): def constrain_vector(self, vector, axis): res = vector - axis * axis.dot(vector) - res.normalize() + res = res.normalized() return res def apply_quat_2_matrix(self, q): diff --git a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_1_5_04/P_2_1_5_04.py b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_1_5_04/P_2_1_5_04.py index 3d8d8401..43447b70 100644 --- a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_1_5_04/P_2_1_5_04.py +++ b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_1_5_04/P_2_1_5_04.py @@ -71,9 +71,9 @@ def draw(self): previous_pos = self.shape_path[index - 1] a = atan2(previous_pos.y - pos.y, previous_pos.x - pos.x) offset_pos = Vec2.from_angle(a) - offset_pos.add(0, i) - offset_pos = offset_pos.rotate(a) - offset_pos.add(pos) + offset_pos += Vec2(0, i) + offset_pos = offset_pos.rotated(a) + offset_pos += pos curve_vertex(offset_pos.x, offset_pos.y) end_shape() i += density diff --git a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_01/P_2_2_6_01.py b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_01/P_2_2_6_01.py index 97e361cf..436d8492 100644 --- a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_01/P_2_2_6_01.py +++ b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_01/P_2_2_6_01.py @@ -93,8 +93,8 @@ def draw(): if i % 2 == 1: a = -a next_pos = Vec2.from_angle(radians(a)) - next_pos.set_mag((joints - i) / joints * line_length) - next_pos.add(pos) + next_pos = next_pos.with_mag((joints - i) / joints * line_length) + next_pos += pos if show_pendulum: no_stroke() diff --git a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_01_class_version/P_2_2_6_01_class_version.py b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_01_class_version/P_2_2_6_01_class_version.py index 5e3c4e06..723d023e 100644 --- a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_01_class_version/P_2_2_6_01_class_version.py +++ b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_01_class_version/P_2_2_6_01_class_version.py @@ -72,8 +72,8 @@ def draw(self): next_pos = Vec2.from_angle(radians(a)) - next_pos.set_mag((self.line_length / self.joints) * (self.joints - i)) - next_pos.add(pos) + next_pos = next_pos.with_mag((self.line_length / self.joints) * (self.joints - i)) + next_pos += pos if show_pendulum: no_stroke() diff --git a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_02/P_2_2_6_02.py b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_02/P_2_2_6_02.py index a3ee6730..eae634a7 100644 --- a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_02/P_2_2_6_02.py +++ b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_02/P_2_2_6_02.py @@ -88,8 +88,8 @@ def draw(self): self.iterator / pow(-2, self.joints - i) * self.speed ) - offset_pos_b.set_mag((self.line_length / self.joints) * (self.joints - i)) - offset_pos_b.add(offset_pos_a) + offset_pos_b = offset_pos_b.with_mag((self.line_length / self.joints) * (self.joints - i)) + offset_pos_b += offset_pos_a if show_pendulum: fill(0.0, 0.1) diff --git a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_03/P_2_2_6_03.py b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_03/P_2_2_6_03.py index acb0f2bf..466779e0 100644 --- a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_03/P_2_2_6_03.py +++ b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_03/P_2_2_6_03.py @@ -118,7 +118,7 @@ def __init__(self, size, hierarchy): self.pendulum_arm = Pendulum(self.size / 1.5, self.hierarchy) def update(self, heading): - self.end.set(self.origin.x + self.size * sin(self.angle), self.origin.y + self.size * cos(self.angle)) + self.end = Vec2(self.origin.x + self.size * sin(self.angle), self.origin.y + self.size * cos(self.angle)) self.angular_acceleration = (-self.gravity / self.size) * sin(self.angle + heading) self.angle += self.angular_velocity @@ -131,15 +131,15 @@ def update(self, heading): def get_trail(self, offset, end=None): if self.pendulum_arm is not None: if end is not None: - end.add(self.end) + end += self.end else: end = self.end.copy() return self.pendulum_arm.get_trail(offset, end) else: trail = self.end.copy() if end is not None: - trail.add(end) - trail.add(offset) + trail += end + trail += offset return trail def draw(self): diff --git a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_04/P_2_2_6_04.py b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_04/P_2_2_6_04.py index e86fca2e..7a17a4f6 100644 --- a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_04/P_2_2_6_04.py +++ b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_04/P_2_2_6_04.py @@ -132,7 +132,7 @@ def __init__(self, size, hierarchy): i += 1 def update(self, heading): - self.end.set(self.origin.x + self.size * sin(self.angle), self.origin.y + self.size * cos(self.angle)) + self.end = Vec2(self.origin.x + self.size * sin(self.angle), self.origin.y + self.size * cos(self.angle)) self.angular_acceleration = (-self.gravity / self.size) * sin(self.angle + heading) self.angle += self.angular_velocity @@ -147,14 +147,14 @@ def get_trail(self, offset, pendulum_trail_paths=None): pendulum_trail_paths = [] offset = offset.copy() - offset.add(self.end) + offset += self.end for pendulum_arm in self.pendulum_arms: if len(pendulum_arm.pendulum_arms) > 0: pendulum_arm.get_trail(offset, pendulum_trail_paths) else: trail = offset.copy() - trail.add(pendulum_arm.end) + trail += pendulum_arm.end pendulum_trail_paths.append(trail) return pendulum_trail_paths diff --git a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_05/P_2_2_6_05.py b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_05/P_2_2_6_05.py index 44977e52..3a45727a 100644 --- a/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_05/P_2_2_6_05.py +++ b/crates/processing_pyo3/examples/Generative Design/01_P/P_2_2_6_05/P_2_2_6_05.py @@ -142,7 +142,7 @@ def __init__(self, size, hierarchy): self.pendulum_arm = Pendulum(self.size / 1.5, self.hierarchy) def update(self, heading): - self.end.set(self.origin.x + self.size * sin(self.angle), self.origin.y + self.size * cos(self.angle)) + self.end = Vec2(self.origin.x + self.size * sin(self.angle), self.origin.y + self.size * cos(self.angle)) self.angular_acceleration = (-self.gravity / self.size) * sin(self.angle + heading) self.angle += self.angular_velocity @@ -155,15 +155,15 @@ def update(self, heading): def get_trail(self, offset, end=None): if self.pendulum_arm is not None: if end is not None: - end.add(self.end) + end += self.end else: end = self.end.copy() return self.pendulum_arm.get_trail(offset, end) else: trail = self.end.copy() if end is not None: - trail.add(end) - trail.add(offset) + trail += end + trail += offset return trail def draw(self): diff --git a/crates/processing_pyo3/examples/Generative Design/01_P/P_3_2_4_01/P_3_2_4_01.py b/crates/processing_pyo3/examples/Generative Design/01_P/P_3_2_4_01/P_3_2_4_01.py index 45fe96ff..30a7b020 100644 --- a/crates/processing_pyo3/examples/Generative Design/01_P/P_3_2_4_01/P_3_2_4_01.py +++ b/crates/processing_pyo3/examples/Generative Design/01_P/P_3_2_4_01/P_3_2_4_01.py @@ -63,9 +63,9 @@ def draw(self): p0 = Vec2(pos[0], pos[1]) p1 = Vec2(next_pos[0], next_pos[1]) v = p1 - p0 - v = v.normalize() - v = v.rotate(HALF_PI) - v.mult(d) + v = v.normalized() + v = v.rotated(HALF_PI) + v *= d pneu = p0 + v curve_vertex(pneu.x, pneu.y) diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_02_TOOL/M_1_5_02_TOOL.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_02_TOOL/M_1_5_02_TOOL.py index 2a0b0d8c..fa6fe86c 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_02_TOOL/M_1_5_02_TOOL.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_02_TOOL/M_1_5_02_TOOL.py @@ -69,12 +69,12 @@ def update1(self): if self.is_outside: self.p.x = random(width) self.p.y = random(height) - self.p_old.set(self.p) + self.p_old = self.p.copy() stroke_weight(stroke_width * self.step_size) line(self.p_old.x, self.p_old.y, self.p.x, self.p.y) - self.p_old.set(self.p) + self.p_old = self.p.copy() self.is_outside = False @@ -97,12 +97,12 @@ def update2(self): if self.is_outside: self.p.x = random(width) self.p.y = random(height) - self.p_old.set(self.p) + self.p_old = self.p.copy() stroke_weight(stroke_width * self.step_size) line(self.p_old.x, self.p_old.y, self.p.x, self.p.y) - self.p_old.set(self.p) + self.p_old = self.p.copy() self.is_outside = False diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_03_TOOL/M_1_5_03_TOOL.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_03_TOOL/M_1_5_03_TOOL.py index 37e024ab..54b45ae7 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_03_TOOL/M_1_5_03_TOOL.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_03_TOOL/M_1_5_03_TOOL.py @@ -74,7 +74,7 @@ def update1(self): stroke_weight(stroke_width * self.step_size) line(self.p_old.x, self.p_old.y, self.p.x, self.p.y) - self.p_old.set(self.p) + self.p_old = self.p.copy() self.noise_z += self.noise_z_velocity def update2(self): @@ -97,7 +97,7 @@ def update2(self): stroke_weight(stroke_width * self.step_size) line(self.p_old.x, self.p_old.y, self.p.x, self.p.y) - self.p_old.set(self.p) + self.p_old = self.p.copy() self.noise_z += self.noise_z_velocity def set_noise_z_range(self, the_noise_z_range): diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_04_TOOL/M_1_5_04_TOOL.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_04_TOOL/M_1_5_04_TOOL.py index d79a1bcc..806245c0 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_04_TOOL/M_1_5_04_TOOL.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_5_04_TOOL/M_1_5_04_TOOL.py @@ -91,7 +91,7 @@ def update1(self): line(0, -agent_width, 0, agent_width) pop_matrix() - self.p_old.set(self.p) + self.p_old = self.p.copy() self.z_noise += self.z_noise_velocity def update2(self): @@ -115,7 +115,7 @@ def update2(self): agent_width = remap(self.randomizer, 0, 1, agent_width_min, agent_width_max) * 2 ellipse(self.p_old.x, self.p_old.y, agent_width, agent_width) - self.p_old.set(self.p) + self.p_old = self.p.copy() self.z_noise += self.z_noise_velocity def set_noise_sticking(self, sticking_range): diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_6_01_TOOL/M_1_6_01_TOOL.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_6_01_TOOL/M_1_6_01_TOOL.py index 2e076607..69a627bb 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_6_01_TOOL/M_1_6_01_TOOL.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_6_01_TOOL/M_1_6_01_TOOL.py @@ -122,9 +122,9 @@ def update(self, the_p, the_is_gap): # shift the values to the right side # simple queue for i in range(self.count - 1, 0, -1): - self.p[i].set(self.p[i - 1]) + self.p[i] = self.p[i - 1] self.is_gap[i] = self.is_gap[i - 1] - self.p[0].set(the_p) + self.p[0] = the_p.copy() self.is_gap[0] = the_is_gap def draw_mesh_ribbon(self, the_mesh_col, the_width): @@ -146,11 +146,11 @@ def draw_mesh_ribbon(self, the_mesh_col, the_width): v2 = self.p[i + 1] + self.p[i] v3 = v1.cross(v2) v2 = v1.cross(v3) - # v1 = v1.normalize() - v2 = v2.normalize() - # v3 = v3.normalize() + # v1 = v1.normalized() + v2 = v2.normalized() + # v3 = v3.normalized() # v1.mult(the_width) - v2.mult(the_width) + v2 *= the_width # v3.mult(the_width) strip.vertex(self.p[i].x + v2.x, self.p[i].y + v2.y, self.p[i].z + v2.z) strip.vertex(self.p[i].x - v2.x, self.p[i].y - v2.y, self.p[i].z - v2.z) diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_6_02_TOOL/M_1_6_02_TOOL.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_6_02_TOOL/M_1_6_02_TOOL.py index 269516ce..be2d2371 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_1_6_02_TOOL/M_1_6_02_TOOL.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_1_6_02_TOOL/M_1_6_02_TOOL.py @@ -170,9 +170,9 @@ def __init__(self, the_p, the_count): def update(self, the_p, the_is_wraped): # shift the values to the right side for i in range(self.count - 1, 0, -1): - self.p[i].set(self.p[i - 1]) + self.p[i] = self.p[i - 1] self.is_gap[i] = self.is_gap[i - 1] - self.p[0].set(the_p) + self.p[0] = the_p.copy() self.is_gap[0] = the_is_wraped def draw_mesh_ribbon(self, the_mesh_col, the_width): @@ -194,11 +194,11 @@ def draw_mesh_ribbon(self, the_mesh_col, the_width): v2 = self.p[i + 1] + self.p[i] v3 = v1.cross(v2) v2 = v1.cross(v3) - # v1 = v1.normalize() - v2 = v2.normalize() - # v3 = v3.normalize() + # v1 = v1.normalized() + v2 = v2.normalized() + # v3 = v3.normalized() # v1.mult(the_width) - v2.mult(the_width) + v2 *= the_width # v3.mult(the_width) strip.vertex(self.p[i].x + v2.x, self.p[i].y + v2.y, self.p[i].z + v2.z) strip.vertex(self.p[i].x - v2.x, self.p[i].y - v2.y, self.p[i].z - v2.z) diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_2_4_01/M_2_4_01.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_2_4_01/M_2_4_01.py index 85b215bd..0ab88c03 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_2_4_01/M_2_4_01.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_2_4_01/M_2_4_01.py @@ -120,7 +120,7 @@ def calculate_lissajous_points(): if i % 3 == 0: # lit geometry needs a normal per vertex n = lissajous_points[i].cross(lissajous_points[i + 2]) - n.normalize() + n = n.normalized() triangles.normal(n.x, n.y, n.z) # gradient for every trinangle to lissajou path triangles.color(50 / 255, 50 / 255, 50 / 255, 1.0) diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_01/M_4_2_01.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_01/M_4_2_01.py index 1e4a526d..a8a3c980 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_01/M_4_2_01.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_01/M_4_2_01.py @@ -84,7 +84,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -93,7 +93,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -125,7 +125,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_02/M_4_2_02.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_02/M_4_2_02.py index d9bb1936..4bc99df4 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_02/M_4_2_02.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_02/M_4_2_02.py @@ -84,7 +84,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -93,7 +93,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -125,7 +125,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_03/M_4_2_03.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_03/M_4_2_03.py index 8517680c..03747046 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_03/M_4_2_03.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_03/M_4_2_03.py @@ -84,7 +84,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -93,7 +93,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -125,7 +125,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_04/M_4_2_04.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_04/M_4_2_04.py index 971a3093..014782dd 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_04/M_4_2_04.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_2_04/M_4_2_04.py @@ -84,7 +84,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -93,7 +93,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -125,7 +125,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_3_01_TOOL/M_4_3_01_TOOL.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_3_01_TOOL/M_4_3_01_TOOL.py index c9428774..2eb71353 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_3_01_TOOL/M_4_3_01_TOOL.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_3_01_TOOL/M_4_3_01_TOOL.py @@ -94,7 +94,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -103,7 +103,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -135,7 +135,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): @@ -471,7 +471,7 @@ def draw_line(points, len_, curves): # vector form previous to next point d2 = Vec2(points[i + 1].x - points[i - 1].x, points[i + 1].y - points[i - 1].y) # shortening of this vector - d2.mult(0.333) + d2 *= 0.333 # how to distribute d2 to the anchors q1 = l1 / (l1 + l2) q2 = l2 / (l1 + l2) diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_4_01_TOOL/M_4_4_01_TOOL.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_4_01_TOOL/M_4_4_01_TOOL.py index b6b8822a..841e1772 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_4_4_01_TOOL/M_4_4_01_TOOL.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_4_4_01_TOOL/M_4_4_01_TOOL.py @@ -92,7 +92,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -101,7 +101,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -133,7 +133,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): @@ -489,7 +489,7 @@ def draw_line(points, len_, curves): # vector form previous to next point d2 = Vec3(points[i + 1].x - points[i - 1].x, points[i + 1].y - points[i - 1].y, points[i + 1].z - points[i - 1].z) # shortening of this vector - d2.mult(0.333) + d2 *= 0.333 # how to distribute d2 to the anchors q1 = l1 / (l1 + l2) q2 = l2 / (l1 + l2) diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_01/M_6_1_01.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_01/M_6_1_01.py index 3916c6ac..a8baa708 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_01/M_6_1_01.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_01/M_6_1_01.py @@ -81,7 +81,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -90,7 +90,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -122,7 +122,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_02/M_6_1_02.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_02/M_6_1_02.py index d602d387..7c064565 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_02/M_6_1_02.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_02/M_6_1_02.py @@ -83,7 +83,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -92,7 +92,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -124,7 +124,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): @@ -163,17 +163,17 @@ def update(self): # calculate the target position # target = normalize(to - from) * length + from diff = Vec3(self.to_node.x - self.from_node.x, self.to_node.y - self.from_node.y, self.to_node.z - self.from_node.z) - diff = diff.normalize() - diff.mult(self.length) + diff = diff.normalized() + diff *= self.length target = Vec3(self.from_node.x + diff.x, self.from_node.y + diff.y, self.from_node.z + diff.z) force = Vec3(target.x - self.to_node.x, target.y - self.to_node.y, target.z - self.to_node.z) - force.mult(0.5) - force.mult(self.stiffness) - force.mult(1 - self.damping) + force *= 0.5 + force *= self.stiffness + force *= 1 - self.damping - self.to_node.velocity.add(force) - self.from_node.velocity.add(force * -1) + self.to_node.velocity += force + self.from_node.velocity += force * -1 # ------ getters and setters ------ def set_length(self, the_length): diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_03/M_6_1_03.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_03/M_6_1_03.py index b20c8845..42cf7559 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_03/M_6_1_03.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_1_03/M_6_1_03.py @@ -81,7 +81,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -90,7 +90,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -122,7 +122,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): @@ -161,17 +161,17 @@ def update(self): # calculate the target position # target = normalize(to - from) * length + from diff = Vec3(self.to_node.x - self.from_node.x, self.to_node.y - self.from_node.y, self.to_node.z - self.from_node.z) - diff = diff.normalize() - diff.mult(self.length) + diff = diff.normalized() + diff *= self.length target = Vec3(self.from_node.x + diff.x, self.from_node.y + diff.y, self.from_node.z + diff.z) force = Vec3(target.x - self.to_node.x, target.y - self.to_node.y, target.z - self.to_node.z) - force.mult(0.5) - force.mult(self.stiffness) - force.mult(1 - self.damping) + force *= 0.5 + force *= self.stiffness + force *= 1 - self.damping - self.to_node.velocity.add(force) - self.from_node.velocity.add(force * -1) + self.to_node.velocity += force + self.from_node.velocity += force * -1 # ------ getters and setters ------ def set_length(self, the_length): diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_3_01/M_6_3_01.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_3_01/M_6_3_01.py index 11029d4c..0c61e754 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_3_01/M_6_3_01.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_3_01/M_6_3_01.py @@ -98,7 +98,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -107,7 +107,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -139,7 +139,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): @@ -178,17 +178,17 @@ def update(self): # calculate the target position # target = normalize(to - from) * length + from diff = Vec3(self.to_node.x - self.from_node.x, self.to_node.y - self.from_node.y, self.to_node.z - self.from_node.z) - diff = diff.normalize() - diff.mult(self.length) + diff = diff.normalized() + diff *= self.length target = Vec3(self.from_node.x + diff.x, self.from_node.y + diff.y, self.from_node.z + diff.z) force = Vec3(target.x - self.to_node.x, target.y - self.to_node.y, target.z - self.to_node.z) - force.mult(0.5) - force.mult(self.stiffness) - force.mult(1 - self.damping) + force *= 0.5 + force *= self.stiffness + force *= 1 - self.damping - self.to_node.velocity.add(force) - self.from_node.velocity.add(force * -1) + self.to_node.velocity += force + self.from_node.velocity += force * -1 # ------ getters and setters ------ def set_length(self, the_length): @@ -659,7 +659,7 @@ def draw(self): f = pow(1 / (1 - accomplish_per_second), -dt / 1000.0) d = self.target_offset - self.offset - d.mult(f) + d *= f self.offset = self.target_offset - d self.zoom = self.target_zoom - ((self.target_zoom - self.zoom) * f) @@ -703,7 +703,7 @@ def draw_arrow(self, n1, n2): margin2 = n2.diameter / 2.0 + 3 + self.line_weight / 2 if d.mag() > margin1 + margin2: - d = d.normalize() + d = d.normalized() line(n1.x + d.x * margin1, n1.y + d.y * margin1, n2.x - d.x * margin2, n2.y - d.y * margin2) a = atan2(d.y, d.x) diff --git a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_4_01_TOOL/M_6_4_01_TOOL.py b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_4_01_TOOL/M_6_4_01_TOOL.py index fbb1d8d1..59b7bbec 100644 --- a/crates/processing_pyo3/examples/Generative Design/02_M/M_6_4_01_TOOL/M_6_4_01_TOOL.py +++ b/crates/processing_pyo3/examples/Generative Design/02_M/M_6_4_01_TOOL/M_6_4_01_TOOL.py @@ -108,7 +108,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -117,7 +117,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -149,7 +149,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): @@ -188,17 +188,17 @@ def update(self): # calculate the target position # target = normalize(to - from) * length + from diff = Vec3(self.to_node.x - self.from_node.x, self.to_node.y - self.from_node.y, self.to_node.z - self.from_node.z) - diff = diff.normalize() - diff.mult(self.length) + diff = diff.normalized() + diff *= self.length target = Vec3(self.from_node.x + diff.x, self.from_node.y + diff.y, self.from_node.z + diff.z) force = Vec3(target.x - self.to_node.x, target.y - self.to_node.y, target.z - self.to_node.z) - force.mult(0.5) - force.mult(self.stiffness) - force.mult(1 - self.damping) + force *= 0.5 + force *= self.stiffness + force *= 1 - self.damping - self.to_node.velocity.add(force) - self.from_node.velocity.add(force * -1) + self.to_node.velocity += force + self.from_node.velocity += force * -1 # ------ getters and setters ------ def set_length(self, the_length): @@ -947,7 +947,7 @@ def draw(self): f = pow(1 / (1 - accomplish_per_second), -dt / 1000.0) d = self.target_offset - self.offset - d.mult(f) + d *= f self.offset = self.target_offset - d self.zoom = self.target_zoom - ((self.target_zoom - self.zoom) * f) @@ -1005,7 +1005,7 @@ def draw_arrow(self, n1, n2): margin2 = n2.diameter * n2.sfactor / 2.0 + 3 + self.line_weight / 2 if d.mag() > margin1 + margin2: - d = d.normalize() + d = d.normalized() line(n1.sx + d.x * margin1, n1.sy + d.y * margin1, n2.sx - d.x * margin2, n2.sy - d.y * margin2) a = atan2(d.y, d.x) diff --git a/crates/processing_pyo3/examples/Generative Design/03_A/Cover/Cover.py b/crates/processing_pyo3/examples/Generative Design/03_A/Cover/Cover.py index 13383e40..3ea52082 100644 --- a/crates/processing_pyo3/examples/Generative Design/03_A/Cover/Cover.py +++ b/crates/processing_pyo3/examples/Generative Design/03_A/Cover/Cover.py @@ -82,7 +82,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -91,7 +91,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -123,7 +123,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): @@ -162,17 +162,17 @@ def update(self): # calculate the target position # target = normalize(to - from) * length + from diff = Vec3(self.to_node.x - self.from_node.x, self.to_node.y - self.from_node.y, self.to_node.z - self.from_node.z) - diff = diff.normalize() - diff.mult(self.length) + diff = diff.normalized() + diff *= self.length target = Vec3(self.from_node.x + diff.x, self.from_node.y + diff.y, self.from_node.z + diff.z) force = Vec3(target.x - self.to_node.x, target.y - self.to_node.y, target.z - self.to_node.z) - force.mult(0.5) - force.mult(self.stiffness) - force.mult(1 - self.damping) + force *= 0.5 + force *= self.stiffness + force *= 1 - self.damping - self.to_node.velocity.add(force) - self.from_node.velocity.add(force * -1) + self.to_node.velocity += force + self.from_node.velocity += force * -1 # ------ getters and setters ------ def set_length(self, the_length): @@ -293,7 +293,7 @@ def draw(self): dy = pos_y - old_y n = Vec2(-dy, dx) if n.mag() > 0: - n = n.normalize() + n = n.normalized() f = remap(t_all, 0, 1, 1.5, 0.5) * qf / 2 n.x *= f n.y *= f @@ -538,7 +538,7 @@ def setup(): tangent.x = typo_points[typo_prev_index[i]].x - typo_points[typo_next_index[i]].x tangent.y = typo_points[typo_prev_index[i]].y - typo_points[typo_next_index[i]].y if tangent.mag() > 0: - tangent = tangent.normalize() + tangent = tangent.normalized() typo_tangents.append(tangent) typo_normals.append(Vec2(-tangent.y, tangent.x)) diff --git a/crates/processing_pyo3/examples/Generative Design/03_A/Force_Directed_Tags/Force_Directed_Tags.py b/crates/processing_pyo3/examples/Generative Design/03_A/Force_Directed_Tags/Force_Directed_Tags.py index ff22bf4f..0b9f515d 100644 --- a/crates/processing_pyo3/examples/Generative Design/03_A/Force_Directed_Tags/Force_Directed_Tags.py +++ b/crates/processing_pyo3/examples/Generative Design/03_A/Force_Directed_Tags/Force_Directed_Tags.py @@ -89,7 +89,7 @@ def attract(self, the_node): s = pow(d / self.radius, 1 / self.ramp) f = s * 9 * self.strength * (1 / (s + 1) + ((s - 3) / 4)) / d df = Vec3(self.x - the_node.x, self.y - the_node.y, self.z - the_node.z) - df.mult(f) + df *= f the_node.velocity.x += df.x the_node.velocity.y += df.y @@ -98,7 +98,7 @@ def attract(self, the_node): # ------ update positions ------ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): - self.velocity.limit(self.max_velocity) + self.velocity = self.velocity.limited(self.max_velocity) self.p_velocity = self.velocity.copy() @@ -130,7 +130,7 @@ def update(self, the_lock_x=False, the_lock_y=False, the_lock_z=False): self.z = self.max_z - (self.z - self.max_z) self.velocity.z = -self.velocity.z - self.velocity.mult(1 - self.damping) + self.velocity *= 1 - self.damping # ------ getters and setters ------ def set_id(self, the_id): @@ -169,17 +169,17 @@ def update(self): # calculate the target position # target = normalize(to - from) * length + from diff = Vec3(self.to_node.x - self.from_node.x, self.to_node.y - self.from_node.y, self.to_node.z - self.from_node.z) - diff = diff.normalize() - diff.mult(self.length) + diff = diff.normalized() + diff *= self.length target = Vec3(self.from_node.x + diff.x, self.from_node.y + diff.y, self.from_node.z + diff.z) force = Vec3(target.x - self.to_node.x, target.y - self.to_node.y, target.z - self.to_node.z) - force.mult(0.5) - force.mult(self.stiffness) - force.mult(1 - self.damping) + force *= 0.5 + force *= self.stiffness + force *= 1 - self.damping - self.to_node.velocity.add(force) - self.from_node.velocity.add(force * -1) + self.to_node.velocity += force + self.from_node.velocity += force * -1 # ------ getters and setters ------ def set_length(self, the_length): diff --git a/crates/processing_pyo3/examples/Topics/Create Shapes/particle_system_pshape.py b/crates/processing_pyo3/examples/Topics/Create Shapes/particle_system_pshape.py index 7685e633..6b03dfc7 100644 --- a/crates/processing_pyo3/examples/Topics/Create Shapes/particle_system_pshape.py +++ b/crates/processing_pyo3/examples/Topics/Create Shapes/particle_system_pshape.py @@ -99,7 +99,7 @@ def rebirth(self, x, y): speed = random(0.5, 4) # A velocity with random angle and magnitude self.velocity = Vec2.from_angle(a) - self.velocity.mult(speed) + self.velocity *= speed # Set lifespan self.lifespan = 255 # Set location using translate @@ -107,7 +107,7 @@ def rebirth(self, x, y): self.part.translate(x, y) # GAP: Shape.translate (shape-local transform) # Update center vector - self.center.set(x, y) + self.center = Vec2(x, y) # Is it off the screen, or its lifespan is over? def is_dead(self): @@ -120,12 +120,12 @@ def update(self): # Decrease life self.lifespan = self.lifespan - 1 # Apply gravity - self.velocity.add(self.gravity) + self.velocity += self.gravity self.part.set_tint(color(1.0, self.lifespan / 255)) # GAP: Shape.set_tint(color) # Move the particle according to its velocity self.part.translate(self.velocity.x, self.velocity.y) # and also update the center - self.center.add(self.velocity) + self.center += self.velocity run() diff --git a/crates/processing_pyo3/examples/Topics/Curves/arc_length_parametrization.py b/crates/processing_pyo3/examples/Topics/Curves/arc_length_parametrization.py index 589f2e8d..383deaf8 100644 --- a/crates/processing_pyo3/examples/Topics/Curves/arc_length_parametrization.py +++ b/crates/processing_pyo3/examples/Topics/Curves/arc_length_parametrization.py @@ -182,7 +182,7 @@ def __init__(self, a, b, c, d): arc_length = 0 prev = Vec2() - prev.set(self.v0) + prev = self.v0.copy() # i goes from 0 to SEGMENT_COUNT for i in range(self.SEGMENT_COUNT + 1): @@ -203,7 +203,7 @@ def __init__(self, a, b, c, d): self.arc_lengths[i] = arc_length # keep this point to compute length of next segment - prev.set(point) + prev = point.copy() # Here we have sum of all segment lengths, which should be # very close to the actual length of the curve. The more diff --git a/crates/processing_pyo3/examples/Topics/Fractals and L-Systems/koch.py b/crates/processing_pyo3/examples/Topics/Fractals and L-Systems/koch.py index 1558212b..01631fc6 100644 --- a/crates/processing_pyo3/examples/Topics/Fractals and L-Systems/koch.py +++ b/crates/processing_pyo3/examples/Topics/Fractals and L-Systems/koch.py @@ -108,28 +108,28 @@ def end(self): # This is easy, just 1/3 of the way def kochleft(self): v = self.b - self.a - v.div(3) - v.add(self.a) + v /= 3 + v += self.a return v # More complicated, have to use a little trig to figure out where this Vec2 is! def kochmiddle(self): v = self.b - self.a - v.div(3) + v /= 3 p = self.a.copy() - p.add(v) + p += v - v = v.rotate(-radians(60)) - p.add(v) + v = v.rotated(-radians(60)) + p += v return p # Easy, just 2/3 of the way def kochright(self): v = self.a - self.b - v.div(3) - v.add(self.b) + v /= 3 + v += self.b return v diff --git a/crates/processing_pyo3/examples/Topics/Geometry/icosahedra.py b/crates/processing_pyo3/examples/Topics/Geometry/icosahedra.py index aa3b448f..263ce6f2 100644 --- a/crates/processing_pyo3/examples/Topics/Geometry/icosahedra.py +++ b/crates/processing_pyo3/examples/Topics/Geometry/icosahedra.py @@ -149,7 +149,7 @@ def build(self): # one flat-shaded triangle; lit geometry needs a normal, pointing outwards def face(self, a, b, c): n = (b - a).cross(c - a) - n.normalize() + n = n.normalized() if n.dot(a) < 0: n = n * -1 self.geometry.normal(n.x, n.y, n.z) diff --git a/crates/processing_pyo3/examples/Topics/Geometry/shape_transform.py b/crates/processing_pyo3/examples/Topics/Geometry/shape_transform.py index b13a83dd..889e45a4 100644 --- a/crates/processing_pyo3/examples/Topics/Geometry/shape_transform.py +++ b/crates/processing_pyo3/examples/Topics/Geometry/shape_transform.py @@ -87,7 +87,7 @@ def build_shape(): # Adds a flat-shaded triangle: lit geometry needs a normal per vertex def add_triangle(g, a, b, c): n = (b - a).cross(c - a) - n.normalize() + n = n.normalized() g.normal(n.x, n.y, n.z) g.vertex(a.x, a.y, a.z) g.vertex(b.x, b.y, b.z) diff --git a/crates/processing_pyo3/examples/Topics/Geometry/space_junk.py b/crates/processing_pyo3/examples/Topics/Geometry/space_junk.py index aa35927d..a6554a65 100644 --- a/crates/processing_pyo3/examples/Topics/Geometry/space_junk.py +++ b/crates/processing_pyo3/examples/Topics/Geometry/space_junk.py @@ -124,7 +124,7 @@ def build(self): def face(self, g, a, b, c, d): center = Vec3(self.shift_x + self.w / 4, self.shift_y + self.h / 4, self.shift_z + self.d / 4) n = (b - a).cross(c - a) - n.normalize() + n = n.normalized() if n.dot(a - center) < 0: n = n * -1 g.normal(n.x, n.y, n.z) diff --git a/crates/processing_pyo3/examples/Topics/Geometry/toroid.py b/crates/processing_pyo3/examples/Topics/Geometry/toroid.py index d87002b5..5018cf96 100644 --- a/crates/processing_pyo3/examples/Topics/Geometry/toroid.py +++ b/crates/processing_pyo3/examples/Topics/Geometry/toroid.py @@ -118,7 +118,7 @@ def build_toroid(): # Adds a flat-shaded triangle: lit geometry needs a normal per vertex def add_triangle(g, a, b, c): n = (b - a).cross(c - a) - n.normalize() + n = n.normalized() g.normal(n.x, n.y, n.z) g.vertex(a.x, a.y, a.z) g.vertex(b.x, b.y, b.z) diff --git a/crates/processing_pyo3/examples/Topics/Geometry/vertices.py b/crates/processing_pyo3/examples/Topics/Geometry/vertices.py index 27b97039..6e28349e 100644 --- a/crates/processing_pyo3/examples/Topics/Geometry/vertices.py +++ b/crates/processing_pyo3/examples/Topics/Geometry/vertices.py @@ -34,7 +34,7 @@ def draw(): # Adds a flat-shaded triangle: lit geometry needs a normal per vertex def add_triangle(g, a, b, c): n = (b - a).cross(c - a) - n.normalize() + n = n.normalized() g.normal(n.x, n.y, n.z) g.vertex(a.x, a.y, a.z) g.vertex(b.x, b.y, b.z) diff --git a/crates/processing_pyo3/examples/Topics/Motion/circle_collision.py b/crates/processing_pyo3/examples/Topics/Motion/circle_collision.py index 3f0623b2..2538a53d 100644 --- a/crates/processing_pyo3/examples/Topics/Motion/circle_collision.py +++ b/crates/processing_pyo3/examples/Topics/Motion/circle_collision.py @@ -29,12 +29,12 @@ class Ball: def __init__(self, x, y, r_): self.position = Vec2(x, y) self.velocity = Vec2.random() - self.velocity.mult(3) + self.velocity *= 3 self.radius = r_ self.m = self.radius * 0.1 def update(self): - self.position.add(self.velocity) + self.position += self.velocity def check_boundary_collision(self): if self.position.x > width - self.radius: @@ -63,10 +63,10 @@ def check_collision(self, other): if distance_vect_mag < min_distance: distance_correction = (min_distance - distance_vect_mag) / 2.0 d = distance_vect.copy() - correction_vector = d.normalize() - correction_vector.mult(distance_correction) - other.position.add(correction_vector) - self.position.sub(correction_vector) + correction_vector = d.normalized() + correction_vector *= distance_correction + other.position += correction_vector + self.position -= correction_vector # get angle of distance_vect theta = distance_vect.heading() @@ -125,7 +125,7 @@ def check_collision(self, other): other.position.x = self.position.x + b_final[1].x other.position.y = self.position.y + b_final[1].y - self.position.add(b_final[0]) + self.position += b_final[0] # update velocities self.velocity.x = cosine * v_final[0].x - sine * v_final[0].y diff --git a/crates/processing_pyo3/examples/Topics/Motion/cubes_within_cube.py b/crates/processing_pyo3/examples/Topics/Motion/cubes_within_cube.py index 79faef79..adf3adc1 100644 --- a/crates/processing_pyo3/examples/Topics/Motion/cubes_within_cube.py +++ b/crates/processing_pyo3/examples/Topics/Motion/cubes_within_cube.py @@ -104,7 +104,7 @@ def __init__(self, w, h, d): a = self.vertices[4 * i] b = self.vertices[4 * i + 1] c = self.vertices[4 * i + 2] - n = (b - a).cross(c - a).normalize() + n = (b - a).cross(c - a).normalized() g = create_geometry(topology=TRIANGLES) for j in range(4): v = self.vertices[j + 4 * i] @@ -122,7 +122,7 @@ def draw_cube(self): # Update location def update(self): - self.position.add(self.velocity) + self.position += self.velocity # Check wall collisions if self.position.x > bounds / 2 or self.position.x < -bounds / 2: diff --git a/crates/processing_pyo3/examples/Topics/Motion/morph.py b/crates/processing_pyo3/examples/Topics/Motion/morph.py index 81396461..a9f4f525 100644 --- a/crates/processing_pyo3/examples/Topics/Motion/morph.py +++ b/crates/processing_pyo3/examples/Topics/Motion/morph.py @@ -26,7 +26,7 @@ def setup(): # Note we are not starting from 0 in order to match the # path of a circle. v = Vec2.from_angle(radians(angle - 135)) - v.mult(100) + v *= 100 circle.append(v) # Let's fill out morph list with blank vectors while we are at it morph.append(Vec2()) @@ -63,7 +63,8 @@ def draw(): # Get the vertex we will draw v2 = morph[i] # Lerp to the target - v2.set(v2.lerp(v1, 0.1)) + v2 = v2.lerp(v1, 0.1) + morph[i] = v2 # Check how far we are from target total_distance += v1.dist(v2) diff --git a/crates/processing_pyo3/examples/Topics/Motion/reflection_1.py b/crates/processing_pyo3/examples/Topics/Motion/reflection_1.py index f7793163..deb6eeac 100644 --- a/crates/processing_pyo3/examples/Topics/Motion/reflection_1.py +++ b/crates/processing_pyo3/examples/Topics/Motion/reflection_1.py @@ -37,11 +37,12 @@ def setup(): # calculate initial random velocity velocity = Vec2.random() - velocity.mult(speed) + velocity *= speed def draw(): # draw background + global position, velocity fill(0.0, 0.05) no_stroke() rect(0, 0, width, height) @@ -51,7 +52,7 @@ def draw(): quad(base1.x, base1.y, base2.x, base2.y, base2.x, height, 0, height) # calculate base top normal - base_delta = (base2 - base1).normalize() + base_delta = (base2 - base1).normalized() normal = Vec2(-base_delta.y, base_delta.x) # draw ellipse @@ -60,10 +61,10 @@ def draw(): ellipse(position.x, position.y, r * 2, r * 2) # move ellipse - position.add(velocity) + position += velocity # normalized incidence vector - incidence = (velocity * -1).normalize() + incidence = (velocity * -1).normalized() # detect and handle collision for i in range(len(coords)): @@ -74,8 +75,8 @@ def draw(): # calculate reflection vector # assign reflection vector to direction vector - velocity.set(2 * normal.x * dot - incidence.x, 2 * normal.y * dot - incidence.y) - velocity.mult(speed) + velocity = Vec2(2 * normal.x * dot - incidence.x, 2 * normal.y * dot - incidence.y) + velocity *= speed # draw base top normal at collision point stroke(1.0, 0.5, 0.0) diff --git a/crates/processing_pyo3/examples/Topics/Motion/reflection_2.py b/crates/processing_pyo3/examples/Topics/Motion/reflection_2.py index dfd508c8..3dd6f136 100644 --- a/crates/processing_pyo3/examples/Topics/Motion/reflection_2.py +++ b/crates/processing_pyo3/examples/Topics/Motion/reflection_2.py @@ -81,8 +81,8 @@ def __init__(self, x, y, r_): def move(self): # Move orb - self.velocity.add(gravity) - self.position.add(self.velocity) + self.velocity += gravity + self.position += self.velocity def display(self): # Draw orb diff --git a/crates/processing_pyo3/examples/Topics/Simulate/flocking.py b/crates/processing_pyo3/examples/Topics/Simulate/flocking.py index 33e97d9b..289bee96 100644 --- a/crates/processing_pyo3/examples/Topics/Simulate/flocking.py +++ b/crates/processing_pyo3/examples/Topics/Simulate/flocking.py @@ -48,7 +48,7 @@ def run(self, boids): def apply_force(self, force): # We could add mass here if we want A = F / M - self.acceleration.add(force) + self.acceleration += force # We accumulate a new acceleration each time based on three rules def flock(self, boids): @@ -56,9 +56,9 @@ def flock(self, boids): ali = self.align(boids) # Alignment coh = self.cohesion(boids) # Cohesion # Arbitrarily weight these forces - sep.mult(1.5) - ali.mult(1.0) - coh.mult(1.0) + sep *= 1.5 + ali *= 1.0 + coh *= 1.0 # Add the force vectors to acceleration self.apply_force(sep) self.apply_force(ali) @@ -67,23 +67,23 @@ def flock(self, boids): # Method to update position def update(self): # Update velocity - self.velocity.add(self.acceleration) + self.velocity += self.acceleration # Limit speed - self.velocity.limit(self.maxspeed) - self.position.add(self.velocity) + self.velocity = self.velocity.limited(self.maxspeed) + self.position += self.velocity # Reset acceleration to 0 each cycle - self.acceleration.mult(0) + self.acceleration *= 0 # A method that calculates and applies a steering force towards a target # STEER = DESIRED MINUS VELOCITY def seek(self, target): desired = target - self.position # A vector pointing from the position to the target # Scale to maximum speed - desired.set_mag(self.maxspeed) + desired = desired.with_mag(self.maxspeed) # Steering = Desired minus Velocity steer = desired - self.velocity - steer.limit(self.maxforce) # Limit to maximum steering force + steer = steer.limited(self.maxforce) # Limit to maximum steering force return steer def render(self): @@ -126,20 +126,20 @@ def separate(self, boids): if d > 0 and d < desiredseparation: # Calculate vector pointing away from neighbor diff = self.position - other.position - diff = diff.normalize() - diff.div(d) # Weight by distance - steer.add(diff) + diff = diff.normalized() + diff /= d # Weight by distance + steer += diff count += 1 # Keep track of how many # Average -- divide by how many if count > 0: - steer.div(count) + steer /= count # As long as the vector is greater than 0 if steer.mag() > 0: # Implement Reynolds: Steering = Desired - Velocity - steer.set_mag(self.maxspeed) - steer.sub(self.velocity) - steer.limit(self.maxforce) + steer = steer.with_mag(self.maxspeed) + steer -= self.velocity + steer = steer.limited(self.maxforce) return steer # Alignment @@ -151,14 +151,14 @@ def align(self, boids): for other in boids: d = self.position.dist(other.position) if d > 0 and d < neighbordist: - sum.add(other.velocity) + sum += other.velocity count += 1 if count > 0: - sum.div(count) + sum /= count # Implement Reynolds: Steering = Desired - Velocity - sum.set_mag(self.maxspeed) + sum = sum.with_mag(self.maxspeed) steer = sum - self.velocity - steer.limit(self.maxforce) + steer = steer.limited(self.maxforce) return steer else: return Vec2(0, 0) @@ -172,10 +172,10 @@ def cohesion(self, boids): for other in boids: d = self.position.dist(other.position) if d > 0 and d < neighbordist: - sum.add(other.position) # Add position + sum += other.position # Add position count += 1 if count > 0: - sum.div(count) + sum /= count return self.seek(sum) # Steer towards the position else: return Vec2(0, 0) diff --git a/crates/processing_pyo3/examples/Topics/Simulate/forces_with_vectors.py b/crates/processing_pyo3/examples/Topics/Simulate/forces_with_vectors.py index 92ce5292..9851932c 100644 --- a/crates/processing_pyo3/examples/Topics/Simulate/forces_with_vectors.py +++ b/crates/processing_pyo3/examples/Topics/Simulate/forces_with_vectors.py @@ -86,10 +86,10 @@ def drag(self, m): # Direction is inverse of velocity drag = m.velocity.copy() - drag.mult(-1) + drag *= -1 # Scale according to magnitude - drag.set_mag(drag_magnitude) + drag = drag.with_mag(drag_magnitude) return drag def display(self): @@ -112,15 +112,15 @@ def apply_force(self, force): # Divide by mass f = force / self.mass # Accumulate all forces in acceleration - self.acceleration.add(f) + self.acceleration += f def update(self): # Velocity changes according to acceleration - self.velocity.add(self.acceleration) + self.velocity += self.acceleration # position changes by velocity - self.position.add(self.velocity) + self.position += self.velocity # We must clear acceleration each frame - self.acceleration.mult(0) + self.acceleration *= 0 # Draw Mover def display(self): diff --git a/crates/processing_pyo3/examples/Topics/Simulate/gravitational_attraction_3d.py b/crates/processing_pyo3/examples/Topics/Simulate/gravitational_attraction_3d.py index 0ac63aa4..2678de6e 100644 --- a/crates/processing_pyo3/examples/Topics/Simulate/gravitational_attraction_3d.py +++ b/crates/processing_pyo3/examples/Topics/Simulate/gravitational_attraction_3d.py @@ -69,13 +69,13 @@ def __init__(self, m, x, y, z): # Newton's 2nd Law (F = M*A) applied def apply_force(self, force): f = force / self.mass - self.acceleration.add(f) + self.acceleration += f # Our motion algorithm (aka Euler Integration) def update(self): - self.velocity.add(self.acceleration) # Velocity changes according to acceleration - self.position.add(self.velocity) # position changes according to velocity - self.acceleration.mult(0) + self.velocity += self.acceleration # Velocity changes according to acceleration + self.position += self.velocity # position changes according to velocity + self.acceleration *= 0 # Draw the Planet def display(self): @@ -99,7 +99,7 @@ def attract(self, m): d = force.mag() # Distance between objects d = constrain(d, 5.0, 25.0) # Limiting the distance to eliminate "extreme" results for very close or very far objects strength = (self.G * self.mass * m.mass) / (d * d) # Calculate gravitional force magnitude - force.set_mag(strength) # Get force vector --> magnitude * direction + force = force.with_mag(strength) # Get force vector --> magnitude * direction return force # Draw Sun diff --git a/crates/processing_pyo3/examples/Topics/Simulate/multiple_particle_systems.py b/crates/processing_pyo3/examples/Topics/Simulate/multiple_particle_systems.py index c887f736..a8813201 100644 --- a/crates/processing_pyo3/examples/Topics/Simulate/multiple_particle_systems.py +++ b/crates/processing_pyo3/examples/Topics/Simulate/multiple_particle_systems.py @@ -45,8 +45,8 @@ def run(self): # Method to update position def update(self): - self.velocity.add(self.acceleration) - self.position.add(self.velocity) + self.velocity += self.acceleration + self.position += self.velocity self.lifespan -= 2.0 # Method to display diff --git a/crates/processing_pyo3/examples/Topics/Simulate/simple_particle_system.py b/crates/processing_pyo3/examples/Topics/Simulate/simple_particle_system.py index 3510d033..773fb235 100644 --- a/crates/processing_pyo3/examples/Topics/Simulate/simple_particle_system.py +++ b/crates/processing_pyo3/examples/Topics/Simulate/simple_particle_system.py @@ -36,8 +36,8 @@ def run(self): # Method to update position def update(self): - self.velocity.add(self.acceleration) - self.position.add(self.velocity) + self.velocity += self.acceleration + self.position += self.velocity self.lifespan -= 1.0 # Method to display diff --git a/crates/processing_pyo3/examples/Topics/Simulate/smoke_particle_system.py b/crates/processing_pyo3/examples/Topics/Simulate/smoke_particle_system.py index 05e13ad9..302e65b8 100644 --- a/crates/processing_pyo3/examples/Topics/Simulate/smoke_particle_system.py +++ b/crates/processing_pyo3/examples/Topics/Simulate/smoke_particle_system.py @@ -66,14 +66,14 @@ def run(self): # Method to apply a force vector to the Particle object # Note we are ignoring "mass" here def apply_force(self, f): - self.acc.add(f) + self.acc += f # Method to update position def update(self): - self.vel.add(self.acc) - self.loc.add(self.vel) + self.vel += self.acc + self.loc += self.vel self.lifespan -= 2.5 - self.acc.mult(0) # clear Acceleration + self.acc *= 0 # clear Acceleration # Method to display def render(self): diff --git a/crates/processing_pyo3/examples/Topics/Vectors/acceleration_with_vectors.py b/crates/processing_pyo3/examples/Topics/Vectors/acceleration_with_vectors.py index 7f511874..5ba5b53a 100644 --- a/crates/processing_pyo3/examples/Topics/Vectors/acceleration_with_vectors.py +++ b/crates/processing_pyo3/examples/Topics/Vectors/acceleration_with_vectors.py @@ -39,14 +39,14 @@ def update(self): mouse = Vec2(mouse_x, mouse_y) acceleration = mouse - self.location # Set magnitude of acceleration - acceleration.set_mag(0.2) + acceleration = acceleration.with_mag(0.2) # Velocity changes according to acceleration - self.velocity.add(acceleration) + self.velocity += acceleration # Limit the velocity by topspeed - self.velocity.limit(self.topspeed) + self.velocity = self.velocity.limited(self.topspeed) # Location changes by velocity - self.location.add(self.velocity) + self.location += self.velocity def display(self): stroke(1.0) diff --git a/crates/processing_pyo3/examples/Topics/Vectors/bouncing_ball.py b/crates/processing_pyo3/examples/Topics/Vectors/bouncing_ball.py index 803ac8f3..f854a2ec 100644 --- a/crates/processing_pyo3/examples/Topics/Vectors/bouncing_ball.py +++ b/crates/processing_pyo3/examples/Topics/Vectors/bouncing_ball.py @@ -21,12 +21,13 @@ def setup(): def draw(): + global location, velocity background(0.0) # Add velocity to the location. - location.add(velocity) + location += velocity # Add gravity to velocity - velocity.add(gravity) + velocity += gravity # Bounce off edges if location.x > width or location.x < 0: diff --git a/crates/processing_pyo3/examples/Topics/Vectors/vector_math.py b/crates/processing_pyo3/examples/Topics/Vectors/vector_math.py index 5cb29ee1..bbd78d9a 100644 --- a/crates/processing_pyo3/examples/Topics/Vectors/vector_math.py +++ b/crates/processing_pyo3/examples/Topics/Vectors/vector_math.py @@ -22,13 +22,13 @@ def draw(): # Subtract center from mouse which results in a # vector that points from center to mouse - mouse.sub(center) + mouse -= center # Normalize the vector - mouse = mouse.normalize() + mouse = mouse.normalized() # Multiply its length by 150 (Scaling its length) - mouse.mult(150) + mouse *= 150 translate(width / 2, height / 2) # Draw the resulting vector diff --git a/crates/processing_pyo3/examples/flocking.py b/crates/processing_pyo3/examples/flocking.py index cbc926dd..04a12ad3 100644 --- a/crates/processing_pyo3/examples/flocking.py +++ b/crates/processing_pyo3/examples/flocking.py @@ -74,7 +74,7 @@ def run(self, boids): def apply_force(self, force): # We could add mass here if we want A = F / M - self.acceleration.add(force) + self.acceleration += force # We accumulate a new acceleration each time based on three rules def flock(self, boids): @@ -82,9 +82,9 @@ def flock(self, boids): ali = self.align(boids) # Alignment coh = self.cohesion(boids) # Cohesion # Arbitrarily weight these forces - sep.mult(1.5) - ali.mult(1.0) - coh.mult(1.0) + sep *= 1.5 + ali *= 1.0 + coh *= 1.0 # Add the force vectors to acceleration self.apply_force(sep) self.apply_force(ali) @@ -93,23 +93,23 @@ def flock(self, boids): # Method to update position def update(self): # Update velocity - self.velocity.add(self.acceleration) + self.velocity += self.acceleration # Limit speed - self.velocity.limit(self.maxspeed) - self.position.add(self.velocity) + self.velocity = self.velocity.limited(self.maxspeed) + self.position += self.velocity # Reset acceleration to 0 each cycle - self.acceleration.mult(0) + self.acceleration *= 0 # A method that calculates and applies a steering force towards a target # STEER = DESIRED MINUS VELOCITY def seek(self, target): desired = target - self.position # A vector pointing from the position to the target # Scale to maximum speed - desired.set_mag(self.maxspeed) + desired = desired.with_mag(self.maxspeed) # Steering = Desired minus Velocity steer = desired - self.velocity - steer.limit(self.maxforce) # Limit to maximum steering force + steer = steer.limited(self.maxforce) # Limit to maximum steering force return steer def render(self): @@ -151,20 +151,20 @@ def separate(self, boids): # If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) if 0 < d < desired_separation: # Calculate vector pointing away from neighbor - diff = (self.position - other.position).normalize() - diff.div(d) # Weight by distance - steer.add(diff) + diff = (self.position - other.position).normalized() + diff /= d # Weight by distance + steer += diff count += 1 # Keep track of how many # Average -- divide by how many if count > 0: - steer.div(count) + steer /= count # As long as the vector is greater than 0 if steer.mag() > 0: # Implement Reynolds: Steering = Desired - Velocity - steer.set_mag(self.maxspeed) - steer.sub(self.velocity) - steer.limit(self.maxforce) + steer = steer.with_mag(self.maxspeed) + steer -= self.velocity + steer = steer.limited(self.maxforce) return steer # Alignment @@ -176,14 +176,14 @@ def align(self, boids): for other in boids: d = self.position.dist(other.position) if 0 < d < neighbor_dist: - sum.add(other.velocity) + sum += other.velocity count += 1 if count > 0: - sum.div(count) + sum /= count # Implement Reynolds: Steering = Desired - Velocity - sum.set_mag(self.maxspeed) + sum = sum.with_mag(self.maxspeed) steer = sum - self.velocity - steer.limit(self.maxforce) + steer = steer.limited(self.maxforce) return steer else: return Vec2(0, 0) @@ -197,10 +197,10 @@ def cohesion(self, boids): for other in boids: d = self.position.dist(other.position) if 0 < d < neighbor_dist: - sum.add(other.position) # Add position + sum += other.position # Add position count += 1 if count > 0: - sum.div(count) + sum /= count return self.seek(sum) # Steer towards the position else: return Vec2(0, 0) diff --git a/crates/processing_pyo3/src/color.rs b/crates/processing_pyo3/src/color.rs index baad62b0..112340d8 100644 --- a/crates/processing_pyo3/src/color.rs +++ b/crates/processing_pyo3/src/color.rs @@ -4,7 +4,7 @@ use bevy::color::{ }; use pyo3::{exceptions::PyTypeError, prelude::*, types::PyTuple}; -use crate::math::{PyVec3, PyVec4, PyVecIter, hash_f32}; +use crate::math::{PyVec3, PyVec4, PyVecIter}; pub use processing::prelude::color::{ColorMode, ColorSpace}; @@ -413,17 +413,6 @@ impl PyColor { self.0.to_srgba() == other.0.to_srgba() } - fn __hash__(&self) -> u64 { - // Hash in sRGBA so equal colors hash the same regardless of space - let s = self.0.to_srgba(); - let mut hasher = std::collections::hash_map::DefaultHasher::new(); - hash_f32(s.red, &mut hasher); - hash_f32(s.green, &mut hasher); - hash_f32(s.blue, &mut hasher); - hash_f32(s.alpha, &mut hasher); - std::hash::Hasher::finish(&hasher) - } - fn __len__(&self) -> usize { 4 } diff --git a/crates/processing_pyo3/src/math.rs b/crates/processing_pyo3/src/math.rs index 3e788bf5..6b4f3140 100644 --- a/crates/processing_pyo3/src/math.rs +++ b/crates/processing_pyo3/src/math.rs @@ -1,5 +1,4 @@ use std::cell::RefCell; -use std::hash::{Hash, Hasher}; use bevy::math::{Affine2, EulerRot, Mat2, Quat, Vec2, Vec3, Vec4}; use pyo3::{ @@ -22,14 +21,6 @@ fn sample(dist: impl Distribution) -> T { }) } -pub fn hash_f32(val: f32, state: &mut impl Hasher) { - if val == 0.0 { - 0.0f32.to_bits().hash(state); - } else { - val.to_bits().hash(state); - } -} - #[derive(FromPyObject)] pub(crate) enum Vec2Arg { Instance((PyVec2,)), @@ -93,10 +84,11 @@ pub(crate) fn extract_vec3(args: &Bound<'_, PyTuple>) -> PyResult { macro_rules! impl_py_vec { ( $name:ident, $py_name:literal, $n:literal, - [$(($field:ident, $set_field:ident, $idx:literal)),+], + [$(($field:ident, $set_field:ident, $with_field:ident, $idx:literal)),+], $glam_ty:ty $(, extra { $($extra:tt)* })? ) => { + // methods never change the vector; only assigning to a component does #[pyclass(name = $py_name, from_py_object)] #[derive(Clone, Debug)] pub struct $name(pub(crate) $glam_ty); @@ -152,15 +144,19 @@ macro_rules! impl_py_vec { #[setter] fn $set_field(&mut self, val: f32) { self.0[$idx] = val; } + + fn $with_field(&self, val: f32) -> Self { + let mut v = self.0; + v[$idx] = val; + Self(v) + } )+ fn __add__(&self, other: &Self) -> Self { Self(self.0 + other.0) } fn __radd__(&self, other: &Self) -> Self { Self(other.0 + self.0) } - fn __iadd__(&mut self, other: &Self) { self.0 += other.0; } fn __sub__(&self, other: &Self) -> Self { Self(self.0 - other.0) } fn __rsub__(&self, other: &Self) -> Self { Self(other.0 - self.0) } - fn __isub__(&mut self, other: &Self) { self.0 -= other.0; } fn __neg__(&self) -> Self { Self(-self.0) } @@ -191,21 +187,6 @@ macro_rules! impl_py_vec { ))) } - fn __imul__(&mut self, rhs: &Bound<'_, PyAny>) -> PyResult<()> { - if let Ok(s) = rhs.extract::() { - self.0 *= s; return Ok(()); - } - if let Ok(s) = rhs.extract::() { - self.0 *= s as f32; return Ok(()); - } - if let Ok(other) = rhs.extract::>() { - self.0 *= other.0; return Ok(()); - } - Err(PyTypeError::new_err(concat!( - "unsupported operand type(s) for *=: '", $py_name, "'" - ))) - } - fn __truediv__(&self, rhs: &Bound<'_, PyAny>) -> PyResult { if let Ok(s) = rhs.extract::() { return Ok(Self(self.0 / s)); @@ -233,31 +214,8 @@ macro_rules! impl_py_vec { ))) } - fn __itruediv__(&mut self, rhs: &Bound<'_, PyAny>) -> PyResult<()> { - if let Ok(s) = rhs.extract::() { - self.0 /= s; return Ok(()); - } - if let Ok(s) = rhs.extract::() { - self.0 /= s as f32; return Ok(()); - } - if let Ok(other) = rhs.extract::>() { - self.0 /= other.0; return Ok(()); - } - Err(PyTypeError::new_err(concat!( - "unsupported operand type(s) for /=: '", $py_name, "'" - ))) - } - fn __eq__(&self, other: &Self) -> bool { self.0 == other.0 } - fn __hash__(&self) -> u64 { - let mut hasher = std::collections::hash_map::DefaultHasher::new(); - for &c in self.0.to_array().iter() { - hash_f32(c, &mut hasher); - } - std::hash::Hasher::finish(&hasher) - } - fn __repr__(&self) -> String { let parts: Vec = self.0.to_array().iter().map(|c| format!("{c}")).collect(); format!("{}({})", $py_name, parts.join(", ")) @@ -337,7 +295,8 @@ macro_rules! impl_py_vec { fn length(&self) -> f32 { self.0.length() } fn length_squared(&self) -> f32 { self.0.length_squared() } - fn normalize(&self) -> Self { Self(self.0.normalize()) } + /// The zero vector stays zero. + fn normalized(&self) -> Self { Self(self.0.normalize_or_zero()) } fn dot(&self, other: &Self) -> f32 { self.0.dot(other.0) } fn distance(&self, other: &Self) -> f32 { self.0.distance(other.0) } fn lerp(&self, other: &Self, t: f32) -> Self { Self(self.0.lerp(other.0, t)) } @@ -364,122 +323,17 @@ macro_rules! impl_py_vec { fn equals(&self, other: &Self) -> bool { self.0 == other.0 } - fn hash_code(&self) -> u64 { - let mut hasher = std::collections::hash_map::DefaultHasher::new(); - for &c in self.0.to_array().iter() { - hash_f32(c, &mut hasher); - } - std::hash::Hasher::finish(&hasher) - } - - #[pyo3(signature = (*args))] - fn set(&mut self, args: &Bound<'_, PyTuple>) -> PyResult<()> { - match args.len() { - 1 => { - let first = args.get_item(0)?; - if let Ok(s) = first.extract::() { - self.0 = <$glam_ty>::splat(s); - return Ok(()); - } - if let Ok(s) = first.extract::() { - self.0 = <$glam_ty>::splat(s as f32); - return Ok(()); - } - if let Ok(v) = first.extract::>() { - self.0 = v.0; - return Ok(()); - } - if let Ok(arr) = first.extract::<[f32; $n]>() { - self.0 = <$glam_ty>::from_array(arr); - return Ok(()); - } - Err(PyTypeError::new_err(concat!( - "expected scalar, ", $py_name, ", or sequence of ", - stringify!($n), " floats" - ))) - } - $n => { - let mut arr = [0.0f32; $n]; - $(arr[$idx] = args.get_item($idx)?.extract::()?;)+ - self.0 = <$glam_ty>::from_array(arr); - Ok(()) - } - _ => Err(PyTypeError::new_err(concat!( - $py_name, ".set takes 1 or ", stringify!($n), " arguments" - ))), - } + /// The same direction with length `len`. + fn with_mag(&self, len: f32) -> Self { + Self(self.0.normalize_or_zero() * len) } - #[pyo3(signature = (*args))] - fn add(&mut self, args: &Bound<'_, PyTuple>) -> PyResult<()> { - match args.len() { - 1 => { - let first = args.get_item(0)?; - if let Ok(v) = first.extract::>() { - self.0 += v.0; - return Ok(()); - } - if let Ok(arr) = first.extract::<[f32; $n]>() { - self.0 += <$glam_ty>::from_array(arr); - return Ok(()); - } - Err(PyTypeError::new_err(concat!( - "expected ", $py_name, " or sequence of ", - stringify!($n), " floats" - ))) - } - $n => { - let mut arr = [0.0f32; $n]; - $(arr[$idx] = args.get_item($idx)?.extract::()?;)+ - self.0 += <$glam_ty>::from_array(arr); - Ok(()) - } - _ => Err(PyTypeError::new_err(concat!( - $py_name, ".add takes 1 or ", stringify!($n), " arguments" - ))), - } - } - - #[pyo3(signature = (*args))] - fn sub(&mut self, args: &Bound<'_, PyTuple>) -> PyResult<()> { - match args.len() { - 1 => { - let first = args.get_item(0)?; - if let Ok(v) = first.extract::>() { - self.0 -= v.0; - return Ok(()); - } - if let Ok(arr) = first.extract::<[f32; $n]>() { - self.0 -= <$glam_ty>::from_array(arr); - return Ok(()); - } - Err(PyTypeError::new_err(concat!( - "expected ", $py_name, " or sequence of ", - stringify!($n), " floats" - ))) - } - $n => { - let mut arr = [0.0f32; $n]; - $(arr[$idx] = args.get_item($idx)?.extract::()?;)+ - self.0 -= <$glam_ty>::from_array(arr); - Ok(()) - } - _ => Err(PyTypeError::new_err(concat!( - $py_name, ".sub takes 1 or ", stringify!($n), " arguments" - ))), - } - } - - fn mult(&mut self, n: f32) { self.0 *= n; } - fn div(&mut self, n: f32) { self.0 /= n; } - - fn set_mag(&mut self, len: f32) { - self.0 = self.0.normalize_or_zero() * len; - } - - fn limit(&mut self, max: f32) { + /// The same vector, shortened to `max` if it is longer. + fn limited(&self, max: f32) -> Self { if self.0.length_squared() > max * max { - self.0 = self.0.normalize_or_zero() * max; + Self(self.0.normalize_or_zero() * max) + } else { + Self(self.0) } } @@ -499,7 +353,7 @@ macro_rules! impl_py_vec { }; } -impl_py_vec!(PyVec2, "Vec2", 2, [(x, set_x, 0), (y, set_y, 1)], Vec2, extra { +impl_py_vec!(PyVec2, "Vec2", 2, [(x, set_x, with_x, 0), (y, set_y, with_y, 1)], Vec2, extra { #[classattr] #[allow(non_snake_case)] fn ZERO() -> Self { Self(Vec2::ZERO) } #[classattr] #[allow(non_snake_case)] fn ONE() -> Self { Self(Vec2::ONE) } #[classattr] #[allow(non_snake_case)] fn X() -> Self { Self(Vec2::X) } @@ -513,7 +367,7 @@ impl_py_vec!(PyVec2, "Vec2", 2, [(x, set_x, 0), (y, set_y, 1)], Vec2, extra { self.0.y.atan2(self.0.x) } - fn rotate(&self, angle: f32) -> Self { + fn rotated(&self, angle: f32) -> Self { Self(Vec2::from_angle(angle).rotate(self.0)) } @@ -521,9 +375,10 @@ impl_py_vec!(PyVec2, "Vec2", 2, [(x, set_x, 0), (y, set_y, 1)], Vec2, extra { Self(self.0.perp()) } - fn set_heading(&mut self, angle: f32) { + /// The same length, pointing along `angle`. + fn with_heading(&self, angle: f32) -> Self { let mag = self.0.length(); - self.0 = Vec2::new(angle.cos() * mag, angle.sin() * mag); + Self(Vec2::new(angle.cos() * mag, angle.sin() * mag)) } #[staticmethod] @@ -542,7 +397,7 @@ impl_py_vec!(PyVec2, "Vec2", 2, [(x, set_x, 0), (y, set_y, 1)], Vec2, extra { } }); -impl_py_vec!(PyVec3, "Vec3", 3, [(x, set_x, 0), (y, set_y, 1), (z, set_z, 2)], Vec3, extra { +impl_py_vec!(PyVec3, "Vec3", 3, [(x, set_x, with_x, 0), (y, set_y, with_y, 1), (z, set_z, with_z, 2)], Vec3, extra { #[classattr] #[allow(non_snake_case)] fn ZERO() -> Self { Self(Vec3::ZERO) } #[classattr] #[allow(non_snake_case)] fn ONE() -> Self { Self(Vec3::ONE) } #[classattr] #[allow(non_snake_case)] fn X() -> Self { Self(Vec3::X) } @@ -572,7 +427,7 @@ impl_py_vec!( PyVec4, "Vec4", 4, - [(x, set_x, 0), (y, set_y, 1), (z, set_z, 2), (w, set_w, 3)], + [(x, set_x, with_x, 0), (y, set_y, with_y, 1), (z, set_z, with_z, 2), (w, set_w, with_w, 3)], Vec4, extra { #[classattr] #[allow(non_snake_case)] fn ZERO() -> Self { Self(Vec4::ZERO) } @@ -692,7 +547,7 @@ impl PyQuat { self.0 = Quat::from_xyzw(x, y, z, val); } - fn normalize(&self) -> Self { + fn normalized(&self) -> Self { Self(self.0.normalize()) } fn inverse(&self) -> Self { @@ -740,14 +595,6 @@ impl PyQuat { self.0 == other.0 } - fn __hash__(&self) -> u64 { - let mut hasher = std::collections::hash_map::DefaultHasher::new(); - for &c in self.0.to_array().iter() { - hash_f32(c, &mut hasher); - } - std::hash::Hasher::finish(&hasher) - } - fn __repr__(&self) -> String { format!( "Quat({}, {}, {}, {})", @@ -864,14 +711,6 @@ impl PyMat2 { fn __eq__(&self, other: &Self) -> bool { self.0 == other.0 } - - fn __hash__(&self) -> u64 { - let mut hasher = std::collections::hash_map::DefaultHasher::new(); - for &c in self.0.to_cols_array().iter() { - hash_f32(c, &mut hasher); - } - std::hash::Hasher::finish(&hasher) - } } #[pyclass(name = "Affine2", from_py_object)] @@ -939,16 +778,6 @@ impl PyAffine2 { fn __eq__(&self, other: &Self) -> bool { self.0 == other.0 } - - fn __hash__(&self) -> u64 { - let mut hasher = std::collections::hash_map::DefaultHasher::new(); - for &c in self.0.matrix2.to_cols_array().iter() { - hash_f32(c, &mut hasher); - } - hash_f32(self.0.translation.x, &mut hasher); - hash_f32(self.0.translation.y, &mut hasher); - std::hash::Hasher::finish(&hasher) - } } #[cfg(test)] From 0495a6b41673ec7ac9ed9556a85e45c14acd1560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?charlotte=20=F0=9F=8C=B8?= Date: Tue, 1 Dec 2026 12:47:25 -0800 Subject: [PATCH 2/2] Restore imul, etc. --- crates/processing_pyo3/src/math.rs | 88 +++++++++++++++++++----------- 1 file changed, 57 insertions(+), 31 deletions(-) diff --git a/crates/processing_pyo3/src/math.rs b/crates/processing_pyo3/src/math.rs index 6b4f3140..2cb3894e 100644 --- a/crates/processing_pyo3/src/math.rs +++ b/crates/processing_pyo3/src/math.rs @@ -154,9 +154,11 @@ macro_rules! impl_py_vec { fn __add__(&self, other: &Self) -> Self { Self(self.0 + other.0) } fn __radd__(&self, other: &Self) -> Self { Self(other.0 + self.0) } + fn __iadd__(slf: &Bound<'_, Self>, other: Self) { slf.borrow_mut().0 += other.0; } fn __sub__(&self, other: &Self) -> Self { Self(self.0 - other.0) } fn __rsub__(&self, other: &Self) -> Self { Self(other.0 - self.0) } + fn __isub__(slf: &Bound<'_, Self>, other: Self) { slf.borrow_mut().0 -= other.0; } fn __neg__(&self) -> Self { Self(-self.0) } @@ -202,6 +204,19 @@ macro_rules! impl_py_vec { ))) } + // computed before the mutable borrow so `v *= v` works + fn __imul__(slf: &Bound<'_, Self>, rhs: &Bound<'_, PyAny>) -> PyResult<()> { + let v = slf.borrow().__mul__(rhs)?; + slf.borrow_mut().0 = v.0; + Ok(()) + } + + fn __itruediv__(slf: &Bound<'_, Self>, rhs: &Bound<'_, PyAny>) -> PyResult<()> { + let v = slf.borrow().__truediv__(rhs)?; + slf.borrow_mut().0 = v.0; + Ok(()) + } + fn __rtruediv__(&self, lhs: &Bound<'_, PyAny>) -> PyResult { if let Ok(s) = lhs.extract::() { return Ok(Self(<$glam_ty>::splat(s) / self.0)); @@ -813,9 +828,16 @@ mod tests { #[test] fn test_vec3_normalize() { let v = PyVec3(Vec3::new(3.0, 0.0, 0.0)); - let n = v.normalize(); + let n = v.normalized(); assert!((n.0.length() - 1.0).abs() < 1e-6); assert_eq!(n.0, Vec3::X); + assert_eq!(v.0, Vec3::new(3.0, 0.0, 0.0)); + } + + #[test] + fn test_vec3_normalized_zero_stays_zero() { + let v = PyVec3(Vec3::ZERO); + assert_eq!(v.normalized().0, Vec3::ZERO); } #[test] @@ -899,7 +921,6 @@ mod tests { let a = PyVec3(Vec3::new(1.0, 2.0, 3.0)); let b = a.copy(); assert!(a.equals(&b)); - assert_eq!(a.hash_code(), b.hash_code()); } #[test] @@ -912,37 +933,34 @@ mod tests { } #[test] - fn test_vec3_mult_div() { - let mut v = PyVec3(Vec3::new(1.0, 2.0, 3.0)); - v.mult(2.0); - assert_eq!(v.0, Vec3::new(2.0, 4.0, 6.0)); - v.div(2.0); - assert_eq!(v.0, Vec3::new(1.0, 2.0, 3.0)); + fn test_vec3_with_mag() { + let v = PyVec3(Vec3::new(3.0, 4.0, 0.0)); + let r = v.with_mag(10.0); + assert!((r.0.length() - 10.0).abs() < 1e-5); + assert!((r.0.x - 6.0).abs() < 1e-5); + assert!((r.0.y - 8.0).abs() < 1e-5); + assert_eq!(v.0, Vec3::new(3.0, 4.0, 0.0)); } #[test] - fn test_vec3_set_mag() { - let mut v = PyVec3(Vec3::new(3.0, 4.0, 0.0)); - v.set_mag(10.0); - assert!((v.0.length() - 10.0).abs() < 1e-5); - assert!((v.0.x - 6.0).abs() < 1e-5); - assert!((v.0.y - 8.0).abs() < 1e-5); + fn test_vec3_with_mag_zero_vec_stays_zero() { + let v = PyVec3(Vec3::ZERO); + assert_eq!(v.with_mag(5.0).0, Vec3::ZERO); } #[test] - fn test_vec3_set_mag_zero_vec_stays_zero() { - let mut v = PyVec3(Vec3::ZERO); - v.set_mag(5.0); - assert_eq!(v.0, Vec3::ZERO); + fn test_vec3_limited() { + let v = PyVec3(Vec3::new(3.0, 4.0, 0.0)); + assert_eq!(v.limited(10.0).0, Vec3::new(3.0, 4.0, 0.0)); + assert!((v.limited(2.5).0.length() - 2.5).abs() < 1e-5); + assert_eq!(v.0, Vec3::new(3.0, 4.0, 0.0)); } #[test] - fn test_vec3_limit() { - let mut v = PyVec3(Vec3::new(3.0, 4.0, 0.0)); - v.limit(10.0); - assert_eq!(v.0, Vec3::new(3.0, 4.0, 0.0)); - v.limit(2.5); - assert!((v.0.length() - 2.5).abs() < 1e-5); + fn test_vec3_with_component() { + let v = PyVec3(Vec3::new(1.0, 2.0, 3.0)); + assert_eq!(v.with_y(9.0).0, Vec3::new(1.0, 9.0, 3.0)); + assert_eq!(v.0, Vec3::new(1.0, 2.0, 3.0)); } #[test] @@ -962,13 +980,21 @@ mod tests { } #[test] - fn test_vec2_set_heading_preserves_mag() { - let mut v = PyVec2(Vec2::new(3.0, 4.0)); - let mag_before = v.0.length(); - v.set_heading(0.0); - assert!((v.0.length() - mag_before).abs() < 1e-5); - assert!((v.0.x - mag_before).abs() < 1e-5); - assert!(v.0.y.abs() < 1e-5); + fn test_vec2_with_heading_preserves_mag() { + let v = PyVec2(Vec2::new(3.0, 4.0)); + let r = v.with_heading(0.0); + assert!((r.0.x - 5.0).abs() < 1e-5); + assert!(r.0.y.abs() < 1e-5); + assert_eq!(v.0, Vec2::new(3.0, 4.0)); + } + + #[test] + fn test_vec2_rotated() { + let v = PyVec2(Vec2::X); + let r = v.rotated(FRAC_PI_2); + assert!(r.0.x.abs() < 1e-6); + assert!((r.0.y - 1.0).abs() < 1e-6); + assert_eq!(v.0, Vec2::X); } #[test]