高手请进:关于用vb画随机分布的圆
要求:用vb画随机分布的圆,这些圆分为好几种,每种圆个数和半径由excel中读取,要求所有圆中每两个圆不相交PrivateSubForm_click()Randomize...
要求:用vb画随机分布的圆,这些圆分为好几种,每种圆个数和半径由excel中读取,要求所有圆中每两个圆不相交
Private Sub Form_click()
Randomize
Cls
Dim MyWorkbook As Workbook
Set MyWorkbook = Workbooks.Open("D:\c.xls")
Dim R(100) As Long
Dim ArrayX(100)
Dim ArrayY(100)
Dim xn(10000) As Long
Dim yn(10000) As Long
Dim k As Integer, i As Integer, j As Integer, row As Integer, Ri As Integer
Ri = 1
'生成随机数列
For i = 1 To 10000
xn(i) = Rnd
yn(i) = Rnd
Next
For i = 1 To 10000
If Ri < xn(i) And xn(i) < 10000 - Ri And Ri < yn(i) And yn(i) < 10000 - Ri Then
ArrayX(1) = xn(i) '给第一个圆心坐标赋值
ArrayY(1) = yn(i)
End If
Next
'读取excel数据 并画圆
j = 2
For row = 1 To 4
A = MyWorkbook.Worksheets("sheet1").Cells(row, 1)
B = MyWorkbook.Worksheets("sheet1").Cells(row, 2)
For p = 1 To A
For i = 1 To 10000
k = 1
If B < xn(i) And xn(i) < 10000 - B And B < yn(i) And yn(i) < 10000 - B Then
For m = 1 To j - 1
If ((xn(i) - ArrayX(m)) ^ 2 + (yn(i) - ArrayY(m)) ^ 2) ^ 0.5 > B + R(m) Then '和先画的j个圆的位置进行对照
k = k + 1
End If
Next m
If k = j Then
ArrayX(j) = xn(i)
ArrayY(j) = yn(i)
Form1.Circle (ArrayX(j), ArrayY(j)), B, vbRed
R(j) = B
j = j + 1
End If
End If
Next i
Next p
Next row
End Sub
Private Sub Form_Load()
Form1.BackColor = vbBlack
Form1.Height = 10000
Form1.Width = 10000
End Sub
以上是我编的程序,运行不出来,请高手指点!!! 展开
Private Sub Form_click()
Randomize
Cls
Dim MyWorkbook As Workbook
Set MyWorkbook = Workbooks.Open("D:\c.xls")
Dim R(100) As Long
Dim ArrayX(100)
Dim ArrayY(100)
Dim xn(10000) As Long
Dim yn(10000) As Long
Dim k As Integer, i As Integer, j As Integer, row As Integer, Ri As Integer
Ri = 1
'生成随机数列
For i = 1 To 10000
xn(i) = Rnd
yn(i) = Rnd
Next
For i = 1 To 10000
If Ri < xn(i) And xn(i) < 10000 - Ri And Ri < yn(i) And yn(i) < 10000 - Ri Then
ArrayX(1) = xn(i) '给第一个圆心坐标赋值
ArrayY(1) = yn(i)
End If
Next
'读取excel数据 并画圆
j = 2
For row = 1 To 4
A = MyWorkbook.Worksheets("sheet1").Cells(row, 1)
B = MyWorkbook.Worksheets("sheet1").Cells(row, 2)
For p = 1 To A
For i = 1 To 10000
k = 1
If B < xn(i) And xn(i) < 10000 - B And B < yn(i) And yn(i) < 10000 - B Then
For m = 1 To j - 1
If ((xn(i) - ArrayX(m)) ^ 2 + (yn(i) - ArrayY(m)) ^ 2) ^ 0.5 > B + R(m) Then '和先画的j个圆的位置进行对照
k = k + 1
End If
Next m
If k = j Then
ArrayX(j) = xn(i)
ArrayY(j) = yn(i)
Form1.Circle (ArrayX(j), ArrayY(j)), B, vbRed
R(j) = B
j = j + 1
End If
End If
Next i
Next p
Next row
End Sub
Private Sub Form_Load()
Form1.BackColor = vbBlack
Form1.Height = 10000
Form1.Width = 10000
End Sub
以上是我编的程序,运行不出来,请高手指点!!! 展开
1个回答
展开全部
额,我不是高手。但我觉得你好像没有定义坐标系吧?圆心坐标的定义和窗体坐标系有关吧? 默认坐标的坐标圆点是在窗体左上角吧?也就是在窗体上的坐标都是处在坐标系的第四象限,也就是都为负值吧?而你的随机产生的圆心坐标都是正的。 也许就无法在窗体中出现了。
还有啊。
xn(i) = Rnd :yn(i) = Rnd
这些数值都是在[0,1)范围的数。。你是不是漏了? 如果是要取值在(0,10000)的话, 应该是 xn(i) = Rnd*10000: yn(i) = Rnd*10000 吧?
不知道对不对。
还是希望对你有帮助吧。如果有错误的地方请见谅。^_^
还有啊。
xn(i) = Rnd :yn(i) = Rnd
这些数值都是在[0,1)范围的数。。你是不是漏了? 如果是要取值在(0,10000)的话, 应该是 xn(i) = Rnd*10000: yn(i) = Rnd*10000 吧?
不知道对不对。
还是希望对你有帮助吧。如果有错误的地方请见谅。^_^
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询