-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy patharea_of_polygon.rs
94 lines (81 loc) · 2.23 KB
/
area_of_polygon.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @file
* @brief Calculate the area of a polygon defined by a vector of points.
*
* @details
* This program provides a function to calculate the area of a polygon defined by a vector of points.
* The area is calculated using the formula: A = |Σ((xi - xi-1) * (yi + yi-1))| / 2
* where (xi, yi) are the coordinates of the points in the vector.
*
* @param fig A vector of points defining the polygon.
* @return The area of the polygon.
*
* @author [Gyandeep](https://github.com/Gyan172004)
* @see [Wikipedia - Polygon](https://en.wikipedia.org/wiki/Polygon)
*/
pub struct Point {
x: f64,
y: f64,
}
/**
* Calculate the area of a polygon defined by a vector of points.
* @param fig A vector of points defining the polygon.
* @return The area of the polygon.
*/
pub fn area_of_polygon(fig: &[Point]) -> f64 {
let mut res = 0.0;
for i in 0..fig.len() {
let p = if i > 0 {
&fig[i - 1]
} else {
&fig[fig.len() - 1]
};
let q = &fig[i];
res += (p.x - q.x) * (p.y + q.y);
}
f64::abs(res) / 2.0
}
#[cfg(test)]
mod tests {
use super::*;
/**
* Test case for calculating the area of a triangle.
*/
#[test]
fn test_area_triangle() {
let points = vec![
Point { x: 0.0, y: 0.0 },
Point { x: 1.0, y: 0.0 },
Point { x: 0.0, y: 1.0 },
];
assert_eq!(area_of_polygon(&points), 0.5);
}
/**
* Test case for calculating the area of a square.
*/
#[test]
fn test_area_square() {
let points = vec![
Point { x: 0.0, y: 0.0 },
Point { x: 1.0, y: 0.0 },
Point { x: 1.0, y: 1.0 },
Point { x: 0.0, y: 1.0 },
];
assert_eq!(area_of_polygon(&points), 1.0);
}
/**
* Test case for calculating the area of a hexagon.
*/
#[test]
fn test_area_hexagon() {
let points = vec![
Point { x: 0.0, y: 0.0 },
Point { x: 1.0, y: 0.0 },
Point { x: 1.5, y: 0.866 },
Point { x: 1.0, y: 1.732 },
Point { x: 0.0, y: 1.732 },
Point { x: -0.5, y: 0.866 },
];
assert_eq!(area_of_polygon(&points), 2.598);
}
}