1
我想爲我的Cuboid類編寫一個方法,該方法返回由兩個長方體相交定義的長方體。如何計算兩個長方體的交點
長方體在內部由一個原點和一個終點表示,這樣從原點到終點的矢量在所有維度上必然爲正。作爲一個起點(也許有用也許不是)下面的方法(在紅寶石中)確定兩個長方體是否相交。
def intersects? other_cuboid
return not(self.top < other_cuboid.bottom ||
self.bottom > other_cuboid.top ||
self.left > other_cuboid.right ||
self.right < other_cuboid.left ||
self.front < other_cuboid.back ||
self.back > other_cuboid.front)
end
直覺似乎應該有一個相當吝嗇地解決了這個問題,但我想不出它...任何想法?
注:長方體必然與軸對齊
投票關閉。在不知道Cuboid構造函數方法需要什麼參數的情況下,您應該如何返回一個Cuboid的新實例? – sawa
「長方體」是否總是軸向對齊? – andand