예제 #1
0
  /**
   * Compute the angular separation between two vectors.
   *
   * <p>This method computes the angular separation between two vectors using the dot product for
   * well separated vectors and the cross product for almost aligned vectors. This allows to have a
   * good accuracy in all cases, even for vectors very close to each other.
   *
   * @param v1 first vector
   * @param v2 second vector
   * @return angular separation between v1 and v2
   * @exception ArithmeticException if either vector has a null norm
   */
  public static double angle(Vector3D v1, Vector3D v2) {

    double normProduct = v1.getNorm() * v2.getNorm();
    if (normProduct == 0) {
      throw MathRuntimeException.createArithmeticException(LocalizedFormats.ZERO_NORM);
    }

    double dot = dotProduct(v1, v2);
    double threshold = normProduct * 0.9999;
    if ((dot < -threshold) || (dot > threshold)) {
      // the vectors are almost aligned, compute using the sine
      Vector3D v3 = crossProduct(v1, v2);
      if (dot >= 0) {
        return FastMath.asin(v3.getNorm() / normProduct);
      }
      return FastMath.PI - FastMath.asin(v3.getNorm() / normProduct);
    }

    // the vectors are sufficiently separated to use the cosine
    return FastMath.acos(dot / normProduct);
  }
예제 #2
0
 /**
  * Get the elevation of the vector.
  *
  * @return elevation (&delta;) of the vector, between -&pi;/2 and +&pi;/2
  * @see #Vector3D(double, double)
  */
 public double getDelta() {
   return FastMath.asin(z / getNorm());
 }