Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

python - Calculating centroid of a polygon

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].

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...