Created
March 4, 2012 20:52
-
-
Save davish/1974736 to your computer and use it in GitHub Desktop.
Find the center of a 2D polygon or line with the coordinates of it's vertices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
verts = 0 | |
def getInput(): | |
""" Gets the number of vertices from the user """ | |
global verts | |
print "How many vertices?" | |
verts = input() | |
listInputs(verts) | |
def listInputs(vertices): | |
""" Finds out the x and y coords of the vertices together """ | |
# Initiate coords lists | |
xcoords = [] | |
ycoords = [] | |
for x in range(0, vertices): | |
# Loop through each vertex to get the x coord. | |
print 'Please enter %(number)d x coord.' % {"number": x + 1} | |
xcoord = input() | |
xcoords.append(xcoord) | |
print xcoords # FDB | |
for y in range(0, vertices): | |
# Loop through each vertex to get the y coord | |
print 'Please enter %(number)d y coord.' % {"number": y + 1} | |
ycoord = input() | |
ycoords.append(ycoord) | |
print ycoords # FDB | |
addInputs(xcoords, ycoords) | |
def addInputs(coordsx, coordsy): | |
""" Adds x coords and y coords by looping through lists""" | |
xsum = 0 | |
ysum = 0 | |
# Initiate sum variables | |
for x in range(0, len(coordsx)): | |
xsum += coordsx[x] | |
print xsum # For Debug | |
for y in range(0, len(coordsy)): | |
ysum += coordsy[y] | |
print ysum # For Debug | |
divInputs(xsum, ysum) | |
def divInputs(sumx, sumy): | |
""" Divides the sums of the coordinates by the number of vertices | |
and displays them. """ | |
global verts | |
divx = sumx / verts | |
divy = sumy / verts | |
print 'The location of the center is %(xdiv)d , %(ydiv)d'% {"xdiv": divx, "ydiv": divy} | |
if __name__ == "__main__": | |
getInput() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment