1

動いているボールを適切なビンに入れようと取り組んでいます。私は自分が正しい道を進んでいると思いたいのですが、しばらく行き詰まっています。

私の質問に関係がないと思われるコードは省略しましたが、回答者がさらに詳細を必要とする場合は、それらを提供できます。基本的に、200 個の動くボールの世界があります。X 座標と Y 座標があります。世界を幅 256 の正方形のビンに分割し、ボールを適切なビンに配置したいと考えています。

これに対する私のアプローチは、それらを辞書に入れることでした。次のように見えました。

dict_of_balls = {}
for i in range(len(balls)):
    xb = int(balls[i].x/256)
    yb = int(balls[i].y/256)

キーをペアのタプルにして、そのビンに適切なボールを配置したかったの(xb, yb)ですが、タプルをキーとして使用できるとは思いません...

コードは以下のとおりです。

import math
import random
import time
import sys


ball_min_radius = 16.0 #world coordinates         
ball_max_radius = 128.0  #world coordniates
number_balls = 200

class Ball:
    """ 
    Implements a point/ball
    """

    def __init__(self):
          self.x = random.uniform(world_min_x,world_max_x)
          self.y = random.uniform(world_min_y,world_max_y)
          self.radius = int(random.uniform(ball_min_radius,ball_max_radius))
    def __lt__(self, other):
        return self.id < other.id

def main():
    world_min_x = -200.0*number_balls**.5  # minimum x in world coordinates
    world_max_x = +200.0*number_balls**.5  # maximum x in world coordinates
    world_min_y = -200.0*number_balls**.5  # minimum y in world coordinates
    world_max_y = +200.0*number_balls**.5  # maximum y in world coordinates

    balls = [Ball() for i in range(number_balls)]

与えられた世界座標に基づいて世界をビンに分割する方法について誰かアイデアがありますか? キーにタプルを使用できないため、どのデータ構造を使用すればよいかわかりません。フィードバックをお寄せいただきありがとうございます。

4

2 に答える 2

1

なぜ辞書が欲しいのですか?これを行う方法は次のとおりですが、キーを (int, int) に具体的にキャストしていて、キーが一意であるため、ビンごとに 1 つのボールしか得られないことに注意してください。

コレクションを使用する場合は、並べ替えもできます (私の例では、地域識別子で並べ替えています)。

あなたが何のためにそれをしているのかわかりませんが、あなたはそれを行うことができます:

import math
import random
import time
import sys


ball_min_radius = 16.0 #world coordinates         
ball_max_radius = 128.0  #world coordniates
number_balls = 200

world_min_x = -200.0*number_balls**.5  # minimum x in world coordinates
world_max_x = +200.0*number_balls**.5  # maximum x in world coordinates
world_min_y = -200.0*number_balls**.5  # minimum y in world coordinates
world_max_y = +200.0*number_balls**.5  # maximum y in world coordinates


class Ball:
    """ 
    Implements a point/ball
    """

    def __init__(self):
          self.x = random.uniform(world_min_x,world_max_x)
          self.y = random.uniform(world_min_y,world_max_y)
          self.radius = int(random.uniform(ball_min_radius,ball_max_radius))
    def __lt__(self, other):
        return self.id < other.id

    def __str__(self):
        return 'x={x} y={y} r={r}'.format(x=self.x, y=self.y, r=self.radius)

def main():

    balls = [Ball() for i in range(number_balls)]

    dict_of_balls = {}
    ball_collection = []
    for b in balls:
        xb = int(b.x/256)
        yb = int(b.y/256)
        key = (xb, yb)
        dict_of_balls[key] = b

        ball_collection.append((key, b))

    print 'length of dictionary:{}'.format(len(dict_of_balls.keys()))
    print 'length of collection:{}'.format(len(ball_collection))

コレクションよりもディクショナリの項目の方が少ないことに注意してください。

この方法で各アイテムをかなり簡単に印刷することもできます。

    for b in ball_collection:
        print 'ball region: {r}   with coords: {c}'.format(r=b[0], c=b[1])

または、必要に応じて並べ替えます。

    print 'Collections also let you sort the collection by region(s)...'
    sorted_list = sorted(ball_collection, key= lambda x: (x[0][0], x[0][1]))


    for b in sorted_list:
        print 'ball region: {r}   with coords: {c}'.format(r=b[0], c=b[1])

また、特定の地域でボールを簡単に取得することもできます。

    print '... or get only ones in a specific region'
    subset =  [b for b in ball_collection if b[0][0] == 1]

    for b in subset:
        print 'ball region: {r}   with coords: {c}'.format(r=b[0], c=b[1])


main()

コレクションは、あなたが実際に望んでいることをしているようです。

于 2016-03-10T01:46:37.253 に答える