Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Area computation of polygon using coordinates in python

sum=0.0
b=input("Number of corners: ")
while b < 2:
    print "Invalid number of corners."
for i in range (b):
    xcoor=(i+1)
    X=input ("x-coordinate:")
    ycoor=(i+1)
    Y=input ("y-coordinate:")
    if i==0:
        x1=X 
        y1=Y
        xp=X
        yp=Y
elif i>=0:
    sum+=(xp*Y-yp*X)
xp=X
yp=Y

sum=sum+(X*y1-Y*x1)
area=(sum/2.0)
a=abs(area)
print "Area= %.1f" % (a)

the answer is always wrong. why? Thank you. I'm a newbie..I cant seem to find the area and sometimes it gives Area=0.0

When I try to run the code and enter coordinates as x and y, this happens:

Number of corners: 4
x-coordinate:2

y-coordinate:3

x-coordinate:4

y-coordinate:2

x-coordinate:5

y-coordinate:6

x-coordinate:7

y-coordinate:2

Area= 4.5. 

If I were to calculate the area manually, the result should be 18.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You don't correctely implemented the Shoelace formula. I change a little bit your code in order to fix it:

sum1=0.0
sum2=0.0
b=input("Number of corners: ")
matrix=[None]*(b+1);
while b < 2:
    print "Invalid number of corners."
for i in range (b):
    xcoor=(i+1)
    X=input ("x-coordinate:")
    ycoor=(i+1)
    Y=input ("y-coordinate:")
    if i==0:
        x1=X 
        y1=Y
        xp=X
        yp=Y

    matrix[i]=(X,Y)
    xp=X
    yp=Y



matrix[b]=(x1,y1);
print matrix
for i in range(len(matrix)-1):
    sum1 = sum1 + matrix[i][0]*matrix[i+1][1] ;
    #print str(matrix[i][0]) +'*'+str(matrix[i+1][1]) +'='+str(matrix[i][0]*matrix[i+1][1]);
for i in range(len(matrix)-1):
    sum2 = sum2 + matrix[i][1]*matrix[i+1][0] ;
    #print str(matrix[i][1]) +'*'+str(matrix[i+1][0]) +'='+str(matrix[i][1]*matrix[i+1][0]);
area=( abs(sum1-sum2)/2.0)
a=area
print "Area= %.1f" % (a)

But that's not all! If you test this code with the coordinate of the example in the wiki page, the software will give you the right Area. But if you test with your coordinates, it give your the same result. That's not only because your coordinates are all positives, but also because your polyghon is 'twisted'!

If you draw the polyghon ABCDA with:

A(2,3) B(4,2) C(5,6) D(7,2)

You obtain a "twisted" polyghon (two triangle with a common vertex on the intersection between BC and AD lines).

So, if you want to calculate either twisted polyghon area, you have to improve your code to calculate these polyghon types. Hope helped you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...