for instance if they select "circle" it asks "what is the center point's X coordinate"... "what is the Y coordinate"... "what is the radius".... then it prints the circle out and a pop up window says the area of the shape they just made.
The problem I am having is calculating the area of a polygon. I have no problem with any of the user input and the shape prints out just fine but the area for the polygon is usually only correct if it is a square or triangle. Most of the time the area that is output is either double the actual or half of the actual. I am trying to use the formula from the wikipedia page right now and that is(I can't post images or links yet sry):
Area = (1/2)[n-1 Σ i=0](Xi*Yi+1 - Xi+1*Yi)
just go to wikipedia.org and search "Polygon Area" and it's the 1st formula
I tell the user to input each point in clockwise order which is how you are supposed to for this formula to work. Anyway here is the formula I am using right now. If someone could point out what I have done wrong that would be great.
public double getMeasure()
{
for(int i = 0; i < numOfPoints-2; i++)
{
area += (xPoints[i]*yPoints[i+1])-(xPoints[i+1]*yPoints[i]);
}
area /= 2;
//if they enter points counterclockwise the area will be negative but correct
if(area < 0)
area *= -1;
return area;
}
Note: "area" is initialized in the constructor and set at 0


Sign In
Create Account


Back to top









