Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My bad-boids refactoring (Efthymios Maneas) #5

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 73 additions & 44 deletions boids.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,83 @@

from matplotlib import pyplot as plt
from matplotlib import animation
import random

# Deliberately terrible code for teaching purposes

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):
xs,ys,xvs,yvs=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)
# 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])
# 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)
# Move according to velocities
for i in range(len(xs)):
xs[i]=xs[i]+xvs[i]
ys[i]=ys[i]+yvs[i]


figure=plt.figure()
axes=plt.axes(xlim=(-500,1500), ylim=(-500,1500))
scatter=axes.scatter(boids[0],boids[1])
from numpy import array

from my_update_boids import update_boids


class Boid(object):
def __init__(self, x_position, y_position, x_velocity, y_velocity):
self.x_position = x_position
self.y_position = y_position
self.x_velocity = x_velocity
self.y_velocity = y_velocity

def get_position(self):
return array([self.x_position, self.y_position])

def get_velocity(self):
return array([self.x_velocity, self.y_velocity])


def generate_random_boid():
import random

x_position = random.uniform(-450, 50.0)
y_position = random.uniform(300.0, 600.0)
x_velocity = random.uniform(0, 10.0)
y_velocity = random.uniform(-20.0, 20.0)

return Boid(x_position, y_position, x_velocity, y_velocity)


def initialize_boids(total):
new_boids = []

for i in range(total):
new_boids.append(generate_random_boid())

return new_boids


def get_boids(total_boids):
x_pos = []
y_pos = []
x_vel = []
y_vel = []

for boid in total_boids:
x_pos.append(boid.get_position()[0])
y_pos.append(boid.get_position()[1])
x_vel.append(boid.get_velocity()[0])
y_vel.append(boid.get_velocity()[1])

return x_pos, y_pos, x_vel, y_vel


# Number and positions/velocities of boids initialization
boids_number = 50
all_boids = initialize_boids(boids_number)
boids = get_boids(all_boids)

print(boids)

# Boids Animation
figure = plt.figure()
axes = plt.axes(xlim=(-500, 1500), ylim=(-500, 1500))
scatter = axes.scatter(boids[0], boids[1])


def animate(frame):
update_boids(boids)
scatter.set_offsets(zip(boids[0],boids[1]))
update_boids(boids)
zipped = []
for i in range(boids_number):
zipped.append((boids[0][i], boids[1][i]))

scatter.set_offsets(zipped)


anim = animation.FuncAnimation(figure, animate,
frames=50, interval=50)
anim = animation.FuncAnimation(figure, animate, frames=50, interval=50)

if __name__ == "__main__":
plt.show()
4 changes: 4 additions & 0 deletions boids_velocity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from numpy import power

def tmp_velocity(x_positions,y_positions,i,j):
return power((x_positions[j]-x_positions[i]),2) + power((y_positions[j]-y_positions[i]),2)
Loading