凸包算法

  1. use std::cmp::Ordering::Equal;
  2. fn sort_by_min_angle(pts: &[(f64, f64)], min: &(f64, f64)) -> Vec<(f64, f64)> {
  3. let mut points: Vec<(f64, f64, (f64, f64))> = pts
  4. .iter()
  5. .map(|x| {
  6. (
  7. ((x.1 - min.1) as f64).atan2((x.0 - min.0) as f64),
  8. // angle
  9. ((x.1 - min.1) as f64).hypot((x.0 - min.0) as f64),
  10. // distance (we want the closest to be first)
  11. *x,
  12. )
  13. })
  14. .collect();
  15. points.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Equal));
  16. points.into_iter().map(|x| x.2).collect()
  17. }
  18. // calculates the z coordinate of the vector product of vectors ab and ac
  19. fn calc_z_coord_vector_product(a: &(f64, f64), b: &(f64, f64), c: &(f64, f64)) -> f64 {
  20. (b.0 - a.0) * (c.1 - a.1) - (c.0 - a.0) * (b.1 - a.1)
  21. }
  22. /*
  23. If three points are aligned and are part of the convex hull then the three are kept.
  24. If one doesn't want to keep those points, it is easy to iterate the answer and remove them.
  25. The first point is the one with the lowest y-coordinate and the lowest x-coordinate.
  26. Points are then given counter-clockwise, and the closest one is given first if needed.
  27. */
  28. pub fn convex_hull_graham(pts: &[(f64, f64)]) -> Vec<(f64, f64)> {
  29. if pts.is_empty() {
  30. return vec![];
  31. }
  32. let mut stack: Vec<(f64, f64)> = vec![];
  33. let min = pts
  34. .iter()
  35. .min_by(|a, b| {
  36. let ord = a.1.partial_cmp(&b.1).unwrap_or(Equal);
  37. match ord {
  38. Equal => a.0.partial_cmp(&b.0).unwrap_or(Equal),
  39. o => o,
  40. }
  41. })
  42. .unwrap();
  43. let points = sort_by_min_angle(pts, min);
  44. if points.len() <= 3 {
  45. return points;
  46. }
  47. for point in points {
  48. while stack.len() > 1
  49. && calc_z_coord_vector_product(&stack[stack.len() - 2], &stack[stack.len() - 1], &point)
  50. < 0.
  51. {
  52. stack.pop();
  53. }
  54. stack.push(point);
  55. }
  56. stack
  57. }
  58. #[cfg(test)]
  59. mod tests {
  60. use super::*;
  61. #[test]
  62. fn empty() {
  63. assert_eq!(convex_hull_graham(&vec![]), vec![]);
  64. }
  65. #[test]
  66. fn not_enough_points() {
  67. let list = vec![(0f64, 0f64)];
  68. assert_eq!(convex_hull_graham(&list), list);
  69. }
  70. #[test]
  71. fn not_enough_points1() {
  72. let list = vec![(2f64, 2f64), (1f64, 1f64), (0f64, 0f64)];
  73. let ans = vec![(0f64, 0f64), (1f64, 1f64), (2f64, 2f64)];
  74. assert_eq!(convex_hull_graham(&list), ans);
  75. }
  76. #[test]
  77. fn not_enough_points2() {
  78. let list = vec![(2f64, 2f64), (1f64, 2f64), (0f64, 0f64)];
  79. let ans = vec![(0f64, 0f64), (2f64, 2f64), (1f64, 2f64)];
  80. assert_eq!(convex_hull_graham(&list), ans);
  81. }
  82. #[test]
  83. // from https://codegolf.stackexchange.com/questions/11035/find-the-convex-hull-of-a-set-of-2d-points
  84. fn lots_of_points() {
  85. let list = vec![
  86. (4.4, 14.),
  87. (6.7, 15.25),
  88. (6.9, 12.8),
  89. (2.1, 11.1),
  90. (9.5, 14.9),
  91. (13.2, 11.9),
  92. (10.3, 12.3),
  93. (6.8, 9.5),
  94. (3.3, 7.7),
  95. (0.6, 5.1),
  96. (5.3, 2.4),
  97. (8.45, 4.7),
  98. (11.5, 9.6),
  99. (13.8, 7.3),
  100. (12.9, 3.1),
  101. (11., 1.1),
  102. ];
  103. let ans = vec![
  104. (11., 1.1),
  105. (12.9, 3.1),
  106. (13.8, 7.3),
  107. (13.2, 11.9),
  108. (9.5, 14.9),
  109. (6.7, 15.25),
  110. (4.4, 14.),
  111. (2.1, 11.1),
  112. (0.6, 5.1),
  113. (5.3, 2.4),
  114. ];
  115. assert_eq!(convex_hull_graham(&list), ans);
  116. }
  117. #[test]
  118. // from https://codegolf.stackexchange.com/questions/11035/find-the-convex-hull-of-a-set-of-2d-points
  119. fn lots_of_points2() {
  120. let list = vec![
  121. (1., 0.),
  122. (1., 1.),
  123. (1., -1.),
  124. (0.68957, 0.283647),
  125. (0.909487, 0.644276),
  126. (0.0361877, 0.803816),
  127. (0.583004, 0.91555),
  128. (-0.748169, 0.210483),
  129. (-0.553528, -0.967036),
  130. (0.316709, -0.153861),
  131. (-0.79267, 0.585945),
  132. (-0.700164, -0.750994),
  133. (0.452273, -0.604434),
  134. (-0.79134, -0.249902),
  135. (-0.594918, -0.397574),
  136. (-0.547371, -0.434041),
  137. (0.958132, -0.499614),
  138. (0.039941, 0.0990732),
  139. (-0.891471, -0.464943),
  140. (0.513187, -0.457062),
  141. (-0.930053, 0.60341),
  142. (0.656995, 0.854205),
  143. ];
  144. let ans = vec![
  145. (1., -1.),
  146. (1., 0.),
  147. (1., 1.),
  148. (0.583004, 0.91555),
  149. (0.0361877, 0.803816),
  150. (-0.930053, 0.60341),
  151. (-0.891471, -0.464943),
  152. (-0.700164, -0.750994),
  153. (-0.553528, -0.967036),
  154. ];
  155. assert_eq!(convex_hull_graham(&list), ans);
  156. }
  157. }