We help IT Professionals succeed at work.

CODE TO CALCULATE THE AREA OF A POLYGON

sanjeevgogi
sanjeevgogi asked
on
Medium Priority
581 Views
Last Modified: 2008-03-03
hi experts,
I want to calculate the area of a polygon.  I am assuming the polygon is in the first quadrant.So the coordinates of all the vertices are positive.The number of
vertices is to be supplied by the end user.The values for the coordinates are also to be supplied by the user.( I guess we could use a prompt box/text box....I dunno)The vertices are to be read in a clockwise direction.How do I accept the inputs (in this case the vertices of the polygon in (x,y) fashion and also the number of vertices, a positive integer greater than 2)and store them in variables and loop them to calcluate the area? I do not need any graphical output whatsoever. Just the area.Also I need the solution as soon as possible.
thanks
Comment
Watch Question

Commented:
only 100 points won't do it, you know.

Commented:
<SCRIPT>      
number=          prompt("enter number of x y coordinates")

var n=0

x=new Array()
y=new Array()
          for(;n<number;n++)
          {
          x[n]= prompt("enter x coord #" + (n+1));
          y[n]= prompt("enter y coord #" + (n+1));          
       }
           x[x.length]=x[0]
           y[y.length]=y[0]


               
area=0              
         for(i=0;i<number;i++)
{      

 
       j=(i+1);                                                                      
            area = area + ( x[i]*y[j] );                                            


            area = area - ( x[j]*y[i] );
                                                                 
}
 area=area/2;      
 alert(area)    


</SCRIPT>

Commented:
not to say I wouldn't accept more points :)


be sure to enter the points in a counter clockwise direction.

i.e. a square (x,y) area 16 would be

0,0 ; 4,0 ; 4,4 ; 0,4

a traditional pentagon area 20 would be

0,0 ; 4,0 ; 4,4 ; 2,6 ; 0,4


hope that makes sense.



Commented:
oh , upon reading your question again, I see you want to read the variables in a CLOCKWISE DIRECTION


here is that code:


 <SCRIPT>
     
number=          prompt("enter number of x y coordinates")

var n=0

x=new Array()
y=new Array()
          for(;n<number;n++)
          {
          x[n]= prompt("enter x coord #" + (n+1));
          y[n]= prompt("enter y coord #" + (n+1));          
       }
           x[x.length]=x[0]
           y[y.length]=y[0]


               
area=0              
         for(i=0;i<number;i++)
{      

 
       j=(i+1);                                                                      
            area = area + ( x[j]*y[i] );                                            


            area = area - ( x[i]*y[j] );
                                                                 
}
 area=area/2;      
 alert(area)    


</SCRIPT>

Author

Commented:
hi bebonham
thanks a lot.The program works. Now I have an additional question ..sort of like part 2 of the same problem as before.Note I have increased the points ..:)
Assume the I find the area of some polygon.Now I need to scale down the polygon such that the area now becomes 1 square unit, and The output I need must be the new coordinates of the scaled down polygon.
thanks!

Commented:
whew, okay, I'll see what I can come up with :)
Commented:
sorry, I can't help you with that.

My mathematics aren't good enough, but if you have an equation for this, I will apply it.


Bob

Commented:
thanks,

if you ever find a formula, or equation for this, or care to explain to me how it is done mathmatically, I will try to help more,


Thanks for the points and good luck

Bob

Explore More ContentExplore courses, solutions, and other research materials related to this topic.