Guest User

squares_in_circle

a guest
May 4th, 2015
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. import math
  2.  
  3. """
  4. This script calculates how many squares of length 1 fit in a circle.
  5. The fitting squares are divided into partial and completely in the circle.
  6. """
  7.  
  8. def generate_squares(radius):
  9.     """Generates all the squares possibly in the circle
  10.       as 4 point tuples."""
  11.  
  12.     square_list  = []
  13.     length = math.ceil(radius)
  14.    
  15.     for x in range(length):
  16.        
  17.         for y in range(length):
  18.             square = ((x,y),(x+1,y),(x,y+1),(x+1,y+1))
  19.             square_list.append(square)
  20.            
  21.     return square_list
  22.  
  23. def count_squares(radius):
  24.     """ Counts all the full and partial squares in the circle by checking
  25.    how many edges are in the circle"""
  26.    
  27.     square_list = generate_squares(radius)
  28.    
  29.     full_squares = 0
  30.     partial_squares = 0
  31.    
  32.     for square in square_list:
  33.         edges_in_circle = 0
  34.        
  35.         for (x,y) in square:
  36.             if x**2 + y**2 <= radius**2:
  37.                 edges_in_circle += 1
  38.         if(edges_in_circle == 4):
  39.             full_squares += 1
  40.         elif(edges_in_circle>0):
  41.             partial_squares += 1
  42.  
  43.     return (full_squares*4,partial_squares*4)
Advertisement
Add Comment
Please, Sign In to add comment