Rhinoscript, Circle packing

This code generates a basic circle packing based on array of random points. Since the random points can be inside of circles the code will not check the collision and will ignore the point itself. Therefore the number of circles specified in the code is approximate. For further information please refer to this post and this post

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Option Explicit
'Script written by carlosdelab
'I dont rememmber when I wrote the code.
Call Main()
Sub Main()
	Dim Pt, R
 
	Pt = Array(rnd * 50, rnd * 50, 0)
	R = FuncCir(Pt, 10)
 
	Call Rhino.EnableRedraw(False)
	Call Pack(R)
	Call Rhino.EnableRedraw(True)
End Sub
 
Function Pack(R)
 
	Dim Pt, i, count
 
	count = Rhino.GetInteger(" number of circles ", 50, 2, 5000)
	If IsNull(count) Then Exit Function
 
	Dim ArrData()
 
	For i = 0 To count
		ReDim Preserve ArrData(i)
		ArrData(i) = R
 
		Pt = Array(Rnd * 50, rnd * 50, 0)
 
		Dim j
		Dim t: t = 0
 
		For j = 0 To UBound(ArrData)
			If Rhino.IsPointOnSurface(ArrData(j)(1)(0), Pt)Then
				Call Rhino.print("point in surface")
				t = t + 1
 
			End If
		Next
 
		If (t = 0) Then
			ReDim ArrLine(UBound(ArrData))
			Dim k, line
 
			For K = 0 To UBound(ArrData)
				line = Rhino.AddLine(Pt, Rhino.CircleCenterPoint(ArrData(k)(0)))
				ArrLine(k) = Line
 
			Next
 
			Dim d, d2, l
			d = funcDist(Pt, ArrData(0)(0), ArrLine(0))
 
			For l = 0 To UBound(ArrLine)
				d2 = funcDist(Pt, ArrData(l)(0), ArrLine(l))
 
				If d < d2 Then
					'Call Rhino.Print ("nothing")
				
				Else
					d = d2
 
				End If
 
			Next
 
			R = FuncCir(Pt, d)
			Call Rhino.DeleteObjects(ArrLine)
 
		End If
	Next
End Function
 
Function FuncCir(p, D)
 
	If D >= 10 Then
		D = 10
 
	End If
 
	Dim plane
	Dim R(1)
 
	plane = Rhino.PlaneFromFrame(p, Array(1.0, 0.0, 0.0), Array(0.0, 1.0, 0.0))	
	R(0) = Rhino.AddCircle(plane, D)
	R(1) = Rhino.AddPlanarSrf(Array(R(0)))
 
	Dim x: x = Rhino.ObjectMaterialSource(R(1)(0))
	Call Rhino.MaterialColor(x, RGB(0, Rnd * 255, Rnd * 255))
 
	FuncCir = R
 
End Function
 
Function funcDist(Pt, circleP, Line)
	Dim Ap, j, d
	Ap = Rhino.CurveCurveIntersection(circleP, Line)
 
	For j = 0 To UBound(Ap)
		d = Rhino.Distance(Pt, Ap(j, 1))
	Next
 
	funcDist = d
End Function

Leave a Reply