public bool CalcCenter() { double minDispersion = Center.UpdateDispersion(Points); KMPoint newCenter = null; foreach (var point in Points) { if (point == Center) { continue; } double currDispersion = point.UpdateDispersion(Points); if (currDispersion < minDispersion) { newCenter = point; minDispersion = currDispersion; } } if (newCenter != null) { Center = newCenter; return(true); } return(false); }
private void AddToCluster(KMPoint point) { double minDistance = 0; Cluster current = null; if (point == null) { throw new NullReferenceException("point is null"); } foreach (var cluster in _clusters) { double distance = point.GetDistanceTo(cluster.Center); if (current == null || minDistance > distance) { minDistance = distance; current = cluster; } } if (current != null) { current.AddPoint(point); } }
public void DrawClusters(Graphics graphics, bool drawLine) { if (graphics == null) { throw new NullReferenceException("graphics is null"); } foreach (var cluster in _clusters) { KMPoint center = cluster.Center; foreach (var point in cluster.Points) { if (point == cluster.Center) { continue; } graphics.DrawLine(cluster.Pen, point.X - 2, point.Y - 2, point.X + 2, point.Y + 2); graphics.DrawLine(cluster.Pen, point.X - 2, point.Y + 2, point.X + 2, point.Y - 2); if (drawLine) { graphics.DrawLine(cluster.Pen, center.X, center.Y, point.X, point.Y); } } graphics.DrawRectangle(cluster.Pen, center.X - 3, center.Y - 3, 6, 6); graphics.FillRectangle(cluster.Brush, center.X - 3, center.Y - 3, 6, 6); } }
public double GetDistanceTo(KMPoint point) { if (point == null) { throw new NullReferenceException("point is null"); } return(Math.Sqrt(Math.Pow(Math.Abs(X - point.X), 2) + Math.Pow(Math.Abs(Y - point.Y), 2))); }
public void AddPoint(KMPoint point) { if (point == null) { throw new NullReferenceException("point is null"); } if (point == Center) { return; } Points.Add(point); }
public Cluster(KMPoint center, Color color) { if (center == null) { throw new NullReferenceException("center is null"); } if (color == null) { throw new NullReferenceException("color is null"); } Center = center; Pen = new Pen(color); Brush = new SolidBrush(color); Points = new List <KMPoint>(); Points.Add(center); }