Task: Shoelace formula for polygonal area





Java: import java.util.List;

public class ShoelaceFormula {
    private static class Point {
        int x, y;

        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return String.format("(%d, %d)", x, y);
        }
    }

    private static double shoelaceArea(List<Point> v) {
        int n = v.size();
        double a = 0.0;
        for (int i = 0; i < n - 1; i++) {
            a += v.get(i).x * v.get(i + 1).y - v.get(i + 1).x * v.get(i).y;
        }
        return Math.abs(a + v.get(n - 1).x * v.get(0).y - v.get(0).x * v.get(n - 1).y) / 2.0;
    }

    public static void main(String[] args) {
        List<Point> v = List.of(
            new Point(3, 4),
            new Point(5, 11),
            new Point(12, 8),
            new Point(9, 5),
            new Point(5, 6)
        );
        double area = shoelaceArea(v);
        System.out.printf("Given a polygon with vertices %s,%n", v);
        System.out.printf("its area is %f,%n", area);
    }
}






Fortran:       DOUBLE PRECISION FUNCTION AREA(N,P)	
C   Uses the mid-point rule for integration. Consider the line joining (x1,y1) to (x2,y2)
C The area under that line (down to the x-axis) is the y-span midpoint (y1 + y2)/2 times the width (x2 - x1)
C This is the trapezoidal rule for a single interval, and follows from simple geometry.
C Now consider a sequence of such points heading in the +x direction: each successive interval's area is positive.
C Follow with a sequence of points heading in the -x direction, back to the first point: their areas are all negative.
C The resulting sum is the area below the +x sequence and above the -x sequence: the area of the polygon.
C   The point sequence can wobble as it wishes and can meet the other side, but it must not cross itself
c as would be done in a figure 8 drawn with a crossover instead of a meeting.
C   A clockwise traversal (as for an island) gives a positive area; use anti-clockwise for a lake.
       INTEGER N		
       DOUBLE COMPLEX P(N)	
       DOUBLE COMPLEX PP,PC	
       DOUBLE COMPLEX W		
       DOUBLE PRECISION A	
       INTEGER I		
        IF (N.LT.3) STOP "Area: at least three points are needed
        W = (P(1) + P(N/3) + P(2*N/3))/3	
        W = SUM(P(1:N) - W)/N + W	
        A = 0			
        PC = P(N) - W		
        DO I = 1,N		
          PP = PC			
          PC = P(I) - W			
          A = (DIMAG(PC) + DIMAG(PP))*(DBLE(PC) - DBLE(PP)) + A	
        END DO			
        AREA = A/2		
      END FUNCTION AREA		

      DOUBLE PRECISION FUNCTION AREASL(N,P)	
       INTEGER N		
       DOUBLE COMPLEX P(N)	
       DOUBLE PRECISION A	
        A = SUM(DBLE(P(1:N - 1)*DIMAG(P(2:N)))) + DBLE(P(N))*DIMAG(P(1))
     1    - SUM(DBLE(P(2:N)*DIMAG(P(1:N - 1)))) - DBLE(P(1))*DIMAG(P(N))
        AREASL = A/2		
      END FUNCTION AREASL	

      INTEGER ENUFF
      DOUBLE PRECISION AREA,AREASL	
      DOUBLE PRECISION A1,A2		
      PARAMETER (ENUFF = 5)		
      DOUBLE COMPLEX POINT(ENUFF)	
      DATA POINT/(3D0,4D0),(5D0,11D0),(12D0,8D0),(9D0,5D0),(5D0,6D0)/	

      WRITE (6,*) POINT
      A1 = AREA(5,POINT)
      A2 = AREASL(5,POINT)
      WRITE (6,*) "A=",A1,A2
      END