From 098dc955566217f2858c445788f09781b3bf531c Mon Sep 17 00:00:00 2001 From: Bronte Wan Date: Thu, 8 Dec 2022 10:31:38 +0000 Subject: [PATCH 1/5] changed variable names --- boids/boids.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/boids/boids.py b/boids/boids.py index 49e8f69..3e15936 100644 --- a/boids/boids.py +++ b/boids/boids.py @@ -17,27 +17,27 @@ boids=(boids_x,boids_y,boid_x_velocities,boid_y_velocities) def update_boids(boids): - xs,ys,xvs,yvs=boids + x_pos,y_pos,x_vel,y_vel=boids # Fly towards the middle - for i in range(len(xs)): - for j in range(len(xs)): - xvs[i]=xvs[i]+(xs[j]-xs[i])*0.01/len(xs) - for i in range(len(xs)): - for j in range(len(xs)): - yvs[i]=yvs[i]+(ys[j]-ys[i])*0.01/len(xs) + for i in range(len(x_pos)): + for j in range(len(x_pos)): + x_vel[i]=x_vel[i]+(x_pos[j]-x_pos[i])*0.01/len(x_pos) + for i in range(len(x_pos)): + for j in range(len(x_pos)): + y_vel[i]=y_vel[i]+(y_pos[j]-y_pos[i])*0.01/len(x_pos) # Fly away from nearby boids - for i in range(len(xs)): - for j in range(len(xs)): - if (xs[j]-xs[i])**2 + (ys[j]-ys[i])**2 < 100: - xvs[i]=xvs[i]+(xs[i]-xs[j]) - yvs[i]=yvs[i]+(ys[i]-ys[j]) + for i in range(len(x_pos)): + for j in range(len(x_pos)): + if (x_pos[j]-x_pos[i])**2 + (y_pos[j]-y_pos[i])**2 < 100: + x_vel[i]=x_vel[i]+(x_pos[i]-x_pos[j]) + y_vel[i]=y_vel[i]+(y_pos[i]-y_pos[j]) # Try to match speed with nearby boids - for i in range(len(xs)): - for j in range(len(xs)): - if (xs[j]-xs[i])**2 + (ys[j]-ys[i])**2 < 10000: - xvs[i]=xvs[i]+(xvs[j]-xvs[i])*0.125/len(xs) - yvs[i]=yvs[i]+(yvs[j]-yvs[i])*0.125/len(xs) + for i in range(len(x_pos)): + for j in range(len(x_pos)): + if (x_pos[j]-x_pos[i])**2 + (y_pos[j]-y_pos[i])**2 < 10000: + x_vel[i]=x_vel[i]+(x_vel[j]-x_vel[i])*0.125/len(x_pos) + y_vel[i]=y_vel[i]+(y_vel[j]-y_vel[i])*0.125/len(x_pos) # Move according to velocities - for i in range(len(xs)): - xs[i]=xs[i]+xvs[i] - ys[i]=ys[i]+yvs[i] + for i in range(len(x_pos)): + x_pos[i]=x_pos[i]+x_vel[i] + y_pos[i]=y_pos[i]+y_vel[i] From 68954d631d5051a9da427989bd11740f133b6085 Mon Sep 17 00:00:00 2001 From: Bronte Wan Date: Thu, 8 Dec 2022 10:38:35 +0000 Subject: [PATCH 2/5] Merged repeated for loops --- boids/boids.py | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/boids/boids.py b/boids/boids.py index 3e15936..11a6395 100644 --- a/boids/boids.py +++ b/boids/boids.py @@ -10,34 +10,29 @@ import random -boids_x=[random.uniform(-450,50.0) for x in range(50)] -boids_y=[random.uniform(300.0,600.0) for x in range(50)] -boid_x_velocities=[random.uniform(0,10.0) for x in range(50)] -boid_y_velocities=[random.uniform(-20.0,20.0) for x in range(50)] -boids=(boids_x,boids_y,boid_x_velocities,boid_y_velocities) +boids_x = [random.uniform(-450, 50.0) for x in range(50)] +boids_y = [random.uniform(300.0, 600.0) for x in range(50)] +boid_x_velocities = [random.uniform(0, 10.0) for x in range(50)] +boid_y_velocities = [random.uniform(-20.0, 20.0) for x in range(50)] +boids = (boids_x, boids_y, boid_x_velocities, boid_y_velocities) + def update_boids(boids): - x_pos,y_pos,x_vel,y_vel=boids - # Fly towards the middle - for i in range(len(x_pos)): - for j in range(len(x_pos)): - x_vel[i]=x_vel[i]+(x_pos[j]-x_pos[i])*0.01/len(x_pos) - for i in range(len(x_pos)): - for j in range(len(x_pos)): - y_vel[i]=y_vel[i]+(y_pos[j]-y_pos[i])*0.01/len(x_pos) - # Fly away from nearby boids - for i in range(len(x_pos)): - for j in range(len(x_pos)): - if (x_pos[j]-x_pos[i])**2 + (y_pos[j]-y_pos[i])**2 < 100: - x_vel[i]=x_vel[i]+(x_pos[i]-x_pos[j]) - y_vel[i]=y_vel[i]+(y_pos[i]-y_pos[j]) - # Try to match speed with nearby boids + x_pos, y_pos, x_vel, y_vel = boids + for i in range(len(x_pos)): for j in range(len(x_pos)): - if (x_pos[j]-x_pos[i])**2 + (y_pos[j]-y_pos[i])**2 < 10000: - x_vel[i]=x_vel[i]+(x_vel[j]-x_vel[i])*0.125/len(x_pos) - y_vel[i]=y_vel[i]+(y_vel[j]-y_vel[i])*0.125/len(x_pos) - # Move according to velocities - for i in range(len(x_pos)): - x_pos[i]=x_pos[i]+x_vel[i] - y_pos[i]=y_pos[i]+y_vel[i] + # Fly towards the middle + x_vel[i] = x_vel[i] + (x_pos[j] - x_pos[i]) * 0.01 / len(x_pos) + y_vel[i] = y_vel[i] + (y_pos[j] - y_pos[i]) * 0.01 / len(x_pos) + # Fly away from nearby boids + if (x_pos[j] - x_pos[i]) ** 2 + (y_pos[j] - y_pos[i]) ** 2 < 100: + x_vel[i] = x_vel[i] + (x_pos[i] - x_pos[j]) + y_vel[i] = y_vel[i] + (y_pos[i] - y_pos[j]) + # Try to match speed with nearby boids + if (x_pos[j] - x_pos[i]) ** 2 + (y_pos[j] - y_pos[i]) ** 2 < 10000: + x_vel[i] = x_vel[i] + (x_vel[j] - x_vel[i]) * 0.125 / len(x_pos) + y_vel[i] = y_vel[i] + (y_vel[j] - y_vel[i]) * 0.125 / len(x_pos) + # Move according to velocities + x_pos[i] = x_pos[i] + x_vel[i] + y_pos[i] = y_pos[i] + y_vel[i] From daf3051f7746c8a011951e50f6a9c7182d6a6324 Mon Sep 17 00:00:00 2001 From: Bronte Wan Date: Thu, 8 Dec 2022 10:46:28 +0000 Subject: [PATCH 3/5] Input changed for ease of testing --- boids/boids.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/boids/boids.py b/boids/boids.py index 11a6395..032ca2f 100644 --- a/boids/boids.py +++ b/boids/boids.py @@ -10,12 +10,17 @@ import random -boids_x = [random.uniform(-450, 50.0) for x in range(50)] -boids_y = [random.uniform(300.0, 600.0) for x in range(50)] -boid_x_velocities = [random.uniform(0, 10.0) for x in range(50)] -boid_y_velocities = [random.uniform(-20.0, 20.0) for x in range(50)] -boids = (boids_x, boids_y, boid_x_velocities, boid_y_velocities) +# boids_x = [random.uniform(-450, 50.0) for x in range(50)] +# boids_y = [random.uniform(300.0, 600.0) for x in range(50)] +# boid_x_velocities = [random.uniform(0, 10.0) for x in range(50)] +# boid_y_velocities = [random.uniform(-20.0, 20.0) for x in range(50)] +# boids = (boids_x, boids_y, boid_x_velocities, boid_y_velocities) +boids_x = [-300.0, 20.0, 3.0] +boids_y = [420, 480, 540] +boids_x_velocities = [1.0, 2.0, 7.0] +boids_y_velocities = [-15.0, 10.0, 15.0] +boids = (boids_x, boids_y, boids_x_velocities, boids_y_velocities) def update_boids(boids): x_pos, y_pos, x_vel, y_vel = boids @@ -36,3 +41,8 @@ def update_boids(boids): # Move according to velocities x_pos[i] = x_pos[i] + x_vel[i] y_pos[i] = y_pos[i] + y_vel[i] + + print(x_pos, y_pos, x_vel, y_vel) + +update_boids(boids) +print("hi") \ No newline at end of file From 604ace2335b6a970af43b453ca85e092485dd7cd Mon Sep 17 00:00:00 2001 From: Bronte Wan Date: Thu, 8 Dec 2022 10:47:17 +0000 Subject: [PATCH 4/5] Removed unused imports --- boids/boids.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/boids/boids.py b/boids/boids.py index 032ca2f..59f42ac 100644 --- a/boids/boids.py +++ b/boids/boids.py @@ -5,10 +5,7 @@ This code simulates the swarming behaviour of bird-like objects ("boids"). """ -from matplotlib import pyplot as plt -from matplotlib import animation - -import random +#import random # boids_x = [random.uniform(-450, 50.0) for x in range(50)] # boids_y = [random.uniform(300.0, 600.0) for x in range(50)] From ca2eb0a241b1181c3ca34f89d9825c47d08f01bb Mon Sep 17 00:00:00 2001 From: Bronte Wan Date: Thu, 8 Dec 2022 10:51:52 +0000 Subject: [PATCH 5/5] Stored results from repeated calcs into variables --- boids/boids.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/boids/boids.py b/boids/boids.py index 59f42ac..d7826d9 100644 --- a/boids/boids.py +++ b/boids/boids.py @@ -21,20 +21,23 @@ def update_boids(boids): x_pos, y_pos, x_vel, y_vel = boids + number_of_boids = len(x_pos) - for i in range(len(x_pos)): - for j in range(len(x_pos)): + for i in range(number_of_boids): + for j in range(number_of_boids): + diff_x = x_pos[j] - x_pos[i] + diff_y = y_pos[j] - y_pos[i] # Fly towards the middle - x_vel[i] = x_vel[i] + (x_pos[j] - x_pos[i]) * 0.01 / len(x_pos) - y_vel[i] = y_vel[i] + (y_pos[j] - y_pos[i]) * 0.01 / len(x_pos) + x_vel[i] = x_vel[i] + (diff_x) * 0.01 / number_of_boids + y_vel[i] = y_vel[i] + (diff_y) * 0.01 / number_of_boids # Fly away from nearby boids - if (x_pos[j] - x_pos[i]) ** 2 + (y_pos[j] - y_pos[i]) ** 2 < 100: - x_vel[i] = x_vel[i] + (x_pos[i] - x_pos[j]) - y_vel[i] = y_vel[i] + (y_pos[i] - y_pos[j]) + if (diff_x) ** 2 + (diff_y) ** 2 < 100: + x_vel[i] = x_vel[i] + (diff_x) + y_vel[i] = y_vel[i] + (diff_y) # Try to match speed with nearby boids - if (x_pos[j] - x_pos[i]) ** 2 + (y_pos[j] - y_pos[i]) ** 2 < 10000: - x_vel[i] = x_vel[i] + (x_vel[j] - x_vel[i]) * 0.125 / len(x_pos) - y_vel[i] = y_vel[i] + (y_vel[j] - y_vel[i]) * 0.125 / len(x_pos) + if (diff_x) ** 2 + (diff_y) ** 2 < 10000: + x_vel[i] = x_vel[i] + (x_vel[j] - x_vel[i]) * 0.125 / number_of_boids + y_vel[i] = y_vel[i] + (y_vel[j] - y_vel[i]) * 0.125 / number_of_boids # Move according to velocities x_pos[i] = x_pos[i] + x_vel[i] y_pos[i] = y_pos[i] + y_vel[i]