好的,所以现在当我点击网格时,它会遍历网格上的所有“路径”并检查它们是否是椭圆以及鼠标是否在它们上方,如果有 2 个选择它计算相距的距离.. ..但是如果我在网格上得到 50+ 点,点击一个点什么也做不了.. 有没有更有效的方法来做到这一点?
这是我的代码:
foreach (var x in GraphPanel.Children)
{
try
{
if (((Path)x).IsMouseOver && ((Path)x).Data.ToString() == "System.Windows.Media.EllipseGeometry")
{
listViewPoints.SelectedItems.Add(listViewPoints.Items[PointIndex]);
var converter = new System.Windows.Media.BrushConverter();
var brush = (System.Windows.Media.Brush)converter.ConvertFromString("#FFB1D100");
((Path)x).Stroke = brush;
((Path)x).StrokeThickness = 8;
if (listViewPoints.SelectedItems.Count == 2)
{
double diffX;
double diffY;
double ptAX = ((Points)listViewPoints.SelectedItems[0]).A;
double ptAY = ((Points)listViewPoints.SelectedItems[0]).B;
double ptBX = ((Points)listViewPoints.SelectedItems[1]).A;
double ptBY = ((Points)listViewPoints.SelectedItems[1]).B;
if (ptAX > ptBX)
{
diffX = ptAX - ptBX;
}
else
{
diffX = ptBX - ptAX;
}
if (ptAY > ptBY)
{
diffY = ptAY - ptBY;
}
else
{
diffY = ptBY - ptAY;
}
//the distance between the points = sqr/-diff in x squared + diff in y squared
double result = Math.Sqrt(Math.Pow(diffX,2) + Math.Pow(diffY,2));
CalculatedDistanceApart.Content = "Distance Apart: " + result.ToString() + "'";
}
}
if (((Path)x).Data.ToString() == "System.Windows.Media.EllipseGeometry")
{
PointIndex++;
}
}
catch { }
}