Polygon Area

I need to calculate the area of ​​a two-dimensional polygon. (any shape, any size, etc.) I only have a list of points, each point contains X and Y.

Polygons are in the 2D map of the block, therefore: Normal polygon
But, because I have to use blocks / rectangles, then the polygon looks something like this: Blocks polygon
So you need to calculate this: Area of ​​polygon
The block is in the area only if more than 50% of the block is in the polygon OR is the angle / point of this polygon (for example, this lever at the bottom of the image).

Can it be calculated? without getting the minimum and maximum points and checking each individual block ...
I found only the code for normal polygons:

public int getArea(List<BlockVector2D> blockPoints)
{
    double result = 0;
    int j = blockPoints.size() - 1;

    for (int i = 0; i < blockPoints.size(); ++i)
    {
        result += (blockPoints.get(j).getBlockX() + blockPoints.get(i).getBlockX()) * (blockPoints.get(j).getBlockZ() - blockPoints.get(i).getBlockZ());
        j = i;
    }
    return (int) Math.abs(result / 2);
}

But I do not know how to do this using block points ...


... .

+4
2

, .. ( , , ):

, . , , :

area = 0;
foreach(point p : points) {
    area += testIfInsidePolygon(p);
}

.

, , , . :

:)

, O (n ^ 2), . .

0

Source: https://habr.com/ru/post/1536968/


All Articles