Finding if a point is in a right triangle

I was always interested in the easiest way to find out if a point is in a triangle or in this case a rectangle cut in half diagonally.

Say I have a 64x64 pixel rectangle. With this rectangle, I want to return TRUE if the passed point is in the upper left corner of the rectangle, and FALSE if it is not.

----- | /| | / | |<__| 

Horray for bad ASCII art.

In any case, the hypothetical points for this triangle that return TRUE will be (0,0) and (63,0) and (0, 63). If the point hits the line (for example, 50.0), it will also return TRUE.

Assuming 0.0 is in the upper left corner and increases down ...

I had a possible solution in my head, but it seems more complicated than it should be - accepting the transmitted value of Y, determining where it will be in the rectangle, and calculating manually where the line will be cut with Y. For example, the passed value of Y 16 will be a quarter of the height of the rectangle. And thus, depending on which side you checked (left or right), the line should have been 16 or 48 pixels, depending on the direction of the line. In the above example, since we are testing the upper left corner, at a height of 16 pixels, the line will be 48 pixels wide

There must be a better way.

EDIT: A rectangle may also look the same

 ----- |\ | | \ | |__>| 

But I believe that in most cases, the current responses that have already been provided should still be delayed ...

+4
source share
5 answers

Upper left / lower right triangles: For all points in the upper left triangle x+y<=64 . The points in the lower right triangle have x+y>64 .

(for a rectangle of size (w, h) use w * y + h * xw * h <0)

Upper right / lower left triangles: For all points in the lower left triangle x<=y . The points in the upper right triangle have x>y .

(for a rectangle of size (w, h) use h * xw * y <0)


How did we get there?

For a rectangle of sizes (w, h) and triangles TL / BR, the diagonal equation (try it! Assign x = 0 and verify that you get y == h and assign y = 0 and verify that x == w)

 h*x + w*y - w*h = 0 

Points on one side of this line will have

 h*x + w*y - w*h > 0 

While the dots on the other will be

 h*x + w*y - w*h < 0 

Inserting 64 for w and h, we get:

 64x + 64y - 64*64 < 0 

Division by 64 gets us:

 x+y < 64 

For triangles TR / BL, the linear equation and the inequalities obtained are:

 h*x - w*y = 0 h*x - w*y < 0 h*x - w*y > 0 

Inserting 64 for w and h, we get

 64x-64y < 0 => x<y 
+11
source

you can imagine a triangle with three affine functions

take a unit triangle with angles at (0, 0), (1, 0) and (1, 1). the sides are represented by three lines

  • y = 0
  • x = 1
  • y = x

Thus, the interior and the boundary of the triangle are defined as the intersection of sets

  • x> = 1
  • y> = 0
  • y <= x

so for a given point, (x, y), you just need to check that it satisfies these three inequalities.

You can, of course, generalize this to any triangle, using the fact that any affine function (representing a line) can be written in the form y = mx + b.

+2
source

A simple option is to use the ray casting algorithm. Although it may be a bit overkill for what you need, it has the advantage that it will work with more complex triangles and polygons.

Wrong, the algorithm takes an imaginary point in the direction (for example, endlessly to the left) and throws a beam at your test point; you then calculate whether each line of your triangle intersects this infinitely long line. If you get an even number of transitions, your point is inside your triangle; even you are not in your triangle

0
source

The equation for the line looks like this:

 y = mx + b 

So, if you insert your x and y values ​​into this equation, this will probably not be done anymore. Let him reformulate this:

 mx + b - y = 0 

Same thing, different look. Again, the result is probably not zero. But the result will now tell you if it is on one side of the line or the other.

Now you only need to find out if the point is inside your rectangle.

0
source

Let's assume that your right triangle has one angle of 0.0 and a diagonal angle of a, b.

So y = mx + cc = 0 when we start at the origin.

m = b / a

So y = bx / a

To find out which half of the triangle your point (c, d) falls in

if (d <= (bc / a)) {// the point is in the lower half}

if (d> (bc / a)) {// the point is in the upper half}

I think...

0
source

All Articles