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
95
96
97
use crate;
/// Computes the geometric center (centroid) of a set of points.
///
/// The center is calculated by averaging all the point coordinates. This is also known as
/// the centroid or barycenter of the point cloud. All points are weighted equally.
///
/// # Arguments
///
/// * `pts` - A slice of points. Must contain at least one point.
///
/// # Returns
///
/// The geometric center as a `Vector`.
///
/// # Panics
///
/// Panics if the input slice is empty.
///
/// # Examples
///
/// ## 2D Example
///
/// ```
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// use parry2d::utils::center;
/// use parry2d::math::Vector;
///
/// let points = vec![
/// Vector::new(0.0, 0.0),
/// Vector::new(2.0, 0.0),
/// Vector::new(2.0, 2.0),
/// Vector::new(0.0, 2.0),
/// ];
///
/// let c = center(&points);
///
/// // The center of a square is at its middle
/// assert!((c.x - 1.0).abs() < 1e-6);
/// assert!((c.y - 1.0).abs() < 1e-6);
/// # }
/// ```
///
/// ## 3D Example
///
/// ```
/// # #[cfg(all(feature = "dim3", feature = "f32"))] {
/// use parry3d::utils::center;
/// use parry3d::math::Vector;
///
/// let points = vec![
/// Vector::new(0.0, 0.0, 0.0),
/// Vector::new(4.0, 0.0, 0.0),
/// Vector::new(0.0, 4.0, 0.0),
/// ];
///
/// let c = center(&points);
///
/// // The center of these three points
/// assert!((c.x - 4.0 / 3.0).abs() < 1e-6);
/// assert!((c.y - 4.0 / 3.0).abs() < 1e-6);
/// assert!(c.z.abs() < 1e-6);
/// # }
/// ```
///
/// ## Single Vector
///
/// ```
/// # #[cfg(all(feature = "dim2", feature = "f32"))] {
/// use parry2d::utils::center;
/// use parry2d::math::Vector;
///
/// let points = vec![Vector::new(5.0, 10.0)];
/// let c = center(&points);
///
/// // The center of a single point is the point itself
/// assert_eq!(c, points[0]);
/// # }
/// ```