Thoughts on Irregular Shapes and The Monte Carlo Method

Introduction

Counting points inside and outside an irregular shapes is a problem. How can the counting process be automated to avoid tedious hand counting?

The algorithm below is a possible solution. (I make no pretense it is original.)

My Algorithm

Assumptions

The algorithm (pseudo code)

# generate random points and count edge crossings inside_point_count = 0 # inside irregular shape total_point_count = 0 # inside encompassing shape max_point_count = (large number) ibbox = [edges of irregular shape's bounding box] ishape = [edges of irregular shape] eshape_center = [center of encompassing shape] loop: if total_point_count > max_point_count: break point = generate_random_point("in the encompassing shape") total_point_count += 1 if the random_point is outside ibbox: continue line = (line from random_point to eshape_center) crossing_count = (line crossings of the ishape edges) if the eshape_center is inside ishape: if crossing_count is an even number: # including zero inside_point_count += 1 else: if line crossing_count is an odd number: inside_point_count += 1