2010-02-22 52 views
1

Problem illustration http://i49.tinypic.com/2iui4g.jpg如何给定的矩形内识别子三角形在该矩形

鉴于宽度w和高度h的矩形的坐标。以及该矩形中的坐标x,y,我想确定我在哪个三角形内。 (0,A,1 = B,2 = C,3 = D) ),如果他们是在这个顺序。

我认为这应该像> =红线的公式和> =绿线的公式?

我想实现这个在VB.NET

+0

如果是这样的功课,然后请其标记为这样的(它不会停止的答案)。 – Richard 2010-02-22 12:29:04

+0

它不是作业,而是更大问题的一部分:http://stackoverflow.com/questions/2310334/texture-coordinate-mapping-how-to-map-coordinates-of-4-triangles-in-a-square- to-4 – PeanutPower 2010-02-22 12:31:40

回答

6
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"; 
} 
+0

ahh这看起来不错 – PeanutPower 2010-02-22 12:37:18

1

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

你实际上并不需要计算反向三角函数来完成这个任务,因为线长度的比例给你足够的信息(和sin(45)...或者sin(pi/4)是一个固定值)。

3

式绿线: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 
+0

谢谢我给了VB代码的upvote,但接受了Vlad的答案,因为他首先使用算法 – PeanutPower 2010-02-22 12:45:57