euclid icon indicating copy to clipboard operation
euclid copied to clipboard

Poor accuracy in Vector2D::angle_from_x_axis

Open willhansen opened this issue 2 years ago • 1 comments

I expected this to be a lot closer to 45 degrees

default::Vector2D::new(0.5, 0.5).angle_from_x_axis().to_degrees() = 44.98835851188444

willhansen avatar Feb 16 '23 04:02 willhansen

angle_from_x_axis is implemented on top of a faster but less precise atan2.

    let v = Vector2D::new(0.5, 0.5);
    let a1: f32 = v.angle_from_x_axis().radians; // 0.785195
    let a2 = v.y.atan2(v.x);                     // 0.7853982
    let a3 = std::f32::consts::PI / 4.0;         // 0.7853982
    let error = (a1 - a3).abs()                  // 0.00020319223

It would maybe make sense for the function to be slower and more precise by default and provide fast_angle_from_x_axis on the side.

nical avatar Feb 17 '23 09:02 nical