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.algorithm;
034:
035: import com.vividsolutions.jts.geom.*;
036:
037: /**
038: * Contains a pair of points and the distance between them.
039: * Provides methods to update with a new point pair with
040: * either maximum or minimum distance.
041: */
042: public class PointPairDistance {
043:
044: private Coordinate[] pt = { new Coordinate(), new Coordinate() };
045: private double distance = Double.NaN;
046: private boolean isNull = true;
047:
048: public PointPairDistance() {
049: }
050:
051: public void initialize() {
052: isNull = true;
053: }
054:
055: public void initialize(Coordinate p0, Coordinate p1) {
056: pt[0].setCoordinate(p0);
057: pt[1].setCoordinate(p1);
058: distance = p0.distance(p1);
059: isNull = false;
060: }
061:
062: /**
063: * Initializes the points, avoiding recomputing the distance.
064: * @param p0
065: * @param p1
066: * @param distance the distance between p0 and p1
067: */
068: private void initialize(Coordinate p0, Coordinate p1,
069: double distance) {
070: pt[0].setCoordinate(p0);
071: pt[1].setCoordinate(p1);
072: this .distance = distance;
073: isNull = false;
074: }
075:
076: public double getDistance() {
077: return distance;
078: }
079:
080: public Coordinate[] getCoordinates() {
081: return pt;
082: }
083:
084: public Coordinate getCoordinate(int i) {
085: return pt[i];
086: }
087:
088: public void setMaximum(PointPairDistance ptDist) {
089: setMaximum(ptDist.pt[0], ptDist.pt[1]);
090: }
091:
092: public void setMaximum(Coordinate p0, Coordinate p1) {
093: if (isNull) {
094: initialize(p0, p1);
095: return;
096: }
097: double dist = p0.distance(p1);
098: if (dist > distance)
099: initialize(p0, p1, dist);
100: }
101:
102: public void setMinimum(PointPairDistance ptDist) {
103: setMinimum(ptDist.pt[0], ptDist.pt[1]);
104: }
105:
106: public void setMinimum(Coordinate p0, Coordinate p1) {
107: if (isNull) {
108: initialize(p0, p1);
109: return;
110: }
111: double dist = p0.distance(p1);
112: if (dist < distance)
113: initialize(p0, p1, dist);
114: }
115: }
|