Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/main/java/com/thealgorithms/maths/AreaOfPolygon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* This class provides a method to calculate the area of a simple polygon
* using the Shoelace formula (Gauss's area formula).
*
* The vertices should be ordered either clockwise or counter-clockwise.
*/
public class AreaOfPolygon {

/**
* Calculates the area of a polygon using x and y coordinates.
* Uses the Shoelace formula:
* Area = (1/2) * |x[i]*y[i+1] - x[i+1]*y[i]|
*
* @param x x-coordinates of the polygon's vertices
* @param y y-coordinates of the polygon's vertices
* @return area of the polygon
* @throws IllegalArgumentException if x and y are different lengths
*/
public static double area(double[] x, double[] y) {
if (x.length != y.length) {
throw new IllegalArgumentException("Arrays must be same length");
}

double sum = 0.0;
int n = x.length;

for (int i = 0; i < n; i++) {
sum += (x[i] * y[(i + 1) % n]) - (x[(i + 1) % n] * y[i]);
}

return Math.abs(sum) / 2.0;
}

// Sample usage
public static void main(String[] args) {
double[] x = {0, 4, 4, 0};
double[] y = {0, 0, 4, 4};
System.out.println("Area: " + area(x, y)); // Should print 16.0
}
}
Loading