首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中计算boid的邻域

在python中计算boid的邻域
EN

Stack Overflow用户
提问于 2013-03-26 21:32:08
回答 1查看 550关注 0票数 0

我正在编写一个程序,通过boids来模拟蟒蛇中的一群鸟。其中一项任务是计算邻接体的数量(distance <= 50)。我试着这样做(见代码),但我没有得到好的结果。“打印距离”给出了20倍的相同距离,所以我假设我计算的是相同的几个体块20倍。我需要所有的组合。我是编程新手,所以欢迎任何帮助!

代码语言:javascript
复制
WIDTH = 1000            # WIDTH OF SCREEN IN PIXELS
HEIGHT = 500            # HEIGHT OF SCREEN IN PIXELS
BOIDS = 20              # NUMBER OF BOIDS IN SIMULATION
SPEED_LIMIT = 500       # FOR BOID VELOCITY
BOID_EYESIGHT = 50      # HOW FAR A BOID CAN LOOK
WALL = 50               # FROM SIDE IN PIXELS
WALL_FORCE = 100        # ACCELERATION PER MOVE


################################################################################

import random
from math import sqrt

X = 0
Y = 1
VX = 2
VY = 3

boids = []

for i in range(BOIDS):
    b_pos_x = random.uniform(0,WIDTH)
    b_pos_y = random.uniform(0,HEIGHT)
    b_vel_x = random.uniform(-100,100)
    b_vel_y = random.uniform(-100,100)
    b = [b_pos_x, b_pos_y, b_vel_x, b_vel_y]

    boids.append(b)

# definition of functions:

def calculate_distance(a,b):                       # calculates distance between two boids
    distance = sqrt((a[X] - b[X])**2 + (a[Y] - b[Y])**2)
    return distance

def value_velocity(v):                             # calculates velocity of boids
    velocity = sqrt(v[VX]**2+v[VY]**2)
    return velocity

##############

for element_1 in range(len(boids)):             
    for element_2 in range(len(boids)):
        if element_1 != element_2:                    # for two different boids:
            distance = calculate_distance(boids[element_1],boids[element_2]) 
                 # calculate distance between boids

    velocity = value_velocity(boids[element_1])             # calculate velocity of boids

neighbors = 0                      # start neighbor counter

for element_1 in range(len(boids)):
    for element_2 in range(len(boids)):
        if element_1 != element_2:          # for diferent boids
            if distance <= 50:              # with a distance of <=50
                neighbors += 1              # add +1 to neighbor counter
    print distance
EN

回答 1

Stack Overflow用户

发布于 2013-03-26 21:40:30

您应该将距离设置为数组或合并两个循环。因为目前您总是使用在第一对for循环中计算的最后一个distance值。

例如,您可以通过以下方式修改您的代码:

代码语言:javascript
复制
distance=[[calculate_distance(boids[i],boids[j]) for j in range(len(boids))] for i in range(len(boids))]

for element_1 in range(len(boids)):
    for element_2 in range(len(boids)):
        if element_1 != element_2:          # for diferent boids
            if distance[element_1][element_2] <= 50:              # with a distance of <=50
                neighbors += 1
                print distance[element_1][element_2]

你也可以只做第一个循环中的所有邻居计算:

代码语言:javascript
复制
neighbors =0
for element_1 in range(len(boids)):             
    for element_2 in range(len(boids)):
        if element_1 != element_2:                    # for two different boids:
            distance = calculate_distance(boids[element_1],boids[element_2]) 
            if distance <=50:
               neighbors += 1
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15638337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档