查找鼠标过多的点速度太慢

  • 本文关键字:速度 鼠标 查找 c# wpf
  • 更新时间 :
  • 英文 :


好的,所以现在当我点击网格时,它会穿过网格上的所有"路径",检查它们是否是椭圆,鼠标是否在上面,如果选择了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 { }
}

您可以将椭圆添加到网格中,而不是使用Path,只需将MouseLeftButtonUp事件处理程序附加到每个椭圆即可。

您不需要所有这些命中测试代码。

我不知道你缓慢的IsMouseOver,但整个事情肯定可以加快和整理:

//why make these for every item?
var converter = new System.Windows.Media.BrushConverter(); 
var brush = (System.Windows.Media.Brush)converter.ConvertFromString("#FFB1D100"); 
foreach (var x in GraphPanel.Children) 
{ 
    //this is tidier, but what if it's not a Path, is that possible?
    var path = (Path)x;
    //jump out here if it's not the right type, reduces nesting and removes one comparison
    if(!(path.Data is System.Windows.Media.EllipseGeometry)) continue;
     try
     {
         if (path.IsMouseOver)
         { 
            listViewPoints.SelectedItems.Add(listViewPoints.Items[PointIndex]); 
            path.Stroke = brush; 
            path.StrokeThickness = 8;      
            if (listViewPoints.SelectedItems.Count == 2) 
            {
                 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; 
                 //you're going to square these, so negatives don't matter
                 double diffX = ptAX - ptBX;
                 double diffY = ptAY - ptBY;
                 //the distance between the points = sqr/-diff in x squared + diff in y squared 
                 //don't use Math.Pow for squaring
                 double result = Math.Sqrt(diffX*diffX + diffY*diffY); 
                 CalculatedDistanceApart.Content = "Distance Apart: " + result + "'"; 
                 //are we done now?
             } 
        } 
        PointIndex++; 
    } 
    catch { } 
} 

数学Pow(x,2)与x*x

最新更新