Class Area

  1 /*
  2  * Copyright (c) 2016 Vivid Solutions.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12 package org.locationtech.jts.algorithm;
 13  
 14 import org.locationtech.jts.geom.Coordinate;
 15 import org.locationtech.jts.geom.CoordinateSequence;
 16  
 17 /**
 18  * Functions for computing area.
 19  * 
 20  * @author Martin Davis
 21  *
 22  */
 23 public class Area {
 24  
 25   /**
 26    * Computes the area for a ring. 
 27    * 
 28    * @param ring the coordinates forming the ring
 29    * @return the area of the ring
 30    */
 31   public static double ofRing(Coordinate[] ring)
 32   {
 33     return Math.abs(ofRingSigned(ring));
 34   }
 35  
 36   /**
 37    * Computes the area for a ring. 
 38    * 
 39    * @param ring the coordinates forming the ring
 40    * @return the area of the ring
 41    */
 42   public static double ofRing(CoordinateSequence ring)
 43   {
 44     return Math.abs(ofRingSigned(ring));
 45   }
 46  
 47   /**
 48    * Computes the signed area for a ring. The signed area is positive if the
 49    * ring is oriented CW, negative if the ring is oriented CCW, and zero if the
 50    * ring is degenerate or flat.
 51    * 
 52    * @param ring
 53    *          the coordinates forming the ring
 54    * @return the signed area of the ring
 55    */
 56   public static double ofRingSigned(Coordinate[] ring)
 57   {
 58     if (ring.length < 3)
 59       return 0.0;
 60     double sum = 0.0;
 61     /**
 62      * Based on the Shoelace formula.
 63      * http://en.wikipedia.org/wiki/Shoelace_formula
 64      */
 65     double x0 = ring[0].x;
 66     for (int i = 1; i < ring.length - 1; i++) {
 67       double x = ring[i].x - x0;
 68       double y1 = ring[i + 1].y;
 69       double y2 = ring[i - 1].y;
 70       sum += x * (y2 - y1);
 71     }
 72     return sum / 2.0;
 73   }
 74  
 75   /**
 76    * Computes the signed area for a ring. The signed area is:
 77    * <ul>
 78    * <li>positive if the ring is oriented CW
 79    * <li>negative if the ring is oriented CCW
 80    * <li>zero if the ring is degenerate or flat
 81    * </ul>
 82    * 
 83    * @param ring
 84    *          the coordinates forming the ring
 85    * @return the signed area of the ring
 86    */
 87   public static double ofRingSigned(CoordinateSequence ring)
 88   {
 89     int n = ring.size();
 90     if (n < 3)
 91       return 0.0;
 92     /**
 93      * Based on the Shoelace formula.
 94      * http://en.wikipedia.org/wiki/Shoelace_formula
 95      */
 96     Coordinate p0 = new Coordinate();
 97     Coordinate p1 = new Coordinate();
 98     Coordinate p2 = new Coordinate();
 99     ring.getCoordinate(0, p1);
100     ring.getCoordinate(1, p2);
101     double x0 = p1.x;
102     p2.x -= x0;
103     double sum = 0.0;
104     for (int i = 1; i < n - 1; i++) {
105       p0.y = p1.y;
106       p1.x = p2.x;
107       p1.y = p2.y;
108       ring.getCoordinate(i + 1, p2);
109       p2.x -= x0;
110       sum += p1.x * (p0.y - p2.y);
111     }
112     return sum / 2.0;
113   }
114  
115 }
116