sanjeevgogi
asked on
CODE TO CALCULATE THE AREA OF A POLYGON
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
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
only 100 points won't do it, you know.
<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>
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>
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.
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.
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>
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>
ASKER
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!
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!
whew, okay, I'll see what I can come up with :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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