public void FindNearestPoint(UserPoints userPoints, Point point) { int difx, dify; int min = int.MaxValue; int distanceBetweenPoints; Point nearestPoint = point; if (!IsEmpty()) { foreach (Point userPoint in this.userPoints) { difx = point.X - userPoint.X; dify = point.Y - userPoint.Y; distanceBetweenPoints = (int)Math.Sqrt(Math.Pow(difx, 2) + Math.Pow(dify, 2)); if (distanceBetweenPoints < min) { nearestPoint = new Point(difx, dify); min = distanceBetweenPoints; } } } if (!userPoints.IsEmpty()) { foreach (Point userPoint in userPoints.GetUserPoints()) { difx = point.X - userPoint.X; dify = point.Y - userPoint.Y; distanceBetweenPoints = (int)Math.Sqrt(Math.Pow(difx, 2) + Math.Pow(dify, 2)); if (distanceBetweenPoints < min) { nearestPoint = new Point(difx, dify); min = distanceBetweenPoints; } } } if (this.userPoints.Any(pt => pt == nearestPoint)) { userPoints.Remove(nearestPoint); userPoints.GetUserPoints().Add(nearestPoint); } else if (userPoints.GetUserPoints().Any(pt => pt == nearestPoint)) { userPoints.GetUserPoints().Remove(nearestPoint); userPoints.Add(nearestPoint); } }