Reputation:
I have a rectangle which has known width and height.Now devide rectangle diagonally to form four triangles. Now suppose have a point p(m,n) and i have to decide that the point lies in which triangle out of four triangles. Please suggest me some good algo.
Upvotes: 0
Views: 111
Reputation:
if((x * height < y * width) || ( x*width < y*height )) { test = 1; } else { test = 0; } if((x > width / 2 && y > height / 2)) { test1=1; } else { test1=0; } int section = (test) * 2 + (test1);
switch(section)
{
case 0:
Log.d("ABSAR","UP button");
break;
case 1:
Log.d("ABSAR","RIGHT button");
break;
case 2:
Log.d("ABSAR","LEFT button");
break;
case 3:
Log.d("ABSAR","DOWN button");
break;
}
`if point P(x,y) and the rectangle of dimension width * height Then above code works OK...
Upvotes: 0
Reputation: 47403
Well you know the line equations of the diagonals(because you have two points). Both of the diagonals define two halfplanes.
Diagonal1 equation:
A1 * x + B1 * y + C1 = 0
Diagonal2 equation:
A2 * x + B2 * y + C2 = 0
It becomes:
Substitute A1 * m + B1 * n + C1
to find in which halfplane is the point for the first diagonal. If the number is < 0
, then it lies in one halfplane, if it is > 0
then it lies in the other.
Substitute A2 * m + B2 * n + C2
to find in which halfplane is the point for the second diagonal. If the number is < 0
, then it lies in one halfplane, if it is > 0
then it lies in the other.
Upvotes: 4
Reputation: 514
Interesting question. I think this algorithm should do it, but I haven't tried it out yet. Assuming that the rectangle is AxB and the point p(m,n).
int section = (m * B < n * A) * 2 + (m > A / 2 || n > B / 2);
The variable section should then be a value between 0 and 3, depending where the point resides. The sections are called:
0
2 1
3
If you wish to name the sections another way feel free to tweak the algo.
Upvotes: 1