Calculating the Angle of a Swipe Gesture

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
pink
Posts: 280
Joined: Wed Mar 12, 2014 6:18 pm

Calculating the Angle of a Swipe Gesture

Post by pink » Sun Oct 12, 2014 6:31 pm

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
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Calculating the Angle of a Swipe Gesture

Post by [-hh] » Mon Oct 13, 2014 12:30 am

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.
shiftLock happens

Post Reply