find the area of a regular polygon in rust

To find the area of a regular polygon, we need to know the length of its sides (s) and the number of sides (n). Once we have these values, we can use the formula:

main.rs
area = (n * s^2) / (4 * tan(pi / n))
37 chars
2 lines

In Rust, we can implement this formula as follows:

main.rs
use std::f64::consts::PI;

fn regular_polygon_area(n: u32, s: f64) -> f64 {
    let numerator = n as f64 * s.powi(2);
    let denominator = 4.0 * (PI / n as f64).tan();
    numerator / denominator
}

fn main() {
    let n = 6; // hexagon
    let s = 10.0; // length of side
    let area = regular_polygon_area(n, s);
    println!("Area of the regular polygon: {}", area);
}
374 chars
15 lines

In this example, we have defined a function regular_polygon_area that takes two arguments n and s, and returns the area of the regular polygon. We have also implemented this function in the main function for a regular hexagon with side length of 10.0 units. The output of the program would print the area of the hexagon as 259.8076211353316.

gistlibby LogSnag