0

Suppose i have coordinates of a rectangle. Now i want to find out if the touch event or click event position exists within the rectangle, How can i do that?

A      B

    C

D      E

E.g i have this rectangle ABDE. I clicked on C, now i have all the coordinates. How can i figure out that c exists within ABDE

I assume that Martix is created for this purpose but I can't find any particular examples on internet.

Best Regards

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • Is this what you're looking for? http://stackoverflow.com/questions/2597590/how-can-i-tell-if-a-closed-path-contains-a-given-point – bman Jun 17 '12 at 09:50

2 Answers2

0

Express your rectangle as smallest (X;Y) (min(A.X,B.X...);min(A.Y...)) and biggest (X;Y), then check that c.X >= min.X && c.X <= max.X && c.Y >= min.Y && c.Y <= max.Y.

Eric
  • 1,689
  • 15
  • 12
0

Let's assume the points you have given in your example with the corresponding coordinates:

A(x1, y1)      B(x2, y1)

    C(x, y)

D(x1, y2)      E(x2, y2)

Point C is within the rectangle only and only if:

if(((x >= x1) && (x <= x2)) && ((y >= y1) && (y <= y2)){
   System.println.out("Point C is within the rectangle");
}
GETah
  • 20,922
  • 7
  • 61
  • 103
  • can you tell where can i store these coordinates, right now i am using float[] x .. x[0] for x and x[1] for y etc – Muhammad Umar Jun 17 '12 at 10:24
  • @user966227 Yes that can do it. You can express every point in a float[2] where float[0] is x and float[1] is y – GETah Jun 17 '12 at 10:32