[bb] Filled Circle in 2 dimensional array by Pakz [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : Filled Circle in 2 dimensional array
Author : Pakz
Posted : 1+ years ago

Description : I was reading the Java Gaming forum and saw a post with some useful code in it. I was code for the fog of war feature. It drew filled circles on coordinates in the map. I did not know how to do this yet. I used to create a circle with data statements and then check true or false for getting the circle shape. The method in the code below uses some math to create a filled circle. It can have a set radius.

I made a small example around it. This example can also be tweaked to becoming a map generator to :)


Code :
Code (blitzbasic) Select
; Filled circle in 2 dimensional array
; By Rudy van Etten in 2014

Graphics 640,480,32,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
Const mapwidth = 39
Const mapheight = 29
Const tilewidth = 16
Const tileheight = 16
Dim map(mapwidth,mapheight)

Type point
Field x,y,radius,radmod
End Type

makepoint(3)

Repeat
Cls
If KeyDown(1) = True Then End
Dim map(mapwidth,mapheight)
For this.point= Each point
mapcircle(thisx,thisy,thisadius)
thisadius = thisadius + thisadmod
If thisadius > 9 Then thisadmod = -1
If thisadius < 2 Then thisadmod = 1
Next
drawmap()
Flip
For i=0 To 100
Delay 1
If KeyDown(1) = True Then End
Next
Forever

End

Function mapcircle(x1,y1,radius)
For y2=-radius To radius
For x2=-radius To radius
If (y2*y2+x2*x2) <= radius*radius+radius*0.8
x3 = x1+x2
y3 = y1+y2
If x3>=0 And y3>=0 And x3<=mapwidth And y3<=mapheight
map(x3,y3) = 1
End If
End If
Next
Next
End Function

Function makepoint(num)
For i=0 To num
this.point = New point
thisx = Rand(mapwidth)
thisy = Rand(mapheight)
If Rand(0,1) = 1
thisadmod = 1
Else
thisadmod = -1
End If
thisadius = Rand(1,9)
Next
End Function

Function drawmap()
For y=0 To mapheight
For x=0 To mapwidth
If map(x,y) = 0
Color 0,0,0
ElseIf map(x,y) = 1
Color 255,255,255
End If
Rect x*tilewidth,y*tileheight,tilewidth,tileheight,True
Next
Next
End Function


Comments : none...