Math to detect the touch


#1

This has been driving me nuts for quite a bit now, and I am quite rusty when it comes to trig and geometry. I understand that the code mentioned (to my understanding) creates a triangle.

[code]if (hypot(x - p.x, y-p.y) < 20.0) { // no idea why …

[/code]

My confusion derives from the idea that if the length of the hypotenuse is less than 20, then the line has been selected. Could anyone try to explain how this evaluates to true and why the number 20?


#2

Well, think about the formula for finding a hypotenuse:

h = hypot(x, y) = sqrt(x ^ 2 + y ^ 2);

And then think about the formula for finding the distance between two points:

d = sqrt((x - p.x) ^ 2 + (y - p.y) ^ 2);

And thus, replacing x in the initial equation with (x - p.x) and y with (y - p.y), we get "if the distance between (x,y) and (p.x, p.y) is less than 20 points).