2014-11-08 18 views
3

我試圖檢查一個點是否在以(x,y,z)中心點爲中心的球體中,其中(x,y,z )不是(0,0,0)。Python檢查一個點是否在中心x,y,z的球體中

這段代碼我使用生成我要檢查的要點:

def generatecoords(self, i): 
    x, y, z = generatepoint() 

    if i >= 1: 
     valid = False 

     while valid == False: 
      coords = self.checkpoint(x, y, z) 

      for b in world.starlist: 
       if coords == world.starlist[b].coords: 
        coords = self.checkpoint(x, y, z) 

       else: 
        valid = True 

    else: 
     coords = self.checkpoint(x, y, z) 

    return coords 

def checkpoint(self, x, y, z): 
    d = math.sqrt(x * x + y * y + z * z) 

    while d >= self.radius: 
     x, y, z = generatepoint() 
     d = math.sqrt(x * x + y * y + z * z) 

    coords = (int(x), int(y), int(z)) 

    return coords 

def generatepoint(): 
    x, y, z = [int(random.uniform(-self.radius, self.radius)) \ 
       for b in range(3)] 

    return x, y, z 

這些功能被稱爲在for循環中,產生在字典中的點,同時還檢查不大可能機會點沒有放在另一個頂部(主要是因爲我可以)。

我試圖找出我需要添加到math.sqrt(x * x + y * y + z * z),以便它佔的中心不是(0, 0, 0)。我知道有一種方法可以做到這一點,但它需要多行代碼,我寧願一個一個做。我會在另一個問題的答案的評論中提出這個問題,但我不允許對答案發表評論。

+0

math.sqrt((xa)** 2 +(yb)** 2 +(zc)** 2)給你一些幫助(a,b,c)嗎? [正在球體中心座標] – giosans 2014-11-08 15:37:24

回答

16

的公式爲:

甲點(X,Y,Z)是與球的內部中心(CX,CY,CZ)和半徑ř如果

(x-cx) ^2 + (y-cy) ^2 + (z-cz)^2 < r^2 
+0

謝謝,我希望我能投票的東西。 – Eegxeta 2014-11-08 15:56:35

+0

您可以將其標記爲「已回答」,對不對? – JE42 2014-11-08 16:05:00

+0

是的,我沒有注意到複選標記。 – Eegxeta 2014-11-08 16:27:56

相關問題