2013-10-22 57 views
0

我需要能夠基於多個圓圈生成形狀。例如: 如果我想圍繞一個正方形放置8個圓圈,如何計算要觸摸每個周圍圓圈的正方形的大小。 圓或橢圓的問題相同。生成形狀的算法

我嘗試以ascii格式添加一些圖像,但不確定它是否清晰。

           

        @@@@@   @@@@@      
        @@@@@@@@@@  @@@@@@@@@@     
        @@@@@ @@@@@@ @@@@@@ @@@@@     
       @@@@  @@@@ @@@@  @@@@     
       @@@@  @@@@@@@  @@@@    
       @@@   @@@@@   @@@    
       @@@   @@@   @@@    
       @@@   @@@   @@@    
       @@    @@@    @@    
       @@@   @@@   @@@    
       @@@   @@@   @@@    
       @@   @@@@@   @@    
     @@@@@ @@@   @@@@@   @@@ @@@@@   
     @@@@@@@@@@@@@@  @@@ @@@  @@@@@@@@@@@@@@  
    @@@@@@ @@@@@@@@@  @@@@@ @@@@@  @@@@@@@@@ @@@@@@  
    @@@@  @@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@  @@@@ 
    @@@   @@@@@@@@@@@@  @@@@@@@@@@@@@  @@@@ 
    @@@   @@@,,,,,,,,,,,,,,,,,,,,,,, @@@   @@@ 
    @@@   @@@,      ,@@@   @@ 
    @@    @@,      ,@@@   @@ 
    @@    @@,      ,@@    @@ 
    @@    @@,      ,@@@   @@ 
    @@    @@,      ,@@@   @@ 
    @@@   @@@,      , @@   @@@ 
    @@@@   @@@@,      , @@@   @@@ 
    @@@@  @@@@ ,      , @@@@  @@@ 
    @@@@  @@@@ ,      , @@@@  @@@@@ 
     @@@@@@@@@@@ ,      , @@@@@@@@@@@@  
     @@@@@@@@@ ,      , @@@@@@@@@  
     @@@@@@@@@@@ ,      , @@@@@@@@@@@@  
    @@@@  @@@@ ,      , @@@@  @@@@@ 
    @@@@  @@@@ ,      , @@@@  @@@ 
    @@@@   @@@@,      , @@@   @@@ 
    @@@   @@@,      , @@   @@@ 
    @@    @@,      ,@@@   @@ 
    @@    @@,      ,@@@   @@ 
    @@    @@,      ,@@    @@ 
    @@    @@,      ,@@@   @@ 
    @@@   @@@,,,,,,,,,,,,,,,,,,,,,,,@@@   @@ 
    @@@   @@@@@@@@@@@  @@@@@@@@ @@@   @@@ 
    @@@   @@@@@@@@@@@@@  @@@@@@@@@@@@@@  @@@@ 
    @@@@  @@@@@@@ @@@@ @@@@ @@@@@@@@  @@@@ 
    @@@@@@ @@@@@@@@  @@@@ @@@@  @@@@@@@@ @@@@@@  
     @@@@@@@@@@@@@   @@@@@@@   @@@@@@@@@@@@@  
     @@@@@ @@   @@@@@   @@ @@@@@   
       @@@   @@@   @@@    
       @@@   @@@   @@@    
       @@    @@@    @@    
       @@@   @@@   @@@    
       @@@   @@@   @@@    
       @@   @@@@@   @@    
       @@@   @@@@@@@   @@@    
       @@@@  @@@@ @@@@  @@@@    
       @@@@@ @@@@ @@@@ @@@@@     
        @@@@@@@@@@@  @@@@@@@@@@@     
        @@@@@@@@  @@@@@@@@     

它應該是相同的一個圓而不是正方形在中間。

感謝您的幫助。

回答

0

感謝大家的回答,我想出瞭如何爲這個問題生成python代碼。

下產生所要求的形狀

import sys 
    import pygame 
    import math 

    pygame.init() 

    #create the screen 
    window = pygame.display.set_mode((640, 480)) 

    n=15; #n satellites 
    r=20; #radius of a satellite 
    R= abs((2*r)/(2*(math.sin(math.pi/n)))); # circumradius of the regular polygon. 
    a=360/n; #rotating angle for vertex 
    color = (255, 255, 255); 


    #input handling (somewhat boilerplate code): 
    while True: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       sys.exit(0) 
      if event.type == pygame.MOUSEBUTTONUP: 
       mpos = pygame.mouse.get_pos(); 
       mx, my = event.pos 
       for angle in range(0, 361): 
        theta = math.radians(angle) 
        x = R * math.cos(theta) 
        y = R * math.sin(theta) 
        print "Theta = "+str(theta) 
        if (angle % int(a) == 0): 
         pygame.draw.circle(window, color, (int(mx+x),int(my+y)), r,1); 
       pygame.draw.circle(window, color, (mx,my), int(R-r),1); 
       pygame.display.flip(); 

enter image description here

2

如果正方形是1x1的,r是圓的半徑,則可以表明,下列方程式必須持有:

2r + 1 = 2r/sqrt(2) + 2r + 2r/sqrt(2)   (*) 

小代數表示R = SQRT(2)/ 4〜0.354 。

從這很容易得到圓圈的中心。我會讓你弄清楚。

Diagram showing how (*) is obtained

如果你有橢圓大小相同的,只是它們的縱橫縮放問題:這個尺寸做的也顯示了圓圈圖如何(*)獲得

這裏的比例在一邊。

+0

基因,感謝您的回覆。其實,這並不是我想要的。可能我的解釋不正確。我知道圓的直徑和半徑。我需要知道如何在這些圈子的中間放置一個正方形。你的方程仍然有效? – user2360915

+0

這是如何派生的?你是否假設一個對稱的解決方案?爲了對稱,圓的中心必須等於垂直方向的一條直線和一條平分它的斜線和正方形的頂部。但我不認爲這是可以做到的,因爲如果你平分所提到的兩條直線之間的90度夾角和另外兩條夾角之間的135度夾角,你所得到的不是一個等腰三角形,正如你所期望的,從一個恆定半徑的圓。 – mcdowella

+0

我添加了一張照片。確定派生工作。它顯示了半徑與方形邊的比例爲.354:r/s = .354。所以如果你知道r,那麼平方的大小就是r/.354。如果你有elipses,只需在一個軸上縮放問題以使它們成爲圓圈。求解正方形,然後做反比例得到一個矩形。 – Gene