Post by Nicky Peter Hollyoake on Feb 5, 2017 19:42:44 GMT -5
I got bored from my assignments and decided to write a program, I seen someone do something similar before and decided to make my own version in Basic4GL. it's basically an infinity (Probably not) circles "growing" and always trying to find a spot to fill in space. I just thought it looked cool.
- Nicky
dim w=640, h=480
glMatrixMode(GL_PROJECTION)
glLoadidentity()
gluOrTho2d(0, w, 0, h)
glMatrixMode(GL_MODELVIEW)
glLoadidentity()
glLineWidth(2)
glEnable(GL_LINE_SMOOTH)
const ra#=(M_PI*2)/360.0
const co#=1.5
struc sCircle
x as double
y as double
r as double
endstruc
dim sCircle &circle(), sCircle &bCircle()
dim j, i
function r#()
return rnd()/(RND_MAX*1.0)
endfunction
function coll(x1#, y1#, x2#, y2#, r)
return pow(x2#-x1#, 2) + pow(y2#-y1#, 2) < r*r
endfunction
sub createCircle()
dim tx#, ty#, tr#
dim attempts=0
dim foundSpot=false
tr# = r#() * 10
tx#=((tr#+co#)/2)+r#()*(w-tr#-co#)
ty#=((tr#+co#)/2)+r#()*(h-tr#-co#)
do until attempts >= 1000 or &circle = NULL or foundSpot=true
tx#=(tr#/2)+r#()*(w-tr#)
ty#=(tr#/2)+r#()*(h-tr#)
foundSpot=true
for i = 0 to arraymax(circle)
if coll(tx#, ty#, circle(i).x, circle(i).y, circle(i).r+tr#+co#) then
foundSpot=false
i=arraymax(circle)
endif
next
attempts=attempts+1
loop
if attempts = 1000 then
return
endif
&bCircle=&circle
alloc circle, (arraymax(circle) + 1)
for i = 0 to arraymax(bCircle)
circle(i)=bCircle(i)
next
circle(i).r=tr#
circle(i).x=tx#
circle(i).y=ty#
endsub
sub drawCircles()
glBegin(GL_LINE_LOOP)
for i = 0 to 360 step 10
glVertex2f(circle(j).x + sin(ra#*i) * circle(j).r, circle(j).y + cos(ra#*i) * circle(j).r)
next
glEnd()
endsub
sub grow()
if circle(j).x - circle(j).r < co# or circle(j).x + circle(j).r > w-co# or circle(j).y - circle(j).r < co# or circle(j).y + circle(j).r > h-co# then
return
endif
for i = 0 to arrayMax(circle)
if i <> j then
if coll(circle(i).x, circle(i).y, circle(j).x, circle(j).y, circle(j).r+circle(i).r+co#) then
return
endif
endif
next
circle(j).r = circle(j).r + 1
return
endsub
while true
glClear(GL_COLOR_BUFFER_BIT)
createCircle()
for j = 0 to arrayMax(circle)
grow()
drawCircles()
next
swapbuffers()
sleep(25)
wend
- Nicky