Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- """
- This script calculates how many squares of length 1 fit in a circle.
- The fitting squares are divided into partial and completely in the circle.
- """
- def generate_squares(radius):
- """Generates all the squares possibly in the circle
- as 4 point tuples."""
- square_list = []
- length = math.ceil(radius)
- for x in range(length):
- for y in range(length):
- square = ((x,y),(x+1,y),(x,y+1),(x+1,y+1))
- square_list.append(square)
- return square_list
- def count_squares(radius):
- """ Counts all the full and partial squares in the circle by checking
- how many edges are in the circle"""
- square_list = generate_squares(radius)
- full_squares = 0
- partial_squares = 0
- for square in square_list:
- edges_in_circle = 0
- for (x,y) in square:
- if x**2 + y**2 <= radius**2:
- edges_in_circle += 1
- if(edges_in_circle == 4):
- full_squares += 1
- elif(edges_in_circle>0):
- partial_squares += 1
- return (full_squares*4,partial_squares*4)
Advertisement
Add Comment
Please, Sign In to add comment