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

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 1
    What makes you think this is the correct way to calculate area for a closed polygon? Are you trying to do contour integration using Stokes theorem? – duffymo Feb 12 '15 at 13:01
  • I dont know what stokes' theorem is, but I was told that I can use this simple code to compute the area of a polygon. – Joseph G. Arcos Feb 12 '15 at 13:16
  • You should know what Stokes theorem is: converts an area integral to a contour integral. Does your hand calculation work out correctly? – duffymo Feb 12 '15 at 13:23
  • I tried to calculate the area manually, but my answer is always different and correct. (I suppose I should study Stokes' theorem. But I'd like to know why this code doesnt work at all). Thank you very much. – Joseph G. Arcos Feb 12 '15 at 13:26
  • Please fix your indentation and give and example that returns something other than the desired answer. – unutbu Feb 12 '15 at 13:30
  • You might also want to look at the [`area_of_polygon` function shown here](http://stackoverflow.com/a/4682656/190597). See also [this SO question](http://stackoverflow.com/q/451426/190597). – unutbu Feb 12 '15 at 13:31
  • It's always good to try your code on some well known test cases before you attempt something harder. Can you do triangle, square, and rectangle? No sense proceeding if you can't do those. – duffymo Feb 12 '15 at 13:35
  • The formula you are using computes the *signed* area of the polygon. Holes, created by self-intersecting sides, [contribute negative area](http://www.mathpages.com/home/kmath201/kmath201.htm). See [this SO question](http://stackoverflow.com/q/10049454/190597) and also [this page](http://geomalgorithms.com/a09-_intersect-3.html) for how to compute the visible area of a self-intersecting polygon. This idea is to decompose the non-simple polygon into simple pieces. – unutbu Feb 12 '15 at 15:24

1 Answers1

1

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!

vathek
  • 531
  • 2
  • 9