servo / euclid

Geometry primitives (basic linear algebra) for Rust
Other
462 stars 103 forks source link

Poor accuracy in Vector2D::angle_from_x_axis #497

Open willhansen opened 1 year ago

willhansen commented 1 year ago

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

nical commented 1 year ago

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.