/** * Get the square of the simple cartesian distance between this and another * vector. */ public double getDistanceSquared (Vector other) { return subtract(other).vectorLengthSquared(); }
public static Vector zeroVector (int size) { double[] coords = new double[size]; for (int i=0; i<size; ++i) coords[i] = 0.0; return new Vector(coords); }
/** * Get the square of the cartesian length of this vector. */ public double vectorLengthSquared () { return this.dot(this); }
public Vector cross (Vector v) { if (3 != size() || 3 != v.size()) throw new IllegalArgumentException("Attempt to take the cross product of non-3-vectors"); double xa = coord(0); double ya = coord(1); double za = coord(2); double xb = v.coord(0); double yb = v.coord(1); double zb = v.coord(2); return new Vector(ya*zb-za*yb, za*xb-xa*zb, xa*yb-ya*xb); }
@Override protected Position interpolate (Position start, Position end, double t) { Vector sV = start.getAsCartesian(); Vector eV = end.getAsCartesian(); Vector cartesianResult = sV.scale(1 - t).add(eV.scale(t)); Position p = new Position(cartesianResult.coord(0), cartesianResult.coord(1), cartesianResult.coord(2), !start.hasElevation()); p.setPrecision(start.getPrecision()); return p; }
/** * Get the cartesian length of this vector. */ public double vectorLength () { return Math.sqrt(vectorLengthSquared()); }
private double anythingIfNotNaN (double testValue) { return ifNotNaN(testValue, 1.0); } private double ifNotNaN (double testValue, double value) {
public double getPrecision () { if (_baseFormIsPolar) return _polar.getPrecision(); else return _cartesian.getPrecision(); }
public void setPrecision (double precision) { if (null != _polar) _polar.setPrecision(precision); if (null != _cartesian) _cartesian.setPrecision(precision); }
/** * Calculate a scaled version of this vector. * * @param scale The scale to apply. * @return A new vector whose value is a scaled version of this vector. */ public Vector scale (double scale) { int len = size(); double[] coords = new double[len]; for (int i=0; i<len; ++i) coords[i] = _data[i]*scale; return new Vector(coords); }
/** * {@inheritDoc} */ public double getDistanceSquared (Vector v) { return subtract(v).vectorLengthSquared(); }
public static Vector zeroVector (int size) { double[] coords = new double[size]; for (int i=0; i<size; ++i) coords[i] = 0.0; return new Vector(coords); }
@Override public int hashCode () { int len = size(); int hash = len; for (int i=0; i<len; ++i) { hash = hash*71+(int) Math.round(_data[i]*73); } return hash; }
public double vectorLengthSquared () { return this.dot(this); }
private double anythingIfNotNaN (double testValue) { return ifNotNaN(testValue, 1.0); } private double ifNotNaN (double testValue, double value) {
public double getPrecision () { if (_baseFormIsPolar) return _polar.getPrecision(); else return _cartesian.getPrecision(); }
public void setPrecision (double precision) { if (null != _polar) _polar.setPrecision(precision); if (null != _cartesian) _cartesian.setPrecision(precision); }
public static Vector toUnitVector (double theta, double phi) { double sphi = Math.sin(phi); return new Vector(Math.cos(theta) * sphi, Math.sin(theta) * sphi, Math.cos(phi)); } private static double thetaFromUnitVector (Vector v) {