Dim x1 As Single, y1 As Single
Dim x2 As Single, y2 As Single
Dim x3 As Single, y3 As Single
Dim x4 As Single, y4 As Single
Dim m1 As Single, n1 As Single
Dim m2 As Single, n2 As Single
Dim flag As Integer
Dim r As Long
Dim d As Long
Dim s As IntegerPrivate Sub Command1_Click()
Unload Me
End SubPrivate Sub Command2_Click()
Picture1.Cls
x1 = x2 = x3 = x4 = 0
y1 = y2 = y3 = y4 = 0
flag = r = d = s = 0
End SubPrivate Sub Form_Load()
Form4.Height = MDIForm1.ScaleHeight
Form4.Width = MDIForm1.ScaleWidth
Form4.Top = 0
Form4.Left = 0
End SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim k As Long
Dim a As Long
Dim b As Long
Dim c As Long
If s = 0 Then
If flag = 0 Then
Picture1.Cls
x1 = x2 = x3 = x4 = 0
y1 = y2 = y3 = y4 = 0
x1 = X
y1 = Y
flag = 1
Else
x2 = X
y2 = Y
r = Sqr(Abs(x1 - x2) ^ 2 + Abs(y1 - y2) ^ 2)
Picture1.Circle (x1, y1), r
flag = 0
s = 1
End If
Else
If flag = 0 Then
x3 = X
y3 = Y
flag = 1
Else
x4 = X
y4 = Y
Picture1.Line (x3, y3)-(x4, y4)
flag = 0
s = 0
End If
End If
If (x1 * y1 * x2 * y2 * x3 * y3 * x4 * y4) <> 0 Then
k = (y4 - y3) / (x4 - x3)
d = Abs(k * x1 - y1 + y3 - k * x3) / Sqr(k ^ 2 + 1)
a = k ^ 2 + 1
b = -2 * (x1 + k ^ 2 * x3 + k * y1 - k * y3)
c = x1 ^ 2 + k ^ 2 * x3 ^ 2 - 2 * k * y3 * x3 + y3 ^ 2 + 2 * k * y1 * x3 - 2 * y1 * y3 + y1 ^ 2 - r ^ 2
If (d = r) Then
m1 = Sqr(c)
n1 = k * (m1 - x3) + y3
End If
If (d < r) Then
m1 = (-b - Sqr(Abs(b ^ 2 - 4 * a * c))) / (2 * a)
n1 = k * (m1 - x3) + y3
m2 = (-b + Sqr(Abs(b ^ 2 - 4 * a * c))) / (2 * a)
n2 = k * (m2 - x3) + y3
End If
Else
a = b = c = d = k = m1 = m2 = n1 = n2 = 0
End If
End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If m1 * n1 * m2 * n2 <> 0 Then
If (d = r) Then
Call catch(m1, n1, X, Y)
End If
If (d < r) Then
Call catch(m1, n1, X, Y)
Call catch(m2, n2, X, Y)
End If
End If
Call catch(x1, y1, X, Y)
Call catch(x3, y3, X, Y)
Call catch(x4, y4, X, Y)
Call catch((x3 + x4) / 2, (y3 + y4) / 2, X, Y)
End Sub
Private Sub catch(u As Single, v As Single, X As Single, Y As Single)
If ((u - X) * (u - X) + (v - Y) * (v - Y)) < 10000 Then
Picture1.Circle (u, v), 50, RGB(255, 0, 0)
Else
Picture1.Circle (u, v), 50, Picture1.BackColor
End If
End Sub
以上代码要事先画一个圆和一条直线,根据直线和圆的交点个捕捉交点,可是执行后就不能很准确地捕捉交点,不是哪里出错了,请哪位高手帮忙看一看,谢谢了!
Dim x2 As Single, y2 As Single
Dim x3 As Single, y3 As Single
Dim x4 As Single, y4 As Single
Dim m1 As Single, n1 As Single
Dim m2 As Single, n2 As Single
Dim flag As Integer
Dim r As Long
Dim d As Long
Dim s As IntegerPrivate Sub Command1_Click()
Unload Me
End SubPrivate Sub Command2_Click()
Picture1.Cls
x1 = x2 = x3 = x4 = 0
y1 = y2 = y3 = y4 = 0
flag = r = d = s = 0
End SubPrivate Sub Form_Load()
Form4.Height = MDIForm1.ScaleHeight
Form4.Width = MDIForm1.ScaleWidth
Form4.Top = 0
Form4.Left = 0
End SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim k As Long
Dim a As Long
Dim b As Long
Dim c As Long
If s = 0 Then
If flag = 0 Then
Picture1.Cls
x1 = x2 = x3 = x4 = 0
y1 = y2 = y3 = y4 = 0
x1 = X
y1 = Y
flag = 1
Else
x2 = X
y2 = Y
r = Sqr(Abs(x1 - x2) ^ 2 + Abs(y1 - y2) ^ 2)
Picture1.Circle (x1, y1), r
flag = 0
s = 1
End If
Else
If flag = 0 Then
x3 = X
y3 = Y
flag = 1
Else
x4 = X
y4 = Y
Picture1.Line (x3, y3)-(x4, y4)
flag = 0
s = 0
End If
End If
If (x1 * y1 * x2 * y2 * x3 * y3 * x4 * y4) <> 0 Then
k = (y4 - y3) / (x4 - x3)
d = Abs(k * x1 - y1 + y3 - k * x3) / Sqr(k ^ 2 + 1)
a = k ^ 2 + 1
b = -2 * (x1 + k ^ 2 * x3 + k * y1 - k * y3)
c = x1 ^ 2 + k ^ 2 * x3 ^ 2 - 2 * k * y3 * x3 + y3 ^ 2 + 2 * k * y1 * x3 - 2 * y1 * y3 + y1 ^ 2 - r ^ 2
If (d = r) Then
m1 = Sqr(c)
n1 = k * (m1 - x3) + y3
End If
If (d < r) Then
m1 = (-b - Sqr(Abs(b ^ 2 - 4 * a * c))) / (2 * a)
n1 = k * (m1 - x3) + y3
m2 = (-b + Sqr(Abs(b ^ 2 - 4 * a * c))) / (2 * a)
n2 = k * (m2 - x3) + y3
End If
Else
a = b = c = d = k = m1 = m2 = n1 = n2 = 0
End If
End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If m1 * n1 * m2 * n2 <> 0 Then
If (d = r) Then
Call catch(m1, n1, X, Y)
End If
If (d < r) Then
Call catch(m1, n1, X, Y)
Call catch(m2, n2, X, Y)
End If
End If
Call catch(x1, y1, X, Y)
Call catch(x3, y3, X, Y)
Call catch(x4, y4, X, Y)
Call catch((x3 + x4) / 2, (y3 + y4) / 2, X, Y)
End Sub
Private Sub catch(u As Single, v As Single, X As Single, Y As Single)
If ((u - X) * (u - X) + (v - Y) * (v - Y)) < 10000 Then
Picture1.Circle (u, v), 50, RGB(255, 0, 0)
Else
Picture1.Circle (u, v), 50, Picture1.BackColor
End If
End Sub
以上代码要事先画一个圆和一条直线,根据直线和圆的交点个捕捉交点,可是执行后就不能很准确地捕捉交点,不是哪里出错了,请哪位高手帮忙看一看,谢谢了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货