Using numpy to find area of polygon

Viewed 72

I have been reading through some python code and have come to this function for the polygon area using numpy.

def polygon_area(self, x,y):
        correction = x[-1] * y[0] - y[-1]* x[0]
        main_area = np.dot(x[:-1], y[1:]) - np.dot(y[:-1], x[1:])
        return 0.5*np.abs(main_area + correction)

However, I don't quite understand it. Can anyone explain to me why there is a correction of the x and y coordinates and what exactly the function is doing?

1 Answers

This is a calculation of the area of a polygon using the shoelace formula. The correction is just the term not calculated in the main_area shortcut.

A lot more maths than python or numpy for that matter.

Related