Page 1 of 1

Calculating the Angle of a Swipe Gesture

Posted: Sun Oct 12, 2014 6:31 pm
by pink
I'm partly sharing, and partly looking for any suggestions to see if anyone thinks there is a better method...

The following script calculates the angle of a swipe gesture by comparing the start and end points.

0 degrees is straight right
90 degrees is straight down
180 degrees is straight left
270 degrees is straight up

Code: Select all

global gCoor

on touchStart pID 
   put empty into gCoor["xstart"] 
   put empty into gCoor["xend"]
   put empty into gCoor["ystart"] 
   put empty into gCoor["yend"]
   put empty into gCoor["swipeangle"]
end touchStart

on touchMove pID, px, py 
   if gCoor["xstart"] is empty then        
      put px into gCoor["xstart"] 
      put py into gCoor["ystart"] 
   else        
      put px into gCoor["xend"] 
      put py into gCoor["yend"] 
   end if
end touchMove

on touchEnd 
   put gCoor["yend"] - gCoor["ystart"] into tyDiff
   put gCoor["xend"] - gCoor["xstart"] into txDiff
   
   if atan2(tyDiff,txDiff)  < 0 then 
      put atan2(tyDiff,txDiff) *(180/pi)+360 into gCoor["swipeangle"]
   else
      put atan2(tyDiff,txDiff) *(180/pi) into gCoor["swipeangle"]
   end if
end touchEnd

Re: Calculating the Angle of a Swipe Gesture

Posted: Mon Oct 13, 2014 12:30 am
by [-hh]
Well done!
You should begin to build a collection of your 'geometry helpers'.
This example (in short) could explain atan and atan2 in the dictionary.

To praise LC here:
Actually atan2(y,x) uses atan(y/x) and LC handles the case x=0, the division by zero (= 'infinite slope' of a vertical line) for us.