Class PointLocation

  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 import org.locationtech.jts.geom.Location;
 17  
 18 /**
 19  * Functions for locating points within basic geometric
 20  * structures such as lines and rings.
 21  * 
 22  * @author Martin Davis
 23  *
 24  */
 25 public class PointLocation {
 26  
 27   /**
 28    * Tests whether a point lies on the line defined by a list of
 29    * coordinates.
 30    * 
 31    * @param p the point to test
 32    * @param line the line coordinates
 33    * @return true if the point is a vertex of the line or lies in the interior
 34    *         of a line segment in the line
 35    */
 36   public static boolean isOnLine(Coordinate p, Coordinate[] line)
 37   {
 38     LineIntersector lineIntersector = new RobustLineIntersector();
 39     for (int i = 1; i < line.length; i++) {
 40       Coordinate p0 = line[i - 1];
 41       Coordinate p1 = line[i];
 42       lineIntersector.computeIntersection(p, p0, p1);
 43       if (lineIntersector.hasIntersection()) {
 44         return true;
 45       }
 46     }
 47     return false;
 48   }
 49  
 50   /**
 51    * Tests whether a point lies on the line defined by a 
 52    * {@link CoordinateSequence}.
 53    * 
 54    * @param p the point to test
 55    * @param line the line coordinates
 56    * @return true if the point is a vertex of the line or lies in the interior
 57    *         of a line segment in the line
 58    */
 59   public static boolean isOnLine(Coordinate p, CoordinateSequence line)
 60   {
 61     LineIntersector lineIntersector = new RobustLineIntersector();
 62     Coordinate p0 = new Coordinate();
 63     Coordinate p1 = new Coordinate();
 64     int n = line.size();
 65     for (int i = 1; i < n; i++) {
 66       line.getCoordinate(i-1, p0);
 67       line.getCoordinate(i, p1);
 68       lineIntersector.computeIntersection(p, p0, p1);
 69       if (lineIntersector.hasIntersection()) {
 70         return true;
 71       }
 72     }
 73     return false;
 74   }
 75  
 76   /**
 77    * Tests whether a point lies inside or on a ring. The ring may be oriented in
 78    * either direction. A point lying exactly on the ring boundary is considered
 79    * to be inside the ring.
 80    * <p>
 81    * This method does <i>not</i> first check the point against the envelope of
 82    * the ring.
 83    * 
 84    * @param p
 85    *          point to check for ring inclusion
 86    * @param ring
 87    *          an array of coordinates representing the ring (which must have
 88    *          first point identical to last point)
 89    * @return true if p is inside ring
 90    * 
 91    * @see locatePointInRing
 92    */
 93   public static boolean isInRing(Coordinate p, Coordinate[] ring)
 94   {
 95     return PointLocation.locateInRing(p, ring) != Location.EXTERIOR;
 96   }
 97  
 98   /**
 99    * Determines whether a point lies in the interior, on the boundary, or in the
100    * exterior of a ring. The ring may be oriented in either direction.
101    * <p>
102    * This method does <i>not</i> first check the point against the envelope of
103    * the ring.
104    * 
105    * @param p
106    *          point to check for ring inclusion
107    * @param ring
108    *          an array of coordinates representing the ring (which must have
109    *          first point identical to last point)
110    * @return the {@link Location} of p relative to the ring
111    */
112   public static int locateInRing(Coordinate p, Coordinate[] ring)
113   {
114     return RayCrossingCounter.locatePointInRing(p, ring);
115   }
116  
117 }
118