Okay I am writing a program that, in the end, will bring up a GUI window and ask the user what type of shape they want to make. The choices are line, circle, ellipse, rectangle, and polygon. When the user selects a shape they are then asked some key information about the shape they want to make.

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:

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. If someone could point out what I have done wrong that would be great. the code I am showing here is a modified version with just the polygon class and a main area where you can manipulate the polygon's dimensions.

``````import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class PolygonTest
{
// variables
int[] xPoints;
int[] yPoints;
int numOfPoints;
double area;
String name;

// constructor
public PolygonTest(String n, int[] xs, int[] ys, int p)
{
name = n;
xPoints = xs;
yPoints = ys;
numOfPoints = p;
area = 0;
}

public void paint(Graphics g)
{
g.drawPolygon(xPoints, yPoints, numOfPoints);
}

public void A3draw()
{
JFrame frame = new JFrame("This is a Polygon");
frame.setSize(500,500);
Polygon p = new Polygon(name, xPoints, yPoints, numOfPoints);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

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;
}

public static void main(String[] args)
{

// each point should have its x coord and y coord at the same index
// points should be entered in clockwise order
int[] xPts = {50,100,100,50};
int[] yPts = {50,50,100,100};
// name it whatever
String nameOfPolygon = "Imapolygon";

PolygonTest poly1 = new PolygonTest(nameOfPolygon, xPts, yPts, xPts.length);

poly1.A3draw();
JOptionPane.showMessageDialog(null, "The area of " + poly1.name + " is " + poly1.getMeasure() + " pixels.");

}
}``````

## All 6 Replies

``````for(int i = 0; i < numOfPoints-2; i++)
{
area += (xPoints[i]*yPoints[i+1])-(xPoints[i+1]*yPoints[i]);
}
area /= 2;``````

The formula tells you you need to range from 0 to the number of points - 1, not the number of points - 2. I imagine that vertice n is really vertice 0 and you're going through a cycle back to the first point (the formula calls for you to make calculations using vertice n, which doesn't exist). Regardless, if you follow the formula, you are going through one too few iterations.

Yea but the number of points is how long the array is and the last index in the array is one less then the number of points so I figured I need to go one down from that. Oh and I realized my program above may not compile because I forgot that I was using eclipse... this one should compile fine.....

``````import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class PolygonTest extends JPanel
{
// variables
int[] xPoints;
int[] yPoints;
int numOfPoints;
double area;
String name;

// constructor
public PolygonTest(String n, int[] xs, int[] ys, int p)
{
name = n;
xPoints = xs;
yPoints = ys;
numOfPoints = p;
area = 0;
}

public void paint(Graphics g)
{
g.drawPolygon(xPoints, yPoints, numOfPoints);
}

public void A3draw()
{
JFrame frame = new JFrame("This is a Polygon");
frame.setSize(500,500);
PolygonTest p = new PolygonTest(name, xPoints, yPoints, numOfPoints);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

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;
}

public static void main(String[] args)
{

// each point should have its x coord and y coord at the same index
// points should be entered in clockwise order
int[] xPts = {50,100,100,50};
int[] yPts = {50,50,100,100};
// name it whatever
String nameOfPolygon = "Imapolygon";

PolygonTest poly1 = new PolygonTest(nameOfPolygon, xPts, yPts, xPts.length);

poly1.A3draw();
JOptionPane.showMessageDialog(null, "The area of " + poly1.name + " is " + poly1.getMeasure() + " pixels.");

}
}``````

Yea but the number of points is how long the array is and the last index in the array is one less then the number of points so I figured I need to go one down from that. Oh and I realized my program above may not compile because I forgot that I was using eclipse... this one should compile fine.....

O.K., what's the question then? Does it work? I imagine it doesn't since it seems to me that you aren't following the formula.

``````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;
}``````

The red needs to be numPoints-1, right? You'll need to deal with the potential problem of the index going out of range, but it's your job as the programmer to make the code implement the mathematical formula. If you change the formula, you're going to get the wrong answer, I would imagine.

Okay I understand the formula says n-1 as the last point... the formula says to stop at the second to last point (n-1)... the last point in this case is numOfPoints-1 because that is the last index. so the second to last point should be numOfPoints-2... is this logic faulty? numOfPoints-1 would be using the last index as "i" and since we have to have "i+1" this will bring up a null pointer exception

Okay I understand the formula says n-1 as the last point... the formula says to stop at the second to last point (n-1)... the last point in this case is numOfPoints-1 because that is the last index. so the second to last point should be numOfPoints-2... is this logic faulty? numOfPoints-1 would be using the last index as "i" and since we have to have "i+1" this will bring up a null pointer exception

Make some graphs and plug in some numbers and see. Do it on paper with an easy shape that you know the answer to (i.e. a square with side length 1). If it doesn't work for all the polygons on paper, it won't work when you program it.

Area = (1/2)[n-1 Σ i=0](Xi*Yi+1 - Xi+1*Yi)

You are adding n terms together here, so you need a loop that has n iterations. If you get a null pointer exception, you can't just change the formula so that you won't get one. You need to change the program so it follows the math formula and doesn't result in a null pointer exception, which means you must understand the formula first before you start to program.

Yup you were right. I didn't realize that XnYn == XoYo. I was not counting the 1st point as the last point, so I was always one point short according to the formula. Oh and here is the final code if you were wondering:

``````public double getMeasure()
{
for(int i = 0; i <= numOfPoints-1; i++)
{
if(i == numOfPoints-1)
{
area += (xPoints[i]*yPoints[0])-(xPoints[0]*yPoints[i]);
}
else
{
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;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.