Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to calculate the distance between two screen touches?
Started by lcriptocoin Aug 26 2018 05:04 AM

2 replies to this topic
zoom multitouch scale
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

lcriptocoin

[GLOBAL: userInfoPane.html]
lcriptocoin
  • Observer

  • 1 posts
  • Corona SDK

Hello! Please tell me how to calculate the distance between two touches of the screen fingers? This is necessary in order to scale the image, if the distance between the fingers increases , the picture increases, and Vice versa.



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 127 posts
  • Corona SDK

You should read https://docs.coronalabs.com/guide/events/touchMultitouch/.

You would need to get the event.x and event.y values for both touches and then use Pythagora's theorem to calculate the distance.



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,527 posts
  • Corona Staff

This is some pretty straightforward math.  You have two touch points that each have an X and Y value. Let's call them touch1 and touch2 and their values are touch1.x, touch1.y, touch2.x and touch2.y. You can then get the distance via:

 

local dx =  touch1.x - touch2.x
local dy =  touch1.y - touch2.y

local distance = math.sqrt( dx * dx + dy * dy )

Rob




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: zoom, multitouch, scale