是否有某种优化方法可以获得最接近鼠标点的点?
以下是我的代码..
基本上我有一个有点集合的集合..有没有更好的方法可以解决这个问题?
谢谢..
double nearestDist = Snap.radius;
Point nearestPoint = new Point(clickPoint.X, clickPoint.Y);
Point lastpt = activeItem.pc.LastOrDefault();
drawCollection.ToList().ForEach(t =>
{
t.pc.ToList().ForEach(p => {
Point pt = p;
if (!lastpt.Equals(pt) || !activeItem.Equals(t))
{
double dist = (Math.Abs(clickPoint.X - pt.X) + Math.Abs(clickPoint.Y - pt.Y)) / 2;
if (dist > 0 && dist <= nearestDist && dist <= Snap.radius)
{
nearestDist = dist;
nearestPoint = pt;
DoubleSnapItem = t;
}
}
});
});