【问题标题】:How to identify subtriangle within a rectangle given a coordinate in that rectangle给定矩形中的坐标,如何识别矩形内的子三角形
【发布时间】:2010-02-22 12:26:59
【问题描述】:

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

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

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

我想在 VB.NET 中实现这个

【问题讨论】:

标签: vb.net geometry linear-equation


【解决方案1】:
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";
}

【讨论】:

    【解决方案2】:

    绿线方程: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
    

    【讨论】:

    • 感谢我对 VB 代码投了赞成票,但接受了 Vlad 的回答,因为他首先使用了算法
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多