Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slicing a circle in equal segments, Python

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.

enter image description here

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)

enter image description here

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!!

like image 495
Srivatsan Avatar asked Jul 14 '15 10:07

Srivatsan


People also ask

How do you break a circle into segments?

Draw a straight line through the middle and two sides of the circle using a ruler. Place the ruler so that the line passes through the circle's center point. Extend the line all the way through the edges of the circle. Two equal segments will now appear on the circle.


3 Answers

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
like image 189
6502 Avatar answered Oct 17 '22 09:10

6502


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)
like image 26
ebvalaim Avatar answered Oct 17 '22 11:10

ebvalaim


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)

like image 34
Aaron Digulla Avatar answered Oct 17 '22 10:10

Aaron Digulla