Candice Stauffer
Candice Stauffer

Reputation: 59

Counting Values A Certain Distance From The Center of a Circular Region in Python

I have I list of points that describe all of the pixels in the image shown that I've attached. However, only want count the pixels in the circular region. I understand how to do this if the region were a square, but trying to do this with a circle has be a bit stuck.

Here's what I have so far:

#Count the photons within a certain region
from collections import Counter

#Read in Data
RA = []
DEC = []

with open('ChandraXraysources2.txt') as f:
     for row in f.readlines():  
        row.strip('\n')
        if not row.startswith("#"):
            spaces=row.split(',')
            RA.append(float(spaces[0]))
            DEC.append(float(spaces[1]))
list_a = RA
list_b = DEC
coord_list=zip(list_a, list_b)


#This is the most important part I think. Above is just reading my data in. 
#Basically what I tried to do was specify the center of the circle, then count 
#only the points a certain distance from that center. 

points=[]
[(x,y) for x,y in coord_list if x==3984.9634 and y==4146.6652]
if i in coord_list:
    d in 5664.85124
    points.append(i)

Image in Which I'm Trying to Count Pixels, Green Circular Region is Where I Want to Count

Upvotes: 3

Views: 212

Answers (2)

jpkotta
jpkotta

Reputation: 9417

Something like

points = []
xc,yc = 3984.9634, 4146.6652
r2 = 5664.85124**2 # radius squared
for x,y in zip(RA,DEC):
    if (x-xc)**2 + (y-yc)**2 < r2:
        points.append((x,y))

Upvotes: 2

Błotosmętek
Błotosmętek

Reputation: 12927

coord_list = []
with open('ChandraXraysources2.txt') as f:
     for row in f:
        if not row.startswith("#"):
            coord_list.append(map(float, row.split(',')))
points = [(x,y) for (x,y) in coord_list if (x-3984.9634)**2 + (y-4146.6652)**2 <= 5664.85124**2]

Upvotes: 1

Related Questions