Slicing a circle in equal segments, Python

2019-05-03 09:26发布

问题:

I have a set of close of 10,000 points on the sky. They are plotted using the RA (right ascension) and DEC (declination) on the sky. When plotted, they take the shape of a circle.

What I would like to do is to slice the circle into 8 equal parts and remove each part one at a time and do some calculations using the remaining parts.

To do so I came up with this illustration in mind, i.e. slicing them using the arcs.

I know that the equation of the arc is given by:

S = r * theta

where

r --> radius
theta --> angle (in our case 45 degrees)

I would somehow like to do this like:

slice1 = []
for a,b in zip(ra,dec):
    if a>some value and a<some value and b>some value and b<some value:
        slice1.append(a,b)

If they were a square, it becomes really easy, and the above equation can immediately be applied.

So once I have my slice, I can then do a numpy.where() to find out the rest of my circle.

I can easily slice it into four slices by just mentioning the min(RA),max(RA),min(DEC) and max(DEC). One such example when I do it for the first quadrant will give me this:

RA>0.0 and RA<max(RA) DEC>0.0 and DEC<max(DEC)

I don't know how to go about doing this in my case (i.e. into 8 quadrants!!), wherein I only have the x,y coordinates of my data points!!

回答1:

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


回答2:

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)


回答3:

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)