Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8357631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:32:25+00:00 2026-06-09T10:32:25+00:00

I was recently using the Java Area class to wrap up a Rectangle2D.Double type.

  • 0

I was recently using the Java Area class to wrap up a Rectangle2D.Double type. So that I can do manipulations like intersect, add, etc. However, when it comes to the calculation of the area of the shape, I was getting a quite weird result. Below is the code I’m using to calculate the area of a shape:

private static double polyArea(ArrayList<Point2D.Double> pointList) {
    double area = 0;
    for (int loopi = 1; loopi < pointList.size(); loopi++) {
        Point2D.Double p1 = pointList.get(loopi - 1);
        Point2D.Double p2 = pointList.get(loopi);
        area += (p1.x * p2.y - p2.x * p1.y) / 2.0;
    }
    return area;
}

public  static double coverageArea(Shape s) {
    ArrayList<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
    double[] coords = new double[6];
    int type;
    double totalArea = 0;
    PathIterator it = s.getPathIterator(null);
    while (!it.isDone()) {
        type = it.currentSegment(coords);
        if (type == it.SEG_MOVETO) {
            pointList.clear();
            pointList.add(new Point2D.Double(coords[0], coords[1]));
        } else if (type == it.SEG_LINETO) {
            pointList.add(new Point2D.Double(coords[0], coords[1]));
        } else if (type == it.SEG_CLOSE) {
            totalArea += polyArea(pointList);
            pointList.clear();
        } else {
            System.out.println("calculateShapeArea: Cannot calculate area for shapes with segment type other than SEG_MOVETO, SEG_LINETO, or SEG_CLOSE.  Ignoring segment type=" + type);
        }
        it.next();
    }
    if (totalArea < 0) {
        totalArea = -totalArea;
    }
    return totalArea;
}

If I have a Rectangle2D r(1.0, 1.0, 6.0, 6.0), using the above code I will get the area correctly 36. However if I do a = new Area(r), then the result of coverageArea(a) is 39. Sometimes it could be tens of times larger than the correct answer.

Anyone knows why this is happening? Is there any problem with the area calculation? Any advice would be appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:32:27+00:00Added an answer on June 9, 2026 at 10:32 am

    According to this Wiki, your code is not implementing the method correctly. Your polyArea() method forget to close the polygon (it does not consider the line from the last to the first vertex).

    Also your version of the formula seems to have exchanged p1 and p2, although I’m not sure if thats a problem, I don’t personally understand how this method really works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently just created Java project using Eclipse that requires 2 JAR files
I've been recently trying to connect to a hosted MySQL using Java but can't
I have recently started using generics in java, and in that attempt tried to
I have recently created a web project in Java using eclipse. I have a
I'm developing my first java application using Eclipse. I've recently needed to adjust the
I am using Eclipse Helios for Java. Everything was working fine until recently. The
I've found myself recently using the SemaphoreSlim class to limit the work in progress
I recently came across some Java code that simply put some strings into a
I was using the java.net.URL.openStream() method to retrieve content from the server. I recently
I've just started using Java EE recently to build webservices. Most of these have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.