I have the radius of a circle and the equation y = int(math.sqrt(pow(radius, 2)-pow(i,2)))
(Pythagoras). I want to iterate over a specific range with a custom step size, which is a multiple of 16. Here's an example to illustrate my desired output:
radius = 4
# Desired output: -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4
# Actually output: -4, -3, -2, 1, 1, 2, 3, 4
I found a workaround using list comprehensions. By dividing the values by 2, I was able to achieve the desired step size. Here's the code that works for radius = 4:
from itertools import chain
radius = 4
result = [x/2 for x in chain(range(-2*radius, 0), range(1, 2*radius+1))]
print("Length: ", len(result))
print("Values: ", result)
The output is as expected with a length of 16. However, this approach is not generalizable, and it fails for other values of radius, such as radius = 5
. I'm looking for a solution allowing me to loop through a range in a specific number of steps. How can I achieve this?