Algorithm to Calculate the area of a 2D polygon

Posted on 2006-03-29
Last Modified: 2013-12-26
I have two arrays, storing the x and y values of the polygon.

I wish to use these values to calculate the area of the polygon.

The polygon is a simple, concave 2d polygon.

Can someone possibly direct me to how I may achieve this.

Many Thanks.
Question by:stats786
    LVL 84

    Expert Comment


    Author Comment

    I tried implementing a quick test of what you suggested, but I don't get the values I expect. Can you spot what I'm doing wrong. Thanks.

    import  java.lang.Math.*;

    public class areaTest
          //Test Values
          //square - expected area = 8
          //static double[] polyX = { 1.0, 3.0, 3.0, 1.0, 1.0};
          //static double[] polyY = { 1.0, 1.0, 3.0, 3.0, 1.0};
          //triangle - expected area = 10.5
          static double[] polyX = { 1.0, 4.0, 1.0, 1.0};
          static double[] polyY = { 1.0, 4.0, 8.0, 1.0};
          public static void main (String args[])
                double test = calcArea(polyX, polyY);
                System.out.println( test );

          public static double calcArea(double xVals[],double yVals[])
                int i;
                double area = 0;
                double mag;
                      area = area + (xVals[i]*yVals[0])-(xVals[0]*yVals[i]);
                area = area/2;
                mag= Math.abs(area);

                return mag;


    LVL 84

    Accepted Solution

    area = area + (xVals[i]*yVals[(i+1)%xVals.length])-(xVals[(i+1)%xVals.length]*yVals[i]);

    Author Comment

    Thank you very much for that.

    Is'nt the formula you've implemented using the following rhetoric:


    LVL 84

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
    As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now