2d Circle Packing

Monkey Forums/Monkey Code/2d Circle Packing

Pakz(Posted 2017) [#1]


Made after watching a coding tutorial video on a youtube coding channel (The coding train)

What the code does is it adds circles on the screen and grows these until they either touch another circle or touch the border of the screen,

Here is the link to the channel :
https://www.youtube.com/user/shiffman/featured

Here the code :

Import mojo

Class circle
	Field x:Int,y:Int
	Field radius:Int
	Method New()
		radius = 2
		Local exitloop:Bool=False
		While exitloop = False
			exitloop=True
			Self.x = Rnd(DeviceWidth)
			Self.y = Rnd(DeviceHeight)
			For Local i:=Eachin mycircle
				If i.x = x And i.y = y Then
					Else
					If circleoverlap(	i.x,i.y,i.radius,
										x,y,2) = True Then
						exitloop=False
					End If
				End If
			Next
		Wend
	End Method
	Method grow()
		If x-radius<0 Then Return
		If x+radius>DeviceWidth Then Return
		If y-radius<0 Then Return
		If y+radius>DeviceHeight Then Return
		For Local i:=Eachin mycircle
			If i.x=x And i.y=y Then
			Else
				If circleoverlap(	x,y,radius,
									i.x,i.y,i.radius)
					Return
				End If 
			End If
		Next
		radius+=1
	End Method
	Method draw()
		SetColor 255,255,255
		DrawCircle x,y,radius
		SetColor 0,0,0
		DrawCircle x,y,radius-2
	End Method
	Function circleoverlap:Bool(x1:Int,y1:Int,r1:Int,x2:Int,y2:Int,r2:Int)
    	Local dx:Int = x1-x2
    	Local dy:Int = y1-y2
    	Local r:Int = r1+r2
    	If dx*dx+dy*dy <= r*r Then Return True Else Return False
	End Function	
End Class

Global mycircle:List<circle> = New List<circle>

Class MyGame Extends App
	Field counter:Int
    Method OnCreate()
        SetUpdateRate(5)
        mycircle.AddLast(New circle())
    End Method
    Method OnUpdate()   
    	If counter>100 Then Return
    	For Local i=0 Until 5
    		mycircle.AddLast(New circle())
    	Next
    	For Local i:=Eachin mycircle
    		i.grow
    	Next
    	counter+=1
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        For Local i:=Eachin mycircle
        	i.draw
        Next
    End Method
End Class


Function Main()
    New MyGame()
End Function



dubbsta(Posted 2017) [#2]
nice ive been watching him and eventually wanted try everything he does on monkey


Pakz(Posted 2017) [#3]
Yeah, The Coding Train is a good source for things to program. I noticed he makes around 1700 a month on patreon. He must be doing something right!

That language he uses (p5) looks interesting also. It has 3d too.


dubbsta(Posted 2017) [#4]
it does look cool, I've been looking at lua/love2d, would mind learning that and monkey. one thing i like about lua/love2d is the zerobrane ide it lets you live code with the corona extension, i would love that for monkey.

wow 1700$ I've seen his live shows it goes up to 1000 viewers in a matter of minutes the few times I've checked it out.

right now I'm learning trig and stuff before I get back to coding and put some projects on hold. I read somewhere math isn't your fav subject. there's a couple of sites khan academy of course and a new site I'm on which is pretty awesome schoolyourself.com, been picking stuff up pretty quick there.

I love 2d to me it will never die but would like to learn 3d someday, I feel like 3d is for bigger teams and not solo since there is so much involved but hey nothings impossible.


dubbsta(Posted 2017) [#5]
Would be cool if someone could start a patreon supported monkey live video tutorials
Like him