Skip to content

consider use of atan2 to compute angle between vectors #249

@godmar

Description

@godmar

A small note on something I observed here.

In 2D, you can compute the angle between 2 vectors numerically robustly by taking the atan2 of the cross and dot product, as in:

  private static double getAngleBetweenVectorsAtan2(Translation2d u, Translation2d v) {
    double dot_product = u.toVector().dot(v.toVector());

    return Math.atan2(cross(u, v), dot_product);
  }
  public static double cross(Translation2d a, Translation2d b) {
    return a.getX() * b.getY() - a.getY() * b.getX();
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions