find the surface area of a regular polygon inscribed in a pyramid in rust

To find the surface area of a regular polygon inscribed in a pyramid, we need to find the area of each triangular face of the pyramid and add them up. Since the polygon is regular, the triangles will be congruent, so we just need to find the area of one of them and multiply by the number of faces.

We can start by finding the height of the pyramid, which can be found using the Pythagorean theorem. Let s be the length of one side of the polygon and r be the radius of the inscribed circle.

main.rs
let s = 10.0; // length of one side of polygon
let n = 8.0; // number of sides of polygon
let r = (s / 2.0) / f64::tan(std::f64::consts::PI / n); // radius of inscribed circle

let height = f64::sqrt(s.powi(2) - 4.0 * r.powi(2)); // height of pyramid
251 chars
6 lines

Next, we need to find the area of one of the triangular faces. We can use the formula 1/2 * base * height, where the base is simply one side of the polygon and the height is the height of the pyramid.

main.rs
let base = s;
let face_area = 0.5 * base * height;
51 chars
3 lines

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

main.rs
let surface_area = n * face_area;
34 chars
2 lines

And there we have it, the surface area of a regular polygon inscribed in a pyramid!

gistlibby LogSnag