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

To find the surface area of a regular polygon circumscribing a pyramid, we need to sum up the areas of all the triangular faces of the pyramid.

First, we need to calculate the slant height (s) of the pyramid as well as the apothem (a) of the regular polygon circumscribing the pyramid.

main.rs
    let n = 5; //number of sides of the polygon
    let r = 6.0; //radius of the circle that circumscribes the polygon
    let h = 10.0; //height of the pyramid
    let theta = 360.0/n as f64; //interior angle of each polygon
    let apothem = r * (theta/2.0).to_radians().cos(); //apothem of the polygon
    let s = (apothem.powi(2) + h.powi(2)).sqrt(); //slant height of the pyramid
385 chars
7 lines

Once we have calculated the slant height and apothem of the pyramid and regular polygon respectively, we can find the area of each triangular face of the pyramid using the formula:

main.rs
    let face_area = 0.5 * s * apothem;
39 chars
2 lines

Finally, by multiplying the area of each face by the number of faces, which is equal to the number of sides of the polygon, we can find the total surface area of the pyramid.

main.rs
    let surface_area = n as f64 * face_area;
45 chars
2 lines

Therefore, the complete rust code to find the surface area of a regular polygon circumscribing a pyramid would be:

main.rs
fn main() {
    let n = 5; //number of sides of the polygon
    let r = 6.0; //radius of the circle that circumscribes the polygon
    let h = 10.0; //height of the pyramid
    let theta = 360.0/n as f64; //interior angle of each polygon
    let apothem = r * (theta/2.0).to_radians().cos(); //apothem of the polygon
    let s = (apothem.powi(2) + h.powi(2)).sqrt(); //slant height of the pyramid
    let face_area = 0.5 * s * apothem; //area of each face of the pyramid
    let surface_area = n as f64 * face_area; //total surface area of the pyramid
    println!("Surface Area of the Pyramid: {}", surface_area);
}
617 chars
12 lines

gistlibby LogSnag