2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
7 package gov
.nasa
.worldwind
.geom
;
9 import gov
.nasa
.worldwind
.*;
13 * @version $Id: Line.java 1749 2007-05-06 19:48:14Z tgaskins $
15 public final class Line
// Instances are immutable
17 private final Point origin
;
18 private final Point direction
;
23 * @throws IllegalArgumentException if <code>origin</code> is null, or <code>direction</code> is null or has zero
26 public Line(Point origin
, Point direction
)
28 String message
= null;
30 message
= "nullValue.OriginIsNull";
31 else if (direction
== null)
32 message
= "nullValue.DirectionIsNull";
33 else if (direction
.length() <= 0)
34 message
= "geom.Line.DirectionIsZeroVector";
37 message
= WorldWind
.retrieveErrMsg(message
);
38 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
39 throw new IllegalArgumentException(message
);
43 this.direction
= direction
;
46 public final Point
getDirection()
51 public final Point
getOrigin()
56 public final Point
getPointAt(double t
)
58 return Point
.fromOriginAndDirection(t
, this.direction
, this.origin
);
61 public final double selfDot()
63 return this.origin
.dot(this.direction
);
67 * Performs a comparison to test whether this Object is internally identical to the other Object <code>o</code>.
68 * This method takes into account both direction and origin, so two lines which may be equivalent may not be
71 * @param o the object to be compared against.
72 * @return true if these two objects are equal, false otherwise
75 public final boolean equals(Object o
)
79 if (o
== null || getClass() != o
.getClass())
82 final gov
.nasa
.worldwind
.geom
.Line line
= (gov
.nasa
.worldwind
.geom
.Line
) o
;
84 if (!direction
.equals(line
.direction
))
86 if (!line
.origin
.equals(origin
))
93 public final int hashCode()
96 result
= origin
.hashCode();
97 result
= 29 * result
+ direction
.hashCode();
101 public String
toString()
103 return "Origin: " + this.origin
+ ", Direction: " + this.direction
;
107 * Calculate the shortests distance between this line and a specified <code>Point</code>. This method returns a
110 * @param p the <code>Point</code> whose distance from this <code>Line</code> will be calculated
111 * @return the distance between this <code>Line</code> and the specified <code>Point</code>
112 * @throws IllegalArgumentException if <code>p</code> is null
114 public final double distanceTo(Point p
)
118 String message
= WorldWind
.retrieveErrMsg("nullValue.PointIsNull");
119 WorldWind
.logger().log(java
.util
.logging
.Level
.FINE
, message
);
120 throw new IllegalArgumentException(message
);
123 Point origin
= this.getOrigin();
124 Point sideB
= origin
.subtract(p
); // really a vector
126 double distanceToOrigin
= sideB
.dot(this.getDirection());
127 double divisor
= distanceToOrigin
/ this.getDirection().selfDot();
129 Point sideA
= this.getDirection().multiply(divisor
);
131 double aSquared
= sideA
.selfDot();
132 double bSquared
= sideB
.selfDot();
134 return Math
.sqrt(bSquared
- aSquared
);