user3542864
user3542864

Reputation: 11

Area of overlapping circles

I have the following Python code to generate random circles in order to simulate Brownian motion. I need to find the total area of the small red circles so that I can compare it to the total area of a larger blue circle. Since the circles are generated randomly, many of them overlap making it difficult to find the area. I have read many other responses related to this question about pixel painting, etc. What is the best way to find the area of these circles? I do not want to modify the generation of the circles, I just need to find the total area of the red circles on the plot.

The code to generate the circles I need is as follows (Python v. 2.7.6):

import matplotlib.pyplot as plt
import numpy as np

new_line = []
new_angle = []
x_c = [0]
y_c = [0]
x_real = []
y_real = []
xy_dist = []
circ = []   
range_value = 101

for x in range(0,range_value):
    mu, sigma = 0, 1
    new_line = np.random.normal(mu, sigma, 1)
    new_angle = np.random.uniform(0, 360)*np.pi/180
    x_c.append(new_line*np.cos(new_angle))
    y_c.append(new_line*np.sin(new_angle))

x_real = np.cumsum(x_c)
y_real = np.cumsum(y_c)
a = np.mean(x_real)
b = np.mean(y_real)

i = 0
while i<=range_value:
    xy_dist.append(np.sqrt((x_real[i]-a)**2+(y_real[i]-b)**2))
    i += 1

circ_rad = max(xy_dist)
small_rad = 0.2

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
circ1 = plt.Circle((a,b), radius=circ_rad+small_rad, color='b')
ax.add_patch(circ1)

j = 0
while j<=range_value:
    circ = plt.Circle((x_real[j], y_real[j]), radius=small_rad, color='r', fill=True)
    ax.add_patch(circ)
    j += 1

plt.axis('auto')
plt.show()

Upvotes: 1

Views: 4031

Answers (2)

DWilches
DWilches

Reputation: 23015

I can think of an easy way to do it thought the result will have inaccuracies:

With Python draw all your circles on a white image, filling the circles as you draw them. At the end each "pixel" of your image will have one of 2 colors: white color is the background and the other color (let's say red) means that pixel is occupied by a circle.

You then need to sum the number of red pixels and multiply them by the scale with which you draw them. You will have then the area.

This is inaccurate as there is no way of drawing a circle using square pixels, so in the mapping you lose accuracy. Keep in mind that the bigger you draw the circles, the smaller the inaccuracy becomes.

Upvotes: 0

Related Questions