算法生成形状

时间:2013-10-22 03:59:22

标签: algorithm geometry shapes

我需要能够基于多个圆圈生成形状。例如: 如果我想在一个正方形周围放置8个圆圈,如何计算将触及每个周围圆圈的正方形的大小。 圆圈或椭圆的问题相同。

我尝试以ascii格式添加一些图像,但不确定它是否清晰。

                                        

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

圆圈应该是相同的,而不是中间的正方形。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

如果方格是1x1且r是圆的半径,那么您可以显示以下公式必须保持:

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

一个小代数显示r = sqrt(2)/ 4~0.354。

从这里获得圈子的中心非常简单。我会让你弄明白的。

以下是圆圈的图表,圆圈也显示了如何获得(*):

Diagram showing how (*) is obtained

如果你有相同大小的椭圆,只需按一方面的宽高比缩放问题。

答案 1 :(得分: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

相关问题