views:

113

answers:

1

hi, I'm trying to get you into the picture firstly, I'm trying to implement a gesture in WPF4 VS2010, it is like you move your finger until it crosses a TouchPoint that you already passed by with the SAME finger , so my thought is to make a list and check for every new TouchPoint if it exists , if yes then you have done your gesture , if not then add the TouchPoint to the collection to be compared with the following TouchPoints. for some reason this doesn't work well so I moved to another approach , replacing TouchPoints with X , Y for the TouchPoint and convert them into Strings and try to use Contains method against them , using TouchMove and TouchUp events my code looks like :

 private void g1_TouchMove(object sender, TouchEventArgs e)
    {



       if(touchX.Contains(""+e.GetTouchPoint(g1).Position.X) && touchY.Contains(""+e.GetTouchPoint(g1).Position.Y))
        {
         // Clearing the lists , changing the canvas background color to know that the gesture is done
           touchX.Clear();
           touchY.Clear();
           g1.Background = Brushes.AliceBlue;

        }
       else
       {
           //adding new X, Y values to their respective lists
           touchX.Add(""+e.GetTouchPoint(g1).Position.X);
           touchY.Add( ""+e.GetTouchPoint(g1).Position.Y);


       }

    }
private void g1_TouchUp(object sender, TouchEventArgs e)
    {
        //clearing the lists after the touch is up (finger removed)
        touchX.Clear();
        touchY.Clear();
        //getting the canvas it's original background color
        g1.Background = Brushes.Orange;

    }

So , when testing it it doesn't work , even if I move my touch in a straight line it changes the background. any Ideas ?

Thanks in advance

+1  A: 

First off, go back to using numbers. Putting numbers into strings for later comparison is wrong on so many levels :-)

My guess is that your problem is a resolution problem - It's practically impossible to hit the exact same spot as before, since there are a lot of pixels on the screen. Basically, one pixel off will render your algorithm useless. You should instead map your touch area into a few larger clusters and check if the touch has been in this cluster before (as opposed to the exact same pixel).

A simple approach is to just integer division on the coordinates you receive.

In the example below, I divide the pixel coordinate system with clusters of 3 by 3 pixels but you could go with larger if that makes sense for you. It all depends on how big the resolution of the touch area is.

What this means in practice is that any pixel within this 3 by 3 area is considered equal. So a hit on (1,1) is matching a previous hit on (2,3) and so on.

// Divide into 3x3 pixel clusters
var currentCluster = new Point((int)touchPos.X / 3, (int)touchPos.Y / 3)
// previousClusters is a List<Point>() which is cleared on TouchUp
foreach (var cluster in previousClusters)
{
    if (cluster == currentCluster)
    {
        // We've been here before, do your magic here!
        g1.Background = Brushes.AliceBlue;
        // Return here, since we don't want to add the point again
        return;
    }
}
previousClusters.Add(currentCluster);
Isak Savo
Thanks a lot Mr. Isak , didn't test it yet but seems like that is what I was looking for , and yeah I really don't know why I changed numbers to Strings for comparison purposes , shame on me , thanks again
Mrme