Question

So, I have a rectangle divided into two triangles and I have a point:

Rectangle

I know the coordinates of the four vertices of the rectangle (A, B, C and D) and I know the coordinates of P. What I need to know is in which of the two triangles is the point.

I think it's a really simple thing to do, but apparently I can't figure it out by myself.

Any help?

Was it helpful?

Solution

There are couple of ways to tackle this. But the easiest and most straight forward is to check for the slope.

For your example, if slope AP is lesser than slope AD then point P is above the AD line and vice versa. If the slope is the same, then it is on the same line.

Edit:

Assume the following

A -> (0,3)
B -> (3,3)
C -> (0,0)
D -> (3,0)

Then the slope AD is (3-0)/(3-0) = 1. Assume that P is at (2,2), then slope AP is (2-3)/(2-0) = -1/2.

We have AD > AP, thus point P must be above the line AD. Notice that point P needs to cross AD in order to be below the line, and thus the slope changes.

OTHER TIPS

You could consider vector AP and vector AD. Compute the cross product of these two vectors. The sign of the cross product will tell you what side of the diagonal the point is on.

More information: http://en.wikipedia.org/wiki/Cross_product

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top