~ubuntu-branches/ubuntu/natty/jts/natty

« back to all changes in this revision

Viewing changes to src/com/vividsolutions/jts/geom/CoordinateArrays.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2005-08-07 14:12:35 UTC
  • Revision ID: james.westby@ubuntu.com-20050807141235-7hy3ll3xpq79djcb
Tags: upstream-1.6
ImportĀ upstreamĀ versionĀ 1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
/*
 
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.
 
7
 *
 
8
 * Copyright (C) 2001 Vivid Solutions
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
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
 
23
 *
 
24
 * For more information, contact:
 
25
 *
 
26
 *     Vivid Solutions
 
27
 *     Suite #1A
 
28
 *     2328 Government Street
 
29
 *     Victoria BC  V8T 5G5
 
30
 *     Canada
 
31
 *
 
32
 *     (250)385-6040
 
33
 *     www.vividsolutions.com
 
34
 */
 
35
package com.vividsolutions.jts.geom;
 
36
 
 
37
import java.util.List;
 
38
 
 
39
/**
 
40
 * Useful utility functions for handling Coordinate arrays
 
41
 *
 
42
 * @version 1.6
 
43
 */
 
44
public class CoordinateArrays {
 
45
 
 
46
  private final static Coordinate[] coordArrayType = new Coordinate[0];
 
47
 
 
48
  /**
 
49
   * Creates a deep copy of the argument {@link Coordinate) array.
 
50
   *
 
51
   * @param coordinates an array of Coordinates
 
52
   * @return a deep copy of the input
 
53
   */
 
54
  public static Coordinate[] copyDeep(Coordinate[] coordinates) {
 
55
    Coordinate[] copy = new Coordinate[coordinates.length];
 
56
    for (int i = 0; i < coordinates.length; i++) {
 
57
      copy[i] = new Coordinate(coordinates[i]);
 
58
    }
 
59
    return copy;
 
60
  }
 
61
 
 
62
  /**
 
63
   * Converts the given List of Coordinates into a Coordinate array.
 
64
   */
 
65
  public static Coordinate[] toCoordinateArray(List coordList)
 
66
  {
 
67
    return (Coordinate[]) coordList.toArray(coordArrayType);
 
68
  }
 
69
 
 
70
  /**
 
71
   * Returns whether #equals returns true for any two consecutive Coordinates
 
72
   * in the given array.
 
73
   */
 
74
  public static boolean hasRepeatedPoints(Coordinate[] coord)
 
75
  {
 
76
    for (int i = 1; i < coord.length; i++) {
 
77
      if (coord[i - 1].equals(coord[i]) ) {
 
78
        return true;
 
79
      }
 
80
    }
 
81
    return false;
 
82
  }
 
83
 
 
84
  /**
 
85
   * Returns either the given coordinate array if its length is greater than the
 
86
   * given amount, or an empty coordinate array.
 
87
   */
 
88
  public static Coordinate[] atLeastNCoordinatesOrNothing(int n, Coordinate[] c) {
 
89
      return c.length >= n ? c : new Coordinate[] {  };
 
90
  }
 
91
 
 
92
  /**
 
93
   * If the coordinate array argument has repeated points,
 
94
   * constructs a new array containing no repeated points.
 
95
   * Otherwise, returns the argument.
 
96
   * @see #hasRepeatedPoints(Coordinate[])
 
97
   */
 
98
  public static Coordinate[] removeRepeatedPoints(Coordinate[] coord)
 
99
  {
 
100
    if (! hasRepeatedPoints(coord)) return coord;
 
101
    CoordinateList coordList = new CoordinateList(coord, false);
 
102
    return coordList.toCoordinateArray();
 
103
  }
 
104
 
 
105
  /**
 
106
   * Reverses the coordinates in an array in-place.
 
107
   */
 
108
  public static void reverse(Coordinate[] coord)
 
109
  {
 
110
    int last = coord.length - 1;
 
111
    int mid = last / 2;
 
112
    for (int i = 0; i <= mid; i++) {
 
113
      Coordinate tmp = coord[i];
 
114
      coord[i] = coord[last - i];
 
115
      coord[last - i] = tmp;
 
116
    }
 
117
  }
 
118
 
 
119
  /**
 
120
   * Returns true if the two arrays are identical, both null, or pointwise
 
121
   * equal (as compared using Coordinate#equals)
 
122
   * @see Coordinate#equals(Object)
 
123
   */
 
124
  public static boolean equals(
 
125
    Coordinate[] coord1,
 
126
    Coordinate[] coord2)
 
127
  {
 
128
    if (coord1 == coord2) return true;
 
129
    if (coord1 == null || coord2 == null) return false;
 
130
    if (coord1.length != coord2.length) return false;
 
131
    for (int i = 0; i < coord1.length; i++) {
 
132
      if (! coord1[i].equals(coord2[i])) return false;
 
133
    }
 
134
    return true;
 
135
  }
 
136
 
 
137
  /**
 
138
   *  Returns the minimum coordinate, using the usual lexicographic comparison.
 
139
   *
 
140
   *@param  coordinates  the array to search
 
141
   *@return              the minimum coordinate in the array, found using <code>compareTo</code>
 
142
   *@see Coordinate#compareTo(Object)
 
143
   */
 
144
  public static Coordinate minCoordinate(Coordinate[] coordinates)
 
145
  {
 
146
    Coordinate minCoord = null;
 
147
    for (int i = 0; i < coordinates.length; i++) {
 
148
      if (minCoord == null || minCoord.compareTo(coordinates[i]) > 0) {
 
149
        minCoord = coordinates[i];
 
150
      }
 
151
    }
 
152
    return minCoord;
 
153
  }
 
154
  /**
 
155
   *  Shifts the positions of the coordinates until <code>firstCoordinate</code>
 
156
   *  is first.
 
157
   *
 
158
   *@param  coordinates      the array to rearrange
 
159
   *@param  firstCoordinate  the coordinate to make first
 
160
   */
 
161
  public static void scroll(Coordinate[] coordinates, Coordinate firstCoordinate) {
 
162
    int i = indexOf(firstCoordinate, coordinates);
 
163
    if (i < 0) return;
 
164
    Coordinate[] newCoordinates = new Coordinate[coordinates.length];
 
165
    System.arraycopy(coordinates, i, newCoordinates, 0, coordinates.length - i);
 
166
    System.arraycopy(coordinates, 0, newCoordinates, coordinates.length - i, i);
 
167
    System.arraycopy(newCoordinates, 0, coordinates, 0, coordinates.length);
 
168
  }
 
169
 
 
170
  /**
 
171
   *  Returns the index of <code>coordinate</code> in <code>coordinates</code>.
 
172
   *  The first position is 0; the second, 1; etc.
 
173
   *
 
174
   *@param  coordinate   the <code>Coordinate</code> to search for
 
175
   *@param  coordinates  the array to search
 
176
   *@return              the position of <code>coordinate</code>, or -1 if it is
 
177
   *      not found
 
178
   */
 
179
  public static int indexOf(Coordinate coordinate, Coordinate[] coordinates) {
 
180
    for (int i = 0; i < coordinates.length; i++) {
 
181
      if (coordinate.equals(coordinates[i])) {
 
182
        return i;
 
183
      }
 
184
    }
 
185
    return -1;
 
186
  }
 
187
 
 
188
}