MCPcopy Create free account
hub / github.com/ActiveState/code / Boid

Class Boid

recipes/Python/502216_Boids_Demonstration/recipe-502216.py:135–199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

133# BOID RULE IMPLEMENTATION CLASS
134
135class Boid:
136
137 def __init__(self, width, height, offset):
138 self.velocity = TwoD(0, 0)
139 self.position = TwoD(*self.random_start(width, height, offset))
140
141 def random_start(self, width, height, offset):
142 if random.randint(0, 1):
143 # along left and right
144 y = random.randint(1, height)
145 if random.randint(0, 1):
146 # along left
147 x = -offset
148 else:
149 # along right
150 x = width + offset
151 else:
152 # along top and bottom
153 x = random.randint(1, width)
154 if random.randint(0, 1):
155 # along top
156 y = -offset
157 else:
158 # along bottom
159 y = height + offset
160 return x, y
161
162
163 def update_velocity(self, boids):
164 v1 = self.rule1(boids)
165 v2 = self.rule2(boids)
166 v3 = self.rule2(boids)
167 self.__temp = v1 + v2 + v3
168
169 def move(self):
170 self.velocity += self.__temp
171 limit_speed(self)
172 self.position += self.velocity / 100
173
174 def rule1(self, boids):
175 # clumping
176 vector = TwoD(0, 0)
177 for boid in boids:
178 if boid is not self:
179 vector += boid.position
180 vector /= len(boids) - 1
181 return (vector - self.position) / 7.5
182
183 def rule2(self, boids):
184 # avoidance
185 vector = TwoD(0, 0)
186 for boid in boids:
187 if boid is not self:
188 if (self.position - boid.position).mag() < 25:
189 vector -= (boid.position - self.position)
190 return vector
191
192 def rule3(self, boids):

Callers 1

build_boidsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected