Jump to content

Area of a polygon...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
smoore

smoore

    Newbie

  • Members
  • PipPip
  • 17 posts
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(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

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I don't see any obvious errors. Can you give a couple examples of input/output you are getting? I can check the results manually and maybe get some insight.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
smoore

smoore

    Newbie

  • Members
  • PipPip
  • 17 posts

WingedPanther said:

I don't see any obvious errors. Can you give a couple examples of input/output you are getting? I can check the results manually and maybe get some insight.

I modified my program and set up this executable one. Just change the stuff in the main area to get new polygons. As you can see it draws them fine it just doesn't always get the area right.


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.add(p);

		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.");

		

	}

}



Still new to programming in general so sorry if the code is a little messy.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'm having issues getting it to compile... probably my setup.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
smoore

smoore

    Newbie

  • Members
  • PipPip
  • 17 posts
sorry about that I am using eclipse and that messes me up sometime.... this should work:

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.add(p);
		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.");
		
	}
}



#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
OK, I can compile it now, but it throws an exception when I try to run it:
java PolygonTest.class
Exception in thread "main" java.lang.NoClassDefFoundError: PolygonTest/class

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
JerrySpock

JerrySpock

    Newbie

  • Members
  • Pip
  • 9 posts
Looking at this section of the code...
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;
}

I see your loop goes until i <= numOfPoints-2. Looking at the equation for a polygon, it looks like we're suppose to go to numOfPoints-1. So if we have a polygon with 6 sides, there would be 5 points. We would add the result of the equation 5 times, for values 0, 1, 2, 3, and 4. So we should go till i < numOfPoints, not including numOfPoints. Could this be the problem?

Edited by JerrySpock, 28 February 2009 - 04:25 PM.
more thoughts


#8
smoore

smoore

    Newbie

  • Members
  • PipPip
  • 17 posts
the equation is supposed to go till one less than the last index and the last index is numOfPoints-1 so one less than that should be numOfPoints-2..... numOfPoints-1 gets a null pointer exception

#9
smoore

smoore

    Newbie

  • Members
  • PipPip
  • 17 posts
Okay figured out where I went wrong.... didn't read this little bit of information on the wikipedia page..."To close the polygon, the first and last vertices are the same, i.e., xn,yn = x0,y0" so I was neglecting to include the 1st point 2 times in the formula. If you care to take a look at the final code here it is:

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