4
* The JTS Topology Suite is a collection of Java classes that
5
* implement the fundamental operations required to validate a given
6
* geo-spatial data set to a known topological specification.
8
* Copyright (C) 2001 Vivid Solutions
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public
12
* License as published by the Free Software Foundation; either
13
* version 2.1 of the License, or (at your option) any later version.
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Lesser General Public License for more details.
20
* You should have received a copy of the GNU Lesser General Public
21
* License along with this library; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
* For more information, contact:
28
* 2328 Government Street
33
* www.vividsolutions.com
35
package com.vividsolutions.jts.index.chain;
38
import com.vividsolutions.jts.geom.Coordinate;
39
import com.vividsolutions.jts.geomgraph.Quadrant;
42
* A MonotoneChainBuilder implements functions to determine the monotone chains
43
* in a sequence of points.
47
public class MonotoneChainBuilder {
49
public static int[] toIntArray(List list)
51
int[] array = new int[list.size()];
52
for (int i = 0; i < array.length; i++) {
53
array[i] = ((Integer) list.get(i)).intValue();
58
public static List getChains(Coordinate[] pts)
60
return getChains(pts, null);
64
* Return a list of the {@link MonotoneChain}s
65
* for the given list of coordinates.
67
public static List getChains(Coordinate[] pts, Object context)
69
List mcList = new ArrayList();
70
int[] startIndex = getChainStartIndices(pts);
71
for (int i = 0; i < startIndex.length - 1; i++) {
72
MonotoneChain mc = new MonotoneChain(pts, startIndex[i], startIndex[i + 1], context);
79
* Return an array containing lists of start/end indexes of the monotone chains
80
* for the given list of coordinates.
81
* The last entry in the array points to the end point of the point array,
82
* for use as a sentinel.
84
public static int[] getChainStartIndices(Coordinate[] pts)
86
// find the startpoint (and endpoints) of all monotone chains in this edge
88
List startIndexList = new ArrayList();
89
startIndexList.add(new Integer(start));
91
int last = findChainEnd(pts, start);
92
startIndexList.add(new Integer(last));
94
} while (start < pts.length - 1);
95
// copy list to an array of ints, for efficiency
96
int[] startIndex = toIntArray(startIndexList);
101
* @return the index of the last point in the monotone chain starting at <code>start</code>.
103
private static int findChainEnd(Coordinate[] pts, int start)
105
// determine quadrant for chain
106
int chainQuad = Quadrant.quadrant(pts[start], pts[start + 1]);
107
int last = start + 1;
108
while (last < pts.length) {
109
// compute quadrant for next possible segment in chain
110
int quad = Quadrant.quadrant(pts[last - 1], pts[last]);
111
if (quad != chainQuad) break;
118
public MonotoneChainBuilder() {