Slicing a circle in equal segments, Python

半城伤御伤魂 提交于 2019-12-05 08:13:43

You can compute the array of slice numbers directly with with numpy operators:

sliceno = numpy.int32((pi + numpy.arctan2(Y, X)) * (N / (2*pi)))

meaning:

  • compute the angle -pi...pi for each point with arctan2
  • shift by pi to make it a positive interval
  • rescale to 0..N-1
  • convert to an integer

You should probably use math.atan2:

angle = math.atan2(dec, ra)
if angle >= n*math.pi/4 and angle < (n+1)*math.pi/4:
    # point is inside slice number n

Basically, atan2 returns the angle to the point from the x axis. By dividing it into intervals of pi/4, you get your slices. But beware - atan2 returns angles between -pi and pi, so you should number your slices from -4 to 3 (or you can add pi to the angle, or convert it in some other way).

EDIT: Modifying your code, it would look like this:

slice1 = []
n = 0  #change 0 to desired slice number here (from -4 to 3)
for a,b in zip(ra,dec):
    angle = math.atan2(b,a)
    if angle >= n*math.pi/4 and angle < (n+1)*math.pi/4:
        slice1.append(a,b)

First, find the quadrant using your formula. The octant can then be determined by comparing abs(x) with abs(y).

In the lower octant, abs(x) >= abs(y). The other one has abs(x) < abs(y)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!