The polygons can be regular or irregular. For example,
The following idea is the easiest way to compute its area (signed) for any given simple 2-D polygons, even for concave ones.
1. Compute the value for every edge segment of the polygon.
2. Sum them up:
The C# code shown following clearly illustrates the idea.
1 2 3 4 5 6 7 8 9 10 11 | public static double area(double[] x, double[] y, int n) { double area = 0.0; for (int i = 1; i < n; i++) { area += (x[i] - x[i - 1]) * (y[i] + y[i - 1]) * 0.5; } area += (x[0] - x[n - 1]) * (y[0] + y[n - 1]) * 0.5; return area; } |
public static double area(double[] x, double[] y, int n) { double area = 0.0; for (int i = 1; i < n; i++) { area += (x[i] - x[i - 1]) * (y[i] + y[i - 1]) * 0.5; } area += (x[0] - x[n - 1]) * (y[0] + y[n - 1]) * 0.5; return area; }
Let’s try to compute the following polygon.
We loop each edge and compute the sum of the difference of width multiply the average height.
The compute process is like following.
For more information, you might visit this page.
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
347 wordsloading...
Last Post: Codeforces: 236A. Boy or Girl ?
Next Post: Algorithm Complexity