rustgym/leetcode/
_469_convex_polygon.rs1struct Solution;
2
3impl Solution {
4 fn is_convex(points: Vec<Vec<i32>>) -> bool {
5 let n = points.len();
6 let mut positive = 0;
7 let mut negative = 0;
8 for i in 0..n {
9 let sign = Self::cross(&points[i], &points[(i + 1) % n], &points[(i + 2) % n]);
10 if sign >= 0 {
11 positive += 1;
12 }
13 if sign <= 0 {
14 negative += 1;
15 }
16 }
17 positive == n || negative == n
18 }
19
20 fn cross(a: &[i32], b: &[i32], c: &[i32]) -> i32 {
21 (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0])
22 }
23}
24
25#[test]
26fn test() {
27 let points = vec_vec_i32![[0, 0], [0, 1], [1, 1], [1, 0]];
28 let res = true;
29 assert_eq!(Solution::is_convex(points), res);
30 let points = vec_vec_i32![[0, 0], [0, 10], [10, 10], [10, 0], [5, 5]];
31 let res = false;
32 assert_eq!(Solution::is_convex(points), res);
33}