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); } }
private void imgBox_MouseDown(object sender, MouseEventArgs e) { if (imgBox.Image != null & (tabControl1.SelectedIndex == 0)) { if (e.Button == MouseButtons.Left) { if (userRadioBtn.Checked) { userPoints.Add(e.Location); } else if (x1RadioButton.Checked) { axisPointsArray[0] = e.Location; } else if (x2RadioButton.Checked) { axisPointsArray[1] = e.Location; } else if (y1RadioButton.Checked) { axisPointsArray[2] = e.Location; } else if (y2RadioButton.Checked) { axisPointsArray[3] = e.Location; } axisPoints.userPoints = axisPointsArray.ToList(); } else if (e.Button == MouseButtons.Right) { SelectNearestPoint(e.Location); } } Invalidate(true); Update(); }