user2360915
user2360915

Reputation: 1139

algorithm to generate shapes

I need to be able to generate shapes based on multiple circles. So for example: If I want to put 8 circles around a square, how to calculate the size of the square that is going to touch each surrounding circles. Same issue for a circle or ellipse.

I try to add some image in ascii format, not sure it is clear.

                                        

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

It should be the same for a circle instead of square in the middle.

Thanks for your help.

Upvotes: 0

Views: 688

Answers (2)

user2360915
user2360915

Reputation: 1139

Thanks to everybody's answers i figured out how to generate python code for this problem.

The following generate the requested shape

   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

Upvotes: 0

Gene
Gene

Reputation: 46960

If the square is 1x1 and r is the radius of the circles, then you can show that the following equation must hold:

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

A little algebra shows r = sqrt(2) / 4 ~ 0.354.

From this it's pretty simple to get the centers of the circles. I'll let you figure that out.

Here's a diagram made with this size for the circles that also shows how (*) is obtained:

Diagram showing how (*) is obtained

If you have ellipses all of the same size, just scale the problem by their aspect ratio on one side.

Upvotes: 2

Related Questions