Skip to content

Instantly share code, notes, and snippets.

@mvanorder
Last active April 19, 2017 22:50
Show Gist options
  • Save mvanorder/181f4857ef5c352b7c4296ad3c4f4e84 to your computer and use it in GitHub Desktop.
Save mvanorder/181f4857ef5c352b7c4296ad3c4f4e84 to your computer and use it in GitHub Desktop.
from math import sqrt
# 2 constants: the square root of 2, and the raduis of the circle we're
# drawing.
ROOT_2 = sqrt(2)
RADIUS = float(1000000000)
# 2 variables: current x position, and the area of the circle being
# calculated.
x = RADIUS
circle_area = float()
# Loop through y values of 1 through the y value where a 45 degree line is
# drawn from the center. Saving time be drawing only 1/8th of a circle, the
# smallest symetrical section of a crcle in a square.
for y in range(1, int(ROOT_2 * 0.5 * RADIUS) + 1):
# While the hypotenuse is greater than the radius, deincriment x by 1.
while sqrt(x ** 2 + y ** 2) > RADIUS:
x -= 1
# Add the area of the section of the current line (from the 45 degree line
# to the edge of the circle) times 8 to the circle's area value.
circle_area += (x - y) * 8
# using a = πr^2 reverse it -> π = a/r^2 and print out the result.
print(format(circle_area / (RADIUS ** 2), '.100g'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment