Trigonometry

Calculate the Side Length of a Triangle

std cat-science

Calculates the length of the hypotenuse of a right-angle triangle with an angle of 2 radians and opposite side length of 80.

/// Calculates the hypotenuse of a right triangle given an angle and the length
/// of the side opposite to it.
fn main() {
    // The angle in radians.
    let angle: f64 = 2.0;
    // The length of the side opposite to the angle.
    let side_length = 80.0;

    // Calculate the hypotenuse using the sine function.
    let hypotenuse = side_length / angle.sin();

    println!("Hypotenuse: {}", hypotenuse);
}

Verify that tan is Equal to sin Divided by cos

std cat-science

Verifies tan(x) is equal to sin(x)/cos(x) for x = 6.

/// This example demonstrates that the tangent of a number is equal to the sine
/// of the number divided by the cosine of the number.
fn main() {
    // Define a number.
    let x: f64 = 6.0;

    // Calculate the tangent of the number.
    let a = x.tan();
    // Calculate the sine of the number divided by the cosine of the number.
    let b = x.sin() / x.cos();

    println!("a: {a}, b: {b}");
    assert_eq!(a, b);
}

Calculate the Distance Between two Points on Earth

std

By default, Rust provides mathematical float methods⮳ such as trigonometric functions, square root, conversion functions between radians and degrees, and so forth.

The following example computes the distance in kilometers between two points on the Earth with the Haversine⮳ formula. Points are expressed as pairs of latitude and longitude in degrees. Then, to_radians⮳ converts them in radians. sincospowi⮳ and sqrt⮳ compute the central angle. Finally, it's possible to calculate the distance.

/// Calculates the distance between two points on the Earth's surface using the
/// Haversine formula.
///
/// This example demonstrates how to calculate the distance between Paris and
/// London based on their latitude and longitude coordinates.
fn main() {
    // Earth's radius in kilometers.
    let earth_radius_kilometer = 6371.0_f64;
    // Latitude and longitude of Paris in degrees.
    let (paris_latitude_degrees, paris_longitude_degrees) =
        (48.85341_f64, -2.34880_f64);
    // Latitude and longitude of London in degrees.
    let (london_latitude_degrees, london_longitude_degrees) =
        (51.50853_f64, -0.12574_f64);

    // Convert latitude from degrees to radians.
    let paris_latitude = paris_latitude_degrees.to_radians();
    let london_latitude = london_latitude_degrees.to_radians();

    let delta_latitude =
        (paris_latitude_degrees - london_latitude_degrees).to_radians();
    let delta_longitude =
        (paris_longitude_degrees - london_longitude_degrees).to_radians();

    // Haversine formula:
    // a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
    // c = 2 ⋅ atan2( √a, √(1−a) ).
    let central_angle_inner = (delta_latitude / 2.0).sin().powi(2)
        + paris_latitude.cos()
            * london_latitude.cos()
            * (delta_longitude / 2.0).sin().powi(2);
    let central_angle = 2.0 * central_angle_inner.sqrt().asin();

    // Distance = radius * central angle.
    let distance = earth_radius_kilometer * central_angle;

    println!(
        "Distance between Paris and London on the surface of Earth is {:.1} kilometers",
        distance
    );
}