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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.lucene.spatial.geometry.shape;
24 * <p><font color="red"><b>NOTE:</b> This API is still in
25 * flux and might change in incompatible ways in the next
28 public class LineSegment {
29 public final Point2D A = new Point2D();
30 public final Point2D B = new Point2D();
32 public LineSegment() {
37 public LineSegment(Point2D p1, Point2D p2) {
43 * Finds the distance of a specified point from the line segment and the
44 * closest point on the segment to the specified point.
49 * (Return) Closest point on the segment to c.
51 * @return Returns the distance from P to the closest point on the segment.
53 public double distance(Point2D P, Point2D /* out */closestPt) {
54 if (closestPt == null)
55 closestPt = new Point2D();
57 // Construct vector v (AB) and w (AP)
58 Vector2D v = new Vector2D(A, B);
59 Vector2D w = new Vector2D(A, P);
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
71 // Get the denominator of the component. If the component >= 1
72 // (d <= n) then point B is the closest point
76 return new Vector2D(B, P).norm();
79 // Closest point is along the segment. The point is the projection of
81 closestPt.set(v.mult(n / d));
83 return new Vector2D(closestPt, P).norm();
87 public int hashCode() {
90 result = prime * result + ((A == null) ? 0 : A.hashCode());
91 result = prime * result + ((B == null) ? 0 : B.hashCode());
96 public boolean equals(Object obj) {
101 if (getClass() != obj.getClass())
103 LineSegment other = (LineSegment) obj;
107 } else if (!A.equals(other.A))
112 } else if (!B.equals(other.B))