Class PointPairDistance

 1 /*
 2  * Copyright (c) 2016 Vivid Solutions.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 package org.locationtech.jts.operation.buffer.validate;
13  
14 import org.locationtech.jts.geom.Coordinate;
15  
16 /**
17  * Contains a pair of points and the distance between them.
18  * Provides methods to update with a new point pair with
19  * either maximum or minimum distance.
20  */
21 public class PointPairDistance {
22  
23   private Coordinate[] pt = { new Coordinate(), new Coordinate() };
24   private double distance = Double.NaN;
25   private boolean isNull = true;
26  
27   public PointPairDistance()
28   {
29   }
30  
31   public void initialize() { isNull = true; }
32  
33   public void initialize(Coordinate p0, Coordinate p1)
34   {
35     pt[0].setCoordinate(p0);
36     pt[1].setCoordinate(p1);
37     distance = p0.distance(p1);
38     isNull = false;
39   }
40  
41   /**
42    * Initializes the points, avoiding recomputing the distance.
43    * @param p0
44    * @param p1
45    * @param distance the distance between p0 and p1
46    */
47   private void initialize(Coordinate p0, Coordinate p1, double distance)
48   {
49     pt[0].setCoordinate(p0);
50     pt[1].setCoordinate(p1);
51     this.distance = distance;
52     isNull = false;
53   }
54  
55   public double getDistance() { return distance; }
56  
57   public Coordinate[] getCoordinates() { return pt; }
58  
59   public Coordinate getCoordinate(int i) { return pt[i]; }
60  
61   public void setMaximum(PointPairDistance ptDist)
62   {
63     setMaximum(ptDist.pt[0], ptDist.pt[1]);
64   }
65  
66   public void setMaximum(Coordinate p0, Coordinate p1)
67   {
68     if (isNull) {
69       initialize(p0, p1);
70       return;
71     }
72     double dist = p0.distance(p1);
73     if (dist > distance)
74       initialize(p0, p1, dist);
75   }
76  
77   public void setMinimum(PointPairDistance ptDist)
78   {
79     setMinimum(ptDist.pt[0], ptDist.pt[1]);
80   }
81  
82   public void setMinimum(Coordinate p0, Coordinate p1)
83   {
84     if (isNull) {
85       initialize(p0, p1);
86       return;
87     }
88     double dist = p0.distance(p1);
89     if (dist < distance)
90       initialize(p0, p1, dist);
91   }
92 }
93