私はクラス立方体を持っています:
class Cuboid:
#a rectangular solid
def __init__(self, (x0, y0, z0), (x1, y1, z1)):
self.z0 = min(z0,z1)
self.x0 = min(x0,x1)
self.y0 = min(y0,y1)
self.z1 = max(z0,z1)
self.x1 = max(x0,x1)
self.y1 = max(y0,y1)
self.start = (self.x0, self.y0, self.z0)
self.end = (self.x1, self.y1, self.z1)
def centre(self):
center_x = (self.x0 + self.x1)/2
center_y = (self.y0 + self.y1)/2
center_z = (self.z0 + self.z1)/2
return (center_x, center_y, center_z)
def intersects(self, other):
#returns true if this cuboid intersects with another one
if self == other:
return True
else:
return (
self.x0 <= other.x1
and self.x1 >= other.x0
and self.y0 <= other.y1
and self.y1 >= other.y0
and self.z0 <= other.z1
and self.z1 >= other.z0
)
ランダムな座標を持つ 10 個の立方体のリストを作成します。
cubelist = []
for i in range(10):
cube = Cuboid((rand.randint(1,80), rand.randint(1,80), rand.randint(1,3)), (rand.randint(1,80), rand.randint(1,80), rand.randint(5,9)))
cubelist.append(cube)
私ができるようにしたいのは、このリストを取得して、交差するすべてのキューブを削除することです(追加を呼び出すときに交差を確認できますが、この方法でもできるかどうかを確認したいと思います)
私ができる唯一の方法は、次のようなものです:
def cube_untangler(cubelist)
cubelist1 = [x for x in cubelist]
cubelist2 = [x for x in cubelist]
for x in cubelist:
cubelist1.remove(x)
if any(x.intersects(y) for y in cubelist1):
cubelist2.remove(x)
return cubelist2
ちょっと不器用な感じですけどね。以前にリスト内包表記を使用してみましたが、交差する立方体をすべて削除することはできませんでした。
これを行うより良い方法はありますか?