Find area of a polygon

The following java method calculates the sum of a polygon given a list of the points in either clockwise or counter clockwise order:

private double calculateArea(List<Point> orderedPoints) {
		//multiply each x coordinate by the following y
		int positiveDiagonalSum = 0;
		for (int i = 0; i<orderedPoints.size(); i++){
			if (i == orderedPoints.size() - 1){
				positiveDiagonalSum += orderedPoints.get(i).x * orderedPoints.get(0).y;
			} else {
				positiveDiagonalSum += orderedPoints.get(i).x * orderedPoints.get(i+1).y;
			}
		}
		
		//multiply each y coordinate by the following x
		int negativeDiaglonalSum = 0;
		for (int i = 0; i<orderedPoints.size(); i++){
			if (i == orderedPoints.size() - 1){
				negativeDiaglonalSum += orderedPoints.get(i).y * orderedPoints.get(0).x;
			} else {
				negativeDiaglonalSum += orderedPoints.get(i).y * orderedPoints.get(i+1).x;
			}
		}
		
		//subtract second sum from first, divide by 2
		double area = Math.abs((positiveDiagonalSum - negativeDiaglonalSum) / 2.0);
		
		return area;
	}

Leave a Reply