001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2005, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.referencing.operation.builder;
017:
018: import org.geotools.geometry.DirectPosition2D;
019: import org.opengis.geometry.DirectPosition;
020: import java.awt.geom.GeneralPath;
021: import java.awt.geom.Point2D;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: /**
027: * Simple polygons like three - sided (triangle) or four - sided
028: * (qadrilateral), that are used for triangulation.
029: *
030: * @since 2.4
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/referencing/operation/builder/Polygon.java $
032: * @version $Id: Polygon.java 28966 2008-01-27 17:36:43Z acuster $
033: * @author Jan Jezek
034: */
035: class Polygon {
036: /** Vertices of this polygon. */
037: private DirectPosition[] vertices;
038:
039: /**
040: * Creates a polygon using specified vertices.
041: *
042: * @param coordinates of vertices
043: */
044: Polygon(DirectPosition[] coordinates) {
045: this .vertices = coordinates;
046: }
047:
048: /**
049: * Sets the vertices of this polygon.
050: *
051: * @param coordinates
052: */
053: public void setCoordinates(DirectPosition[] coordinates) {
054: this .vertices = coordinates;
055: }
056:
057: /**
058: * Returns vertices of this polygon.
059: *
060: * @return vertices of this polygon.
061: */
062: public DirectPosition[] getPoints() {
063: return vertices;
064: }
065:
066: /**
067: * Returns the LINESTRING representation in WKT.
068: *
069: * @return WKT format.
070: */
071: public String toString() {
072: String wkt = "";
073:
074: for (int i = 0; i < vertices.length; i++) {
075: wkt = wkt + vertices[i].getCoordinates()[0] + " "
076: + vertices[i].getCoordinates()[1];
077:
078: if (i != (vertices.length - 1)) {
079: wkt = wkt + ", ";
080: }
081: }
082:
083: return "LINESTRING (" + wkt + ")";
084: }
085:
086: /**
087: * Generates GeneralPath from the array of points.
088: *
089: * @param points vertices of polygon.
090: *
091: * @return generated GeneralPath.
092: */
093: protected GeneralPath generateGeneralPath(DirectPosition[] points) {
094: GeneralPath ring = new GeneralPath();
095:
096: // Set the initiakl coordinates of the general Path
097: ring.moveTo((float) points[0].getCoordinates()[0],
098: (float) points[0].getCoordinates()[1]);
099:
100: // Create the star. Note: this does not draw the star.
101: for (int i = 1; i < points.length; i++) {
102: ring.lineTo((float) points[i].getCoordinates()[0],
103: (float) points[i].getCoordinates()[1]);
104: }
105:
106: return ring;
107: }
108:
109: /**
110: * Test whether the coordinate is inside or is vertex of ploygon.
111: *
112: * @param dp Point to be tested whether is inside of polygon.
113: *
114: * @return True if the point is inside (or is the vertex of polygon, false
115: * if not.
116: */
117: protected boolean containsOrIsVertex(DirectPosition dp) {
118: if (generateGeneralPath(vertices).contains((Point2D) dp)
119: || (hasVertex(dp))) {
120: return true;
121: }
122:
123: return false;
124: }
125:
126: /**
127: * Returns whether v is one of the vertices of this polygon.
128: *
129: * @param p the candidate point
130: *
131: * @return whether v is equal to one of the vertices of this Triangle
132: */
133: public boolean hasVertex(DirectPosition p) {
134: for (int i = 0; i < vertices.length; i++) {
135: if (p == vertices[i]) {
136: return true;
137: }
138: }
139:
140: return false;
141: }
142:
143: /**
144: * Enlarge the polygon using homothetic transformation method.
145: *
146: * @param scale of enlargement (when scale = 1 then polygon stays
147: * unchanged)
148: */
149: protected void enlarge(double scale) {
150: double sumX = 0;
151: double sumY = 0;
152:
153: for (int i = 0; i < vertices.length; i++) {
154: sumX = sumX + vertices[i].getCoordinates()[0];
155: sumY = sumY + vertices[i].getCoordinates()[1];
156: }
157:
158: // The center of polygon is calculated.
159: sumX = sumX / vertices.length;
160: sumY = sumY / vertices.length;
161:
162: // The homothetic transformation is made.
163: for (int i = 0; i < vertices.length; i++) {
164: vertices[i].getCoordinates()[0] = (scale * (vertices[i]
165: .getCoordinates()[0] - sumX))
166: + sumX;
167: vertices[i].getCoordinates()[1] = (scale * (vertices[i]
168: .getCoordinates()[1] - sumY))
169: + sumY;
170: }
171: }
172:
173: /**
174: * Returns reduced coordinates of vertices so the first vertex has
175: * [0,0] coordinats.
176: *
177: * @return The List of reduced vertives
178: */
179: protected List reduce() {
180: //Coordinate[] redCoords = new Coordinate[coordinates.length];
181: ArrayList redCoords = new ArrayList();
182:
183: for (int i = 0; i < vertices.length; i++) {
184: redCoords.add(new DirectPosition2D(vertices[i]
185: .getCoordinateReferenceSystem(), vertices[i]
186: .getCoordinates()[0]
187: - vertices[0].getCoordinates()[0], vertices[i]
188: .getCoordinates()[1]
189: - vertices[0].getCoordinates()[1]));
190: }
191:
192: return redCoords;
193: }
194:
195: /**
196: * Returns whether this Polygon contains all of the the given
197: * coordinates.
198: *
199: * @param coordinate of coordinates
200: *
201: * @return whether this Polygon contains the all of the given coordinates
202: */
203: protected boolean containsAll(List coordinate) {
204: for (Iterator i = coordinate.iterator(); i.hasNext();) {
205: if (!this .containsOrIsVertex((DirectPosition) i.next())) {
206: return false;
207: }
208: }
209:
210: return true;
211: }
212:
213: /**
214: * Returns a copy of this.
215: *
216: * @return copy of this object.
217: */
218: public Object clone() {
219: return new Polygon(vertices);
220: }
221: }
|