/// <summary> /// Adds a point to a streaming cloud : the mean and variance of the clouds are updated. /// </summary> /// <param name="point">The point to add to the cloud.</param> public void Add(WeightedPoint point) { _nbPoints++; if (_barycenter == null) { _barycenter = new WeightedPoint(point.X, point.Y, 1); _dispersion = 0; } else { _barycenter = _barycenter.Multiply(_nbPoints - 1); _barycenter = _barycenter.Add(point); _barycenter = _barycenter.Divide(_nbPoints); _dispersion = (_dispersion * (_nbPoints - 1) + Distances.Euclide(_barycenter, point)) / _nbPoints; } }
/// <summary> /// Given a list of WeightedPoints returns. /// </summary> /// <param name="weightedPoints">Weighted points to average.</param> /// <returns>The barycenter of the input points.</returns> public static WeightedPoint Barycenter(IList<WeightedPoint> weightedPoints) { WeightedPoint res = new WeightedPoint(); double counter = 0; foreach (WeightedPoint pt in weightedPoints) { res = res.Add(pt.Multiply(pt.Weight)); counter += pt.Weight; } res = res.Divide(counter); return res; }