001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.geom;
034:
035: import java.awt.geom.Point2D;
036: import java.util.Collection;
037: import java.util.Iterator;
038:
039: import com.vividsolutions.jts.geom.Coordinate;
040: import com.vividsolutions.jts.util.Assert;
041: import com.vividsolutions.jump.util.MathUtil;
042:
043: /**
044: * Utility functions for working with Coordinates.
045: */
046: public class CoordUtil {
047: /**
048: * Returns the average of two Coordinates.
049: * @param c1 one coordinate
050: * @param c2 another coordinate
051: * @return a new Coordinate with the average x and average y
052: */
053: public static Coordinate average(Coordinate c1, Coordinate c2) {
054: return new Coordinate(MathUtil.avg(c1.x, c2.x), MathUtil.avg(
055: c1.y, c2.y));
056: }
057:
058: /**
059: * @param coordinates not empty
060: */
061: public static Coordinate average(Collection coordinates) {
062: Assert.isTrue(!coordinates.isEmpty());
063:
064: double xSum = 0;
065: double ySum = 0;
066:
067: for (Iterator i = coordinates.iterator(); i.hasNext();) {
068: Coordinate coordinate = (Coordinate) i.next();
069: xSum += coordinate.x;
070: ySum += coordinate.y;
071: }
072:
073: return new Coordinate(xSum / coordinates.size(), ySum
074: / coordinates.size());
075: }
076:
077: /**
078: * @param coordinates not empty
079: */
080: public static Coordinate closest(Collection coordinates,
081: Coordinate p) {
082: Assert.isTrue(!coordinates.isEmpty());
083:
084: Coordinate closest = (Coordinate) coordinates.iterator().next();
085:
086: for (Iterator i = coordinates.iterator(); i.hasNext();) {
087: Coordinate candidate = (Coordinate) i.next();
088:
089: if (p.distance(candidate) < p.distance(closest)) {
090: closest = candidate;
091: }
092: }
093:
094: return closest;
095: }
096:
097: /**
098: * Adds two coordinates.
099: * @param c1 the first coordinate
100: * @param c2 the second coordinate
101: * @return a new coordinate: c1 + c2
102: */
103: public static Coordinate add(Coordinate c1, Coordinate c2) {
104: return new Coordinate(c1.x + c2.x, c1.y + c2.y);
105: }
106:
107: /**
108: * Subtracts two coordinates.
109: * @param c1 the first coordinate
110: * @param c2 the second coordinate
111: * @return a new coordinate: c1 - c2
112: */
113: public static Coordinate subtract(Coordinate c1, Coordinate c2) {
114: return new Coordinate(c1.x - c2.x, c1.y - c2.y);
115: }
116:
117: /**
118: * Multiplies a scalar and a coordinate.
119: * @param d the scalar
120: * @param c the coordinate
121: * @return a new coordinate: d * c
122: */
123: public static Coordinate multiply(double d, Coordinate c) {
124: return new Coordinate(d * c.x, d * c.y);
125: }
126:
127: /**
128: * Divides a coordinate by a scalar.
129: * @param c the coordinate
130: * @param d the scalar *
131: * @return a new coordinate: c / d
132: */
133: public static Coordinate divide(Coordinate c, double d) {
134: return new Coordinate(c.x / d, c.y / d);
135: }
136:
137: public static Coordinate toCoordinate(Point2D point) {
138: return new Coordinate(point.getX(), point.getY());
139: }
140:
141: public static Point2D toPoint2D(Coordinate coordinate) {
142: return new Point2D.Double(coordinate.x, coordinate.y);
143: }
144:
145: public static Point2D add(Point2D a, Point2D b) {
146: return new Point2D.Double(a.getX() + b.getX(), a.getY()
147: + b.getY());
148: }
149:
150: public static Point2D subtract(Point2D a, Point2D b) {
151: return new Point2D.Double(a.getX() - b.getX(), a.getY()
152: - b.getY());
153: }
154: }
|