find the area of a regular polygon circumscribed around a circle in rust

To find the area of a regular polygon circumscribed around a circle, we can use the formula:

Area = 0.5 * Perimeter * apothem

Where, Perimeter is the perimeter of the polygon and apothem is the distance from the center of the polygon to the midpoint of a side of the polygon.

We can calculate the perimeter of the regular polygon as P = n * s, where n is the number of sides and s is the length of each side.

The apothem can be calculated as a = r * cos(pi/n), where r is the radius of the circle that circumscribes the polygon.

Here's the Rust code to calculate the area of a regular polygon circumscribed around a circle:

main.rs
fn area_of_regular_polygon_circumscribed_around_circle(n: i32, r: f64) -> f64 {
    let pi = std::f64::consts::PI;
    let s = 2.0 * r * (pi / n as f64).sin();
    let p = n as f64 * s;
    let a = r * (pi / n as f64).cos();
    0.5 * p * a
}

fn main() {
    let n = 6; // number of sides
    let r = 5.0; // radius of the circle
    let area = area_of_regular_polygon_circumscribed_around_circle(n, r);
    println!("Area of regular polygon with {} sides and circumscribed around a circle with radius {} = {}", n, r, area);
}
528 chars
15 lines

In this code, we first calculate the length of each side s using the formula 2r * sin(pi/n). Then, we calculate the perimeter p of the polygon using n * s. Finally, we calculate the apothem a using r * cos(pi/n). We then substitute these values in the formula for the area and return the result.

gistlibby LogSnag