In my program I'm using a Polygon
class I crated. The class contains two lists one for x coordinates, and one for y coordinates. I want to write a method to the class that will calculate and return the centroid of the Polygon
for n amount of vertices. So far what I managed to write this:
def centroid(self):
x_value = [p for p in self.__x]
y_value = [p for p in self.__y]
centroid = [sum(x_value) / len(self.__x), sum(y_value) / len(self.__x)]
return centroid
For example, an input of x = Polygon([-10.4, 30.4, 60.2], [-10.4, 30.5, 10.2])
should result in:
[26.733333333333334, 10.1].
And for an input of x = Polygon([-10.4, 30.4, 60.2, 40.01], [-10.4, 30.5, 10.2, 20.3])
,
the result should be:
[22.794181612615812, 14.280993501698099].
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…