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!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…