find the surface area of a regular polygon circumscribed around a cone in rust

Here's a Rust function that calculates the surface area of a regular polygon circumscribed around a cone:

main.rs
fn polygon_surface_area(radius: f64, sides: u32, slant_height: f64) -> f64 {
    let apothem = radius * (std::f64::consts::PI / sides as f64).tan(); // apothem of polygon
    let base_perimeter = 2.0 * std::f64::consts::PI * radius; // perimeter of base of cone
    let side_length = (apothem.powi(2) + slant_height.powi(2)).sqrt(); // length of a side of the polygon
    let polygon_perimeter = side_length * sides as f64; // perimeter of polygon
    let cone_surface_area = std::f64::consts::PI * radius * slant_height; // lateral surface area of cone
    let polygon_surface_area = 0.5 * apothem * polygon_perimeter; // surface area of polygon
    cone_surface_area + polygon_surface_area + base_perimeter * radius // total surface area
}
742 chars
10 lines

The function takes in the radius of the circle circumscribing the polygon, the number of sides of the polygon, and the slant height of the cone. It first calculates the apothem of the polygon, the perimeter of the base of the cone, and the length of each side of the polygon. Using these values, it calculates the perimeter and surface area of the polygon. Finally, it calculates the lateral surface area of the cone and adds it to the polygon surface area and the surface area of the base of the cone to get the total surface area.

gistlibby LogSnag