Ooops
March 05, 2021, 07:33:18 AM

Author Topic: [bb] Filled Circle in 2 dimensional array by Pakz [ 1+ years ago ]  (Read 402 times)

Offline 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
  1. ; Filled circle in 2 dimensional array
  2. ; By Rudy van Etten in 2014
  3.  
  4. Graphics 640,480,32,2
  5. SetBuffer BackBuffer()
  6. SeedRnd MilliSecs()
  7. Const mapwidth = 39
  8. Const mapheight = 29
  9. Const tilewidth = 16
  10. Const tileheight = 16
  11. Dim map(mapwidth,mapheight)
  12.  
  13. Type point
  14.         Field x,y,radius,radmod
  15. End Type
  16.  
  17. makepoint(3)
  18.  
  19. Repeat
  20.         Cls
  21.         If KeyDown(1) = True Then End
  22.         Dim map(mapwidth,mapheight)
  23.         For this.point= Each point
  24.                 mapcircle(thisx,thisy,this
  25. adius)
  26.                 this
  27. adius = this
  28. adius + this
  29. admod
  30.                 If this
  31. adius > 9 Then this
  32. admod = -1
  33.                 If this
  34. adius < 2 Then this
  35. admod = 1
  36.         Next
  37.         drawmap()
  38.         Flip
  39.         For i=0 To 100
  40.                 Delay 1
  41.                 If KeyDown(1) = True Then End
  42.         Next
  43. Forever
  44.  
  45. End
  46.  
  47. Function mapcircle(x1,y1,radius)
  48.         For y2=-radius To radius
  49.         For x2=-radius To radius
  50.                 If (y2*y2+x2*x2) <= radius*radius+radius*0.8
  51.                         x3 = x1+x2
  52.                         y3 = y1+y2
  53.                         If x3>=0 And y3>=0 And x3<=mapwidth And y3<=mapheight
  54.                                 map(x3,y3) = 1
  55.                         End If
  56.                 End If
  57.         Next
  58.         Next
  59. End Function
  60.  
  61. Function makepoint(num)
  62.         For i=0 To num
  63.                 this.point = New point
  64.                 thisx = Rand(mapwidth)
  65.                 thisy = Rand(mapheight)
  66.                 If Rand(0,1) = 1
  67.                         this
  68. admod = 1
  69.                 Else
  70.                         this
  71. admod = -1
  72.                 End If
  73.                 this
  74. adius = Rand(1,9)
  75.         Next
  76. End Function
  77.  
  78. Function drawmap()
  79.         For y=0 To mapheight
  80.         For x=0 To mapwidth
  81.                 If map(x,y) = 0
  82.                         Color 0,0,0
  83.                 ElseIf map(x,y) = 1
  84.                         Color 255,255,255
  85.                 End If
  86.                 Rect x*tilewidth,y*tileheight,tilewidth,tileheight,True
  87.         Next
  88.         Next
  89. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal