user1200540
user1200540

Reputation:

Finding What point the mouse is over going too slow

Ok, so right now when i click on the grid it goes through all the 'paths' on the grid and checks if they're an ellipse and if the mouse is over them, and if there's 2 selected it calculated the distance apart.... but if i get 50+ points on the grid, clicking on a point does nothing.... Is there any more efficient way of doing this ?

Here's my code:

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 { }
}

Upvotes: 0

Views: 120

Answers (2)

weston
weston

Reputation: 54781

Well I don't know about your slow IsMouseOver, but the whole thing can certainly be sped and tidied up:

//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 { } 
} 

Math.Pow(x,2) vs x * x

Upvotes: 1

Emond
Emond

Reputation: 50672

Instead of using a Path you could add ellipses to the grid and just attach a MouseLeftButtonUp event handler to each ellipse.

You wouldn't need all this hit-test code.

Upvotes: 2

Related Questions