add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / geometry / shape / LineSegment.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.spatial.geometry.shape;
19
20
21 /**
22  * 2d line segment.
23  *
24  * <p><font color="red"><b>NOTE:</b> This API is still in
25  * flux and might change in incompatible ways in the next
26  * release.</font>
27  */
28 public class LineSegment {
29   public final Point2D A = new Point2D();
30   public final Point2D B = new Point2D();
31
32   public LineSegment() {
33     A.set(0, 0);
34     B.set(0, 0);
35   }
36
37   public LineSegment(Point2D p1, Point2D p2) {
38     A.set(p1);
39     B.set(p2);
40   }
41
42   /**
43    * Finds the distance of a specified point from the line segment and the
44    * closest point on the segment to the specified point.
45    * 
46    * @param P
47    *            Test point.
48    * @param closestPt
49    *            (Return) Closest point on the segment to c.
50    * 
51    * @return Returns the distance from P to the closest point on the segment.
52    */
53   public double distance(Point2D P, Point2D /* out */closestPt) {
54     if (closestPt == null)
55       closestPt = new Point2D();
56
57     // Construct vector v (AB) and w (AP)
58     Vector2D v = new Vector2D(A, B);
59     Vector2D w = new Vector2D(A, P);
60
61     // Numerator of the component of w onto v. If <= 0 then A
62     // is the closest point. By separating into the numerator
63     // and denominator of the component we avoid a division unless
64     // it is necessary.
65     double n = w.dot(v);
66     if (n <= 0.0f) {
67       closestPt.set(A);
68       return w.norm();
69     }
70
71     // Get the denominator of the component. If the component >= 1
72     // (d <= n) then point B is the closest point
73     double d = v.dot(v);
74     if (d <= n) {
75       closestPt.set(B);
76       return new Vector2D(B, P).norm();
77     }
78
79     // Closest point is along the segment. The point is the projection of
80     // w onto v.
81     closestPt.set(v.mult(n / d));
82     closestPt.add(A);
83     return new Vector2D(closestPt, P).norm();
84   }
85
86   @Override
87   public int hashCode() {
88     final int prime = 31;
89     int result = 1;
90     result = prime * result + ((A == null) ? 0 : A.hashCode());
91     result = prime * result + ((B == null) ? 0 : B.hashCode());
92     return result;
93   }
94
95   @Override
96   public boolean equals(Object obj) {
97     if (this == obj)
98       return true;
99     if (obj == null)
100       return false;
101     if (getClass() != obj.getClass())
102       return false;
103     LineSegment other = (LineSegment) obj;
104     if (A == null) {
105       if (other.A != null)
106         return false;
107     } else if (!A.equals(other.A))
108       return false;
109     if (B == null) {
110       if (other.B != null)
111         return false;
112     } else if (!B.equals(other.B))
113       return false;
114     return true;
115   }
116   
117 }