4

I made an Android custom view that looks like a D-Pad (image below). Given the touched coordinate (x,y), rectangle width (w), and rectangle height (h), ignoring the white diagonal and rounded corners, could somebody help me how do I determine which triangle that the user touch?

Additional info: (0,0) is upper left corner

D-Pad Image

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51

1 Answers1

3

Shift the origin to the center: subtract w/2 from x and h/2 from y. If it's not a square (say w is greater than h) then normalize the larger dimension: x *= h; x /= w; Then, the four sections can be determined as follows:

x < 0, |y| < |x| = left
x > 0, |y| < |x| = right
y < 0, |x| < |y| = up
y > 0, |x| < |y| = down

(Any other condition, such as x=y=0, or |x|=|y|, means you're on the diagonals).

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55