如何给定的矩形内确定子三角形在矩形的坐标
-
22-09-2019 - |
解决方案
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";
}
其他提示
绿线的方程: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
我会考虑线路从左边顶部和右上拐角的点的角度。如果它小于在这两种情况下45度(调整边缘的基础方向),则点是C.其它组合将覆盖其他三个三角形。
您实际上并不需要计算逆三角函数做到这一点,为各行的长度的比率为您提供足够的信息(和sin(45)...或相当罪(PI / 4)是固定值)。
不隶属于 StackOverflow