如何在给定矩形中的坐标的情况下识别矩形内的子三角形

发布于 2024-08-22 02:12:54 字数 346 浏览 9 评论 0原文

问题说明

给定一个宽度为 w 、高度为 h 的矩形。和该矩形中的坐标 x,y 我想确定我在哪个三角形内。

即该函数应采用参数(x,y)并返回a,b,c,d或代表该三角形索引的从零开始的数字,即(0 = A,1 = B,2 = C,3 = D)如果它们是按这个顺序。

我认为这会类似于 >= 红线的公式和 >= 绿线的公式?

我想在 VB.NET 中实现这个

Problem illustration

Given a rectangle of width w and height h. and a coordinate x,y in that rectangle I would like to identify which triangle I am within.

i.e. the function should take parameters(x,y) and return a,b,c,d or a zero based number representing that triangle index i.e. (0=A,1=B,2=C,3=D) if they are in that order.

I think this would be something like >= the formula of the red line and >= the formula of the green line?

I'd like to implement this in VB.NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界如花海般美丽 2024-08-29 02:12:54
aboveRed = x*h > y*w;
aboveGreen = (w-x)*h > y*w;
if (aboveRed)
{
    if (aboveGreen) return "C"; else return "B";
}
else
{
    if (aboveGreen) return "D"; else return "A";
}
aboveRed = x*h > y*w;
aboveGreen = (w-x)*h > y*w;
if (aboveRed)
{
    if (aboveGreen) return "C"; else return "B";
}
else
{
    if (aboveGreen) return "D"; else return "A";
}
后来的我们 2024-08-29 02:12:54

绿线方程:h * x + w * y = h * w

红线方程:x * h - y * w = 0

Public Function GetTriangleNumber(ByVal x As Integer, ByVal y As Integer) 
                                                                     As Integer
    Dim overGreenLine As Boolean = ((((h * x) + (w * y)) - (h * w)) < 0)
    Dim overRedLine As Boolean = (((h * x) - (w * y)) > 0)
    If overGreenLine Then
        Return IIf(overRedLine, 2, 3)
    End If
    Return IIf(overRedLine, 1, 0)
End Function

Equation of green line: h * x + w * y = h * w

Equation of red line: x * h - y * w = 0

Public Function GetTriangleNumber(ByVal x As Integer, ByVal y As Integer) 
                                                                     As Integer
    Dim overGreenLine As Boolean = ((((h * x) + (w * y)) - (h * w)) < 0)
    Dim overRedLine As Boolean = (((h * x) - (w * y)) > 0)
    If overGreenLine Then
        Return IIf(overRedLine, 2, 3)
    End If
    Return IIf(overRedLine, 1, 0)
End Function
深居我梦 2024-08-29 02:12:54

我会考虑从左上角和右上角到点的线的角度。如果在两种情况下都小于 45 度(根据边的基本方向进行调整),则该点位于 C 中。其他组合将覆盖其他三个三角形。

您实际上不需要计算反三角函数来执行此操作,因为线长度的比率为您提供了足够的信息(并且 sin(45)...或者更确切地说 sin(pi/4) 是一个固定值) 。

I would consider the angle of the line to the point from the top left and top right corners. If it is less than 45 degrees (adjusting for the base direction of the edge) in both cases then the point is in C. Other combinations will cover the other three triangles.

You don't actually need to calculate inverse trig functions to do this, as the ratio of the lengths of the lines gives you enough information (and sin(45)... or rather sin(pi/4) is a fixed value).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文