Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / geom / Line.java
blobea00eae802bd298ee461c70875cfc7802790adc5
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.geom;
9 import gov.nasa.worldwind.*;
11 /**
12 * @author Tom Gaskins
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;
20 /**
21 * @param origin
22 * @param direction
23 * @throws IllegalArgumentException if <code>origin</code> is null, or <code>direction</code> is null or has zero
24 * length
26 public Line(Point origin, Point direction)
28 String message = null;
29 if (origin == 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";
35 if (message != null)
37 message = WorldWind.retrieveErrMsg(message);
38 WorldWind.logger().log(java.util.logging.Level.FINE, message);
39 throw new IllegalArgumentException(message);
42 this.origin = origin;
43 this.direction = direction;
46 public final Point getDirection()
48 return direction;
51 public final Point getOrigin()
53 return origin;
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);
66 /**
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
69 * considered equal.
71 * @param o the object to be compared against.
72 * @return true if these two objects are equal, false otherwise
74 @Override
75 public final boolean equals(Object o)
77 if (this == o)
78 return true;
79 if (o == null || getClass() != o.getClass())
80 return false;
82 final gov.nasa.worldwind.geom.Line line = (gov.nasa.worldwind.geom.Line) o;
84 if (!direction.equals(line.direction))
85 return false;
86 if (!line.origin.equals(origin))
87 return false;
89 return true;
92 @Override
93 public final int hashCode()
95 int result;
96 result = origin.hashCode();
97 result = 29 * result + direction.hashCode();
98 return result;
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
108 * positive distance.
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)
116 if (p == null)
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);