Пример #1
0
  /**
   * get the angle between this and other
   *
   * @param other
   * @return
   */
  public double getAngleBetween(Vector other) {
    // make sure this and other share a point
    // and that point is P1
    if (!this.getP1().equals(other.getP1())) {
      // see if they share any point
      if (other.getP2().equals(this.getP1())) {
        // reverse other
        other = other.reverse();
      } else {
        throw new IllegalArgumentException("Vectors do not meet up");
      }
    }

    double dotProduct = this.dot(other);
    double thisMag = this.getMagnitude();
    double otherMag = other.getMagnitude();

    return Math.acos(dotProduct / (thisMag * otherMag));

    //		Vector thisUnit = this.getUnitVector();
    //		Vector otherUnit = other.getUnitVector();

    //		return Math.atan2(otherUnit.getY2(), otherUnit.getX2()) - Math.atan2(thisUnit.getY2(),
    // thisUnit.getX2());
    //		return Math.acos(arg0)
  }
Пример #2
0
 public Vector add(Vector addVector) {
   // basically, we can do this graphically
   // move the addVector so that it starts at our end
   // then make a vector from our start to the end of the other vector
   Vector movedVect = addVector.moveTo(this.getP2());
   return new Vector(this.getP1(), movedVect.getP2());
 }