VB中怎样判断一个直线控件和图片框控件相交?
VB(visualbasic)中怎样判断一个直线控件(LINE)和图片框控件(picturebox)相交?...
VB(visual basic )中怎样判断一个直线控件(LINE)和图片框控件(picturebox)相交?
展开
2个回答
推荐于2016-05-09
展开全部
‘代码有点麻烦,图片宽为picture1,直线控件为line1
Private Sub Command1_Click()
Dim k
If Line1.X1 - Picture1.Left > 0 And Line1.X1 - Picture1.Left < Picture1.Width And Line1.Y1 - Picture1.Top > 0 And Line1.Y1 - Picture1.Top < Picture1.Height Or _
Line1.X2 - Picture1.Left > 0 And Line1.X2 - Picture1.Left < Picture1.Width And Line1.Y2 - Picture1.Top > 0 And Line1.Y2 - Picture1.Top < Picture1.Height Then '如果有一端点在图片框范围内则相交
MsgBox "直线与图片相交!"
ElseIf Line1.X1 = Line1.X2 Then '如果两端点坐标Y相同,下面情况会相交
If Not (Line1.X1 < Picture1.Left Or Line1.X1 - Picture1.Left > Picture1.Width Or Line1.Y1 < Picture1.Top And Line1.Y2 < Picture1.Top Or Line1.Y1 - Picture1.Top > Picture1.Height And Line1.Y2 - Picture1.Top > Picture1.Height) Then
MsgBox "直线与图片相交!"
Else
MsgBox "直线与图片不相交!"
End If
Else
k = (Line1.Y2 - Line1.Y1) / (Line1.X2 - Line1.X1)
If ((Picture1.Top - Line1.Y1) - k * (Picture1.Left - Line1.X1)) * ((Picture1.Top - Line1.Y1) - k * (Picture1.Left - Line1.X1)) * ((Picture1.Top - Line1.Y1) - k * (Picture1.Left - Line1.X1)) * ((Picture1.Top - Line1.Y1) - k * (Picture1.Left - Line1.X1)) >= 0 Then
If Not (Line1.X1 < Picture1.Left And Line1.X2 < Picture1.Left Or Line1.X1 > Picture1.Left + Picture1.Width And Line1.X2 > Picture1.Left + Picture1.Width Or Line1.Y1 < Picture1.Top And Line1.Y2 < Picture1.Top Or Line1.Y1 > Picture1.Top + Picture1.Height And Line1.Y2 > Picture1.Top + Picture1.Height) Then
MsgBox "直线与图片相交!"
Else
MsgBox "直线与图片不相交!"
End If
Else
MsgBox "直线与图片不相交!"
End If
End If
End Sub
追问
[热心网友]的答案貌似只对水平,或竖直的直线才正确,对于有斜率的直线似乎不准,不过还是采纳你……
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询