#Python 2 (Array-based approach)
Python 2 (Array-based approach)
It creates an array of arrays with truth-values if a specific square in that grid is inside the circle or outside the circle. It should get more precise the bigger the circle is you draw. It then selects either an area below or above a certain row and counts the amount of squares that belongs to the circle and divides that by the amount of squares that are in the entire circle.
import math as magic
magic.more = magic.pow
magic.less = magic.sqrt
def a( width, length, depth, diameter ):
precision = 350 #Crank this up to higher values, such as 20000
circle = []
for x in xrange(-precision,precision):
row = []
for y in xrange(-precision,precision):
if magic.less(magic.more(x, 2.0)+magic.more(y, 2.0)) <= precision:
row.append(True)
else:
row.append(False)
circle.append(row)
if min(width,length,depth) >= diameter:
return 0
elif min(width,length) >= diameter:
row = precision*2-int(precision*2*float(depth)/float(diameter))
total = len([x for y in circle for x in y if x])
ammo = len([x for y in circle[:row] for x in y if x])
return float(ammo)/float(total)
else:
#Why try to fit a sphere in a box if you can try to fit a box on a sphere
maxwidth = int(float(precision*2)*float(min(width,length))/float(diameter))
for row in xrange(0,precision*2):
rowwidth = len([x for x in circle[row] if x])
if rowwidth > maxwidth:
total = len([x for y in circle for x in y if x])
ammo = len([x for y in circle[row:] for x in y if x])
return float(ammo)/float(total)