How to calculate the area of a java.awt.geom.Area?

后端 未结 4 454
清酒与你
清酒与你 2020-12-11 15:47

I am looking for a way to calculate the area, in pixels, of an arbitrary instance of java.awt.geom.Area.

The background: I have Shapes in m

4条回答
  •  余生分开走
    2020-12-11 16:46

    To find the area of a polygon using the following snippet:

    int sum = 0;
    for (int i = 0; i < n -1; i++)
    {
        sum = sum + x[i]*y[i+1] - y[i]*x[i+1];
    }
    // (sum / 2) is your area.
    System.out.println("The area is : " + (sum / 2));
    

    Here n is the total number of vertices and x[i] and y[i] are the x and y coordinates of a vertex i. Note that for this algorithm to work, the polygon must be closed. It doesent work on open polygons.

    You can find mathematical alogrithms related to polygons here. You need to convert it to code yourself:)

提交回复
热议问题