查找一个点是否在直角三角形内 [英] Finding Whether A Point is Within a Right-Angle Triangle

查看:257
本文介绍了查找一个点是否在直角三角形内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直想知道最简单的方法来确定一个点是否位于一个三角形内,或者在这个例子中,一个矩形切成对角线的一半。 >假设我有一个64x64像素的矩形。使用此矩形,如果传递的点位于矩形的左上角,我想返回TRUE值;如果不是,则返回FALSE。

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

Horray用于糟糕的ASCII艺术。

无论如何,这个返回TRUE的三角形的假设点是(0,0)和(63,0)和(0,63)。如果一个点落在一条线上(例如,50,0),它也会返回TRUE。

假设0,0位于左上角并向下增加...



我的头脑中有一个可能的解决方案,但它看起来比它应该更复杂 - 将传递的Y值,确定它将在矩形中的位置,并手动计算该线将以该Y值切割的位置。例如,通过的Y值为16将是矩形的四分之一高度。因此,取决于你检查的是哪一边(左边或右边),该线可以是16px或48px,这取决于线的方向。 在上面的例子中,因为我们正在测试左上角,在16px高度处,线条宽度为48px

必须是更好的方式。



编辑:
矩形也可以是这样的

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

但在大多数情况下,现在的答案仍然存在......

解决方案

左上角/右下角三角形:对于左上角三角形中的所有点, X + Y< = 64 。右下角三角形中的点有 x + y> 64



,h)使用w * y + h * xw * h <0)

右上角/左下角三角形:在左下三角形中, x <= y 。右上三角形中的点有 x> y



(对于大小为(w,h )使用h * xw * y <0)






我们如何到达那里?



对于一个尺寸为(w,h)和TL / BR三角形的矩形,的对角线方程为(尝试它!赋值x = 0并检查是否得到y == h,并赋值y = 0并检查x == w)

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

该行一端的点将有

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

另一方面的积分将有

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

为w和h插入64,我们得到:

  64x + 64y  -  64 * 64 < 0 

除以64得到我们:

  x + y < 64 

对于TR / BL三角形,线方程和由此产生的不等式是:

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

为w和h插入64,我们得到

  64x-64y< 0 
=> x


I've always wondered the easiest way to figure out whether or not a point lies within a triangle, or in this instance, a rectangle cut into half diagonally.

Let's say I have a rectangle that is 64x64 pixels. With this rectangle, I want to return a TRUE value if a passed point is within the upper-left corner of the rectangle, and FALSE if it isn't.

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

Horray for bad ASCII art.

Anyway, the hypothetical points for this triangle that would return TRUE would be (0,0) and (63,0) and (0, 63). If a point lands on a line (e.g., 50,0) it would return TRUE as well.

Assuming 0,0 is in the upper-left corner and increases downwards...

I've had a possible solution in my head, but it seems more complicated than it should be - taking the passed Y value, determining where it would be in the rectangle, and figuring out manually where the line would cut at that Y value. E.g, a passed Y value of 16 would be quarter height of the rectangle. And thus, depending on what side you were checking (left or right), the line would either be at 16px or 48px, depending on the direction of the line. In the example above, since we're testing the upper-left corner, at 16px height, the line would be at 48px width

There has to be a better way.

EDIT: The rectangle could also look like this as well

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

But I'm figuring in most cases the current answers already provided should still hold up...

解决方案

Top-left/bottom-right triangles: For all points in the top-left triangle, x+y<=64. Points in the bottom-right triangle have x+y>64.

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

Top-right/bottom-left triangles: For all points in the bottom-left triangle, x<=y. Points in the top-right triangle have x>y.

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


How did we get there?

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

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

Points on one side of that line will have

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

While points on the other will have

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

Inserting 64 for both w and h, we get:

64x + 64y - 64*64 < 0

Dividing by 64 gets us:

x+y < 64

For TR/BL triangles, the line equation and the resulting inequalities 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

这篇关于查找一个点是否在直角三角形内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆