vb问题,我在VB窗体中用很多少数据,用line画了两条不规则的直线,怎样能求出两线之间的中间值
这两条线是用两组数据用画直线的方法画出来的,用的是line画直线指令。现在想知道有什么方法可以求出这两条线之间的中间值,并取出最大的中间值。请手指教。qq78474795...
这两条线是用两组数据用画直线的方法画出来的,用的是line画直线指令。现在想知道有什么方法可以求出这两条线之间的中间值,并取出最大的中间值。请手指教。qq784747951
如果能帮忙,定当感谢. 展开
如果能帮忙,定当感谢. 展开
2个回答
展开全部
“距离”有很多种定义,不知你是按那种定义的...
先计算线1上每个点到线2的距离,再判断这些距离。
下面是参考代码:
Option Base 1
Private Sub Command1_Click()
'初始化数据
Dim L1() As Single, L2() As Single
Dim D() As Single
ReDim L1(3, 2), L2(3, 2)
L1(1, 1) = 0: L1(1, 2) = 0
L1(2, 1) = 1: L1(2, 2) = 0
L1(3, 1) = 2: L1(3, 2) = 0
L2(1, 1) = 0: L2(1, 2) = 1
L2(2, 1) = 1: L2(2, 2) = 1.1
L2(3, 1) = 2: L2(3, 2) = 1
''''''''''
Dim i As Long, j As Long, a As Single, b As Single, p As Long
Dim LL1 As Long, UL1 As Long, LL2 As Long, UL2 As Long
LL1 = LBound(L1(), 1): UL1 = UBound(L1(), 1)
LL2 = LBound(L2(), 1): UL2 = UBound(L2(), 1)
ReDim D(LL1 To UL1, 2)
'得到L1线上各点到L2线的距离
'距离采用的是两点间距离
'你图上采用的好像高度距离,这个实现更容易
'当数据比较多时,可加入判断L2(j, 1)的范围以减少运算
For i = LL1 To UL1
a = 0: b = 0: p = LL2
a = (L1(i, 1) - L2(LL2, 1)) ^ 2 + (L1(i, 2) - L2(LL2, 2)) ^ 2
For j = LL2 + 1 To UL2
b = (L1(i, 1) - L2(j, 1)) ^ 2 + (L1(i, 2) - L2(j, 2)) ^ 2
If b < a Then a = b: p = j
Next j
D(i, 1) = p: D(i, 2) = Sqr(a)
Next i
'得到中间值最大的点和中间值
a = D(LL1, 2): p = LL1
For i = LL1 + 1 To UL1
If D(i, 2) > a Then a = D(i, 2): p = i
Next i
Print "L1线上的点:" & CStr(p)
Print "L2线上的点:" & CStr(D(p, 1))
Print "距离:" & CStr(D(p, 2))
End Sub
先计算线1上每个点到线2的距离,再判断这些距离。
下面是参考代码:
Option Base 1
Private Sub Command1_Click()
'初始化数据
Dim L1() As Single, L2() As Single
Dim D() As Single
ReDim L1(3, 2), L2(3, 2)
L1(1, 1) = 0: L1(1, 2) = 0
L1(2, 1) = 1: L1(2, 2) = 0
L1(3, 1) = 2: L1(3, 2) = 0
L2(1, 1) = 0: L2(1, 2) = 1
L2(2, 1) = 1: L2(2, 2) = 1.1
L2(3, 1) = 2: L2(3, 2) = 1
''''''''''
Dim i As Long, j As Long, a As Single, b As Single, p As Long
Dim LL1 As Long, UL1 As Long, LL2 As Long, UL2 As Long
LL1 = LBound(L1(), 1): UL1 = UBound(L1(), 1)
LL2 = LBound(L2(), 1): UL2 = UBound(L2(), 1)
ReDim D(LL1 To UL1, 2)
'得到L1线上各点到L2线的距离
'距离采用的是两点间距离
'你图上采用的好像高度距离,这个实现更容易
'当数据比较多时,可加入判断L2(j, 1)的范围以减少运算
For i = LL1 To UL1
a = 0: b = 0: p = LL2
a = (L1(i, 1) - L2(LL2, 1)) ^ 2 + (L1(i, 2) - L2(LL2, 2)) ^ 2
For j = LL2 + 1 To UL2
b = (L1(i, 1) - L2(j, 1)) ^ 2 + (L1(i, 2) - L2(j, 2)) ^ 2
If b < a Then a = b: p = j
Next j
D(i, 1) = p: D(i, 2) = Sqr(a)
Next i
'得到中间值最大的点和中间值
a = D(LL1, 2): p = LL1
For i = LL1 + 1 To UL1
If D(i, 2) > a Then a = D(i, 2): p = i
Next i
Print "L1线上的点:" & CStr(p)
Print "L2线上的点:" & CStr(D(p, 1))
Print "距离:" & CStr(D(p, 2))
End Sub
追问
回答的太好了谢谢你
追答
不用谢啦...
最主要要明确定义(规定),然后才是算法的选择...
不过,说实话,我水平也就这样...
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询