Dot Clusters

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Dot Clusters

Post by KristopherWindsor »

This is fun to play with: mouse + arrow keys.
Also try with max=50.

I made this prototype for part of another prototype I'm making. :P

Code: Select all

' Dot Clusters By Kristopher Windsor

#include once "fbgfx.bi"

const sx = 800, sy = 600

const max = 5 'circles total
const gs = 100 'line length

type p
  as double x, y
end type

dim shared as p c(1 to max)

randomize()
for i as integer = 1 to max
  with c(i)
    .x = sx * rnd()
    .y = sy * rnd()
  end with
next i

dim as integer mx, my, mb

screenres sx, sy, 32,, fb.GFX_ALPHA_PRIMITIVES

do
  'move
  for i as integer = 1 to max
    for j as integer = 1 to max
      if i = j then continue for
      var a = @c(i)
      var b = @c(j)
      var dist = sqr((a->x - b->x) ^ 2 + (a->y - b->y) ^ 2)
      if dist > .000001 then
        var grow = (10 + gs / dist) / 11
        var cx = (a->x + b->x) / 2
        var cy = (a->y + b->y) / 2
        a->x = cx + (a->x - cx) * grow
        a->y = cy + (a->y - cy) * grow
        b->x = cx + (b->x - cx) * grow
        b->y = cy + (b->y - cy) * grow
      end if
    next j
  next i
  
  getmouse(mx, my,, mb)
  if mb > 0 then c(1).x = mx : c(1).y = my
  if multikey(fb.sc_up) then c(2).y -= 20
  if multikey(fb.sc_down) then c(2).y += 20
  if multikey(fb.sc_left) then c(2).x -= 20
  if multikey(fb.sc_right) then c(2).x += 20
  
  'display
  screenlock()
  cls()
  for i as integer = 1 to max
    var j = (i mod max) + 1
    circle (c(i).x, c(i).y), 8, &HFFFFFFFF,,, 1, F
  next i
  for i as integer = 1 to max
    for j as integer = i + 1 to max
      if i = j then continue for
      line (c(i).x, c(i).y) - (c(j).x, c(j).y), &H400000FF
    next j
  next i
  screenunlock()
  
  sleep(18, 1)
loop until inkey() = chr(27)
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

Cool. Hours of entertainment value.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Post by dafhi »

worthy of closer inspection
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

Heh. In case the code isn't clear, I'm iterating over every pair of circles and moving them towards or away from each other, so that after several iterations, the distance between any two circles will approach some constant.
Of course it is not possible to reach this condition with a lot of circles, but they still cluster well. The prototype I'm making now is for some game idea. :]
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Post by AGS »

Set max to 100 (or more), compile, run and you´ll get to see a frantically 'bouncing' blue 'thing'. Movement is limited (it stays within the boundaries of a small square area) but it looks very dynamic.

If the thing would fire a random number of missiles (or something similar) at random intervals the thing could be used in a shoot 'em up.

Sorry about the abuse of your wonderful program, KristopherWindsor (but the blue 'thing' does look mighty fine).
Post Reply