1

I am trying to calculate the area of a particular polygon.

Let me explain better. I am trying to learn how to deal with polygons in Python. I know there are available packages like Shapely, but I would like to write the code myself using more common packages like numpy, scipy or math.

I am currently trying this exercise: Given some vertices coordinates and one radius measure, calculate the area of the polygon you obtain. The area is formed by: (area of the circle with center in one given point and radius given) - (area of the polygon formed by the given vertices in clockwise).

Let's see some examples:

  1. I have a circle and the given vertices form a polygon which does not intersect the circle. The wanted area is just the area of the circle: area = areaCircle (easy task - done).

  2. I have a circle and the given vertices form a polygon which does intersect the circle at some point. The radius is like a stick (rigid, its measure doesn't change overtime). I should calculate area = areaCircle - areaPolygon (medium task).

  3. I have a circle and the given vertices form a polygon which does intersect the circle at some point. The radius is like a rope (flexible). When the two polygons intersect, the radius varies (so the circle won't be perfect). Here's a photo to understand better what happens. As you can see, the center of the circle is (5,5) and it has radius ~ 4. When the radius-rope hits the polygon in (4, 7) and (8, 4), it deforms the circle. So, the area I am trying to obtain should be: area = areaDeformedCircle - areaPolygon (hard task).

Here's, so far, my code:

from math import pi

# parser for input
def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)

input_parser = parser()

def get_word():
    global input_parser
    return next(input_parser)

def get_number():
    data = get_word()
    try:
        return int(data)
    except ValueError:
        return float(data)

# input data: 
X,Y,L = map(int,input().split()) # circle data: X_coord_center, Y_coord_center, radius
N = get_number() # number of vertices of the polygon
vertexes = []
for i in range(N): #vertices of the polygon, given clockwise
    xy = (get_number(), get_number())
    vertexes.append(xy)

def circleArea(radius):
    return pi * radius ** 2

# from question https://stackoverflow.com/questions/24467972/calculate-area-of-polygon-given-x-y-coordinates
def polyArea(coords):
    t=0
    for count in range(len(coords)-1):
        y = coords[count+1][1] + coords[count][1]
        x = coords[count+1][0] - coords[count][0]
        z = y * x
        t += z
    return abs(t/2.0)

# area polygon
print("area polygon: ", polyArea(vertexes))

# area circle
print("area circle:  ", circleArea(L))

The thing is, I am only able to do point 1) between those (calculate a circle's radius, no big deal). in fact, given these input examples, I am able to calculate the correct area only when the polygon does not intersect the circle.

example where the circle does not intersect the polygon (answerArea = 3.14159)
3 3 1
4
3 5
6 7
8 5
7 2

example where the circle intersects the polygon (answerArea = 36.71737) - it's the image's data
5 5 4
4
4 7
7 9
9 7
8 4

How should I modify my code (NOT using Shapely) in order to calculate one of the other two points? I don't have a teacher, I'm learning all by myself and online hints didn't help me much.

Thanks in advise to who will help and happy coding to everyone :)

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
hellomynameisA
  • 546
  • 1
  • 7
  • 28
  • You might be interested in the [shoelace formula](https://en.wikipedia.org/wiki/Shoelace_formula) to calculate the area of a polygon (this also works for concave polygons, but not for self-intersecting ones). – JohanC Aug 03 '20 at 16:24
  • Thanks, @JohanC, I tried to implement it as ```polyArea``` function. Do you think it is all right? Or can you suggest another implementation of it? – hellomynameisA Aug 18 '20 at 17:15

0 Answers0