tags:

views:

1380

answers:

1

I was asking myself if there are examples online which covers how you can for instance detect shapes in touch gestures.

for example a rectangle or a circle (or more complex a heart .. )

or determine the speed of swiping (over time ( like i'm swiping my iphone against 50mph ))

+6  A: 

For very simple gestures (horizontal vs. vertical swipe), calculate the difference in x and y between two touches.

dy = abs(y2 - y1)
dx = abs(x2 - x1)
f = dy/dx

An f close to zero is a horizontal swipe. An f close to 1 is a diagonal swipe. And a very large f is a vertical swipe (keep in mind that dx could be zero, so the above won't yield valid results for all x and y).

If you're interested in speed, pythagoras can help. The length of the distance travelled between two touches is:

l = sqrt(dx*dx + dy*dy)

If the touches happened at times t1 and t2, the speed is:

tdiff = abs(t2 - t1)
s = l/tdiff

It's up to you to determine which value of s you interpret as fast or slow.

You can extend this approach for more complex figures, e.g. your square shape could be a horizontal/vertical/horizontal/vertical swipe with start/end points where the previous swipe stopped.

For more complex figures, it's probably better to work with an idealized shape. One could consider a polygon shape as the ideal, and check if a range of touches

  1. don't have too high a distance to their closest point on the pologyon's outline, and
  2. all touches follow the same direction along the polygon's outline.

You can refine things further from there.

Correct, there is not such library built-in to the Apple iPhone API. Such a development would be great to contribute back to the community, however. Detecting shapes is a complicated matter.
Nolte Burke
Man, I'd love a shape detection library... but as you said, it's complicated and therefore takes time, and time is the one thing I have too little of.