Author Topic: Networking  (Read 2010 times)

B+

  • Guest
Networking
« on: April 14, 2017, 08:46:42 PM »
Code: [Select]
' networking.bas  SmallBASIC 0.12.8 [B+=MGA] 2017-04-14
' hey Ashish at QB64, you have a fan!
' I just watched this:
' https://www.openprocessing.org/sketch/111878
' wow this place has some cool stuff, too bad can't do transparent triangles

def rand(lo, hi) = (rnd * (hi - lo + 1)) \ 1 + lo
def rdir = iff(rnd < .5, -1, 1)
def distance(x1, y1, x2, y2) = ( (x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5

nP = 85
dim p(nP), tri(7)

for i = 0 to nP
  p(i).x = rand(0, xmax)
  p(i).y = rand(0, ymax)
  p(i).dx = rdir * rnd * 6
  p(i).dy = rdir * rnd * 3.5
  p(i).c = rgb(rand(64, 255), rand(64, 255), rand(64, 255))
  p(i).r = rand(1, 4)
next

while 1
  cls
  for i = 0 to nP 'big show of points and triangle
    color p(i).c
    circle p(i).x, p(i).y, p(i).r filled
    for j = i + 1 to nP 'search for triangle points within 100 pixels
      if distance(p(i).x, p(i).y, p(j).x, p(j).y) < 130 then
        tri(0) = p(i).x : tri(1) = p(i).y
        tri(2) = p(j).x : tri(3) = p(j).y
        tri(6) = p(i).x : tri(7) = p(i).y
        for k = j + 1 to NP
          if distance(p(k).x, p(k).y, p(j).x, p(j).y) < 130 then
            tri(4) = p(k).x : tri(5) = p(k).y
            drawpoly tri 'filled 'or not filled ??
          end if
        next
      end if
    next
    'update points
    p(i).x = p(i).x + p(i).dx : p(i).y = p(i).y + p(i).dy
    if p(i).x < 0    then p(i).x = 0    : p(i).dx = p(i).dx * -1
    if p(i).x > xmax then p(i).x = xmax : p(i).dx = p(i).dx * -1
    if p(i).y < 0    then p(i).y = 0    : p(i).dy = p(i).dy * -1
    if p(i).y > ymax then p(i).y = ymax : p(i).dy = p(i).dy * -1
  next
  showpage
wend


B+

  • Guest
Re: Networking
« Reply #1 on: April 16, 2017, 04:46:36 PM »
Networking with trex, now we have tint, sort of:

Code: [Select]
'trex demo.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-16

def rand(lo, hi) = (rnd * (hi - lo + 1)) \ 1 + lo
def rdir = iff(rnd < .5, -1, 1)
def distance(x1, y1, x2, y2) = ( (x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5
def rclr = rgb(rand(64, 255), rand(64, 255), rand(64, 255))

sub trex(x, y, w, h)  'tinted rectangles
  for l.y = y to y + h step 2
    for l.x = x to x + w step 2
      pset l.x, l.y
    next
  next
end

nP = 40
dim p(nP)

for i = 0 to nP
  p(i).x = rand(0, xmax)
  p(i).y = rand(0, ymax)
  p(i).w = rand(50, 100)
  p(i).h = rand(50, 100)
  p(i).dx = rdir * rnd * 6
  p(i).dy = rdir * rnd * 3.5
  p(i).c = rclr
next

while 1
  cls
  for i = 0 to nP
    color p(i).c
    circle p(i).x, p(i).y, 2 filled
    for j = i + 1 to nP
      if distance(p(i).x, p(i).y, p(j).x, p(j).y) < 50 then
        trex p(i).x - .5*p(i).w, p(i).y - .5*p(i).h, p(i).w, p(i).h
        color p(j).c
        trex p(j).x - .5*p(j).w, p(j).y - .5*p(j).h, p(j).w, p(j).h
      end if
    next
    'update points
    p(i).x = p(i).x + p(i).dx : p(i).y = p(i).y + p(i).dy
    if p(i).x < 0    then p(i).x = 0    : p(i).dx = p(i).dx * -1
    if p(i).x > xmax then p(i).x = xmax : p(i).dx = p(i).dx * -1
    if p(i).y < 0    then p(i).y = 0    : p(i).dy = p(i).dy * -1
    if p(i).y > ymax then p(i).y = ymax : p(i).dy = p(i).dy * -1
  next
  showpage
wend

B+

  • Guest
Re: Networking
« Reply #2 on: April 16, 2017, 05:24:52 PM »
Now Networking with tinted circles:

Code: [Select]
'tcircs demos.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-16

def rand(lo, hi) = (rnd * (hi - lo + 1)) \ 1 + lo
def rdir = iff(rnd < .5, -1, 1)
def distance(x1, y1, x2, y2) = ( (x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5
def rclr = rgb(rand(64, 255), rand(64, 255), rand(64, 255))

sub tcirc(x, y, r)
  local l
  l.r2 = r * r
  for l.x = 0 to r step 2
    l.max = sqr(l.r2 - l.x * l.x)
    for l.y = 0 to l.max step 2
      pset x + l.x, y + l.y
      pset x - l.x, y + l.y
      pset x + l.x, y - l.y
      pset x - l.x, y - l.y
    next
  next
end

nP = 40
dim p(nP)

for i = 0 to nP
  p(i).x = rand(0, xmax)
  p(i).y = rand(0, ymax)
  p(i).r = rand(50, 100)
  p(i).dx = rdir * rnd * 6
  p(i).dy = rdir * rnd * 3.5
  p(i).c = rclr
next

while 1
  cls
  for i = 0 to nP
    color p(i).c
    circle p(i).x, p(i).y, 25
    for j = i + 1 to nP
      if distance(p(i).x, p(i).y, p(j).x, p(j).y) < 50 then
        tcirc p(i).x, p(i).y, p(i).r
        color p(j).c
        tcirc p(j).x, p(j).y, p(j).r
      end if
    next
    'update points
    p(i).x = p(i).x + p(i).dx : p(i).y = p(i).y + p(i).dy
    if p(i).x < 0    then p(i).x = 0    : p(i).dx = p(i).dx * -1
    if p(i).x > xmax then p(i).x = xmax : p(i).dx = p(i).dx * -1
    if p(i).y < 0    then p(i).y = 0    : p(i).dy = p(i).dy * -1
    if p(i).y > ymax then p(i).y = ymax : p(i).dy = p(i).dy * -1
  next
  showpage
wend


B+

  • Guest
Re: Networking
« Reply #3 on: April 16, 2017, 05:58:16 PM »
Tricolor test, step 2 and very blinky, step 3 and kind of dark:

Code: [Select]
' tricolor test.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-16

sub tcirc(x, y, r)
  local l
  l.r2 = r * r
  for l.x = 0 to r step 2
    l.max = sqr(l.r2 - l.x * l.x)
    for l.y = 0 to l.max step 2
      pset x + l.x, y + l.y
      pset x - l.x, y + l.y
      pset x + l.x, y - l.y
      pset x - l.x, y - l.y
    next
  next
end

xc = xmax/2 : yc = ymax/2 : a = 0
while 1
  cls
  for i = 0 to 2
    x = xc + 100 * cos(i*2*pi/3 + a)
    y = yc + 100 * sin(i*2*pi/3 + a)
    if i = 0 then
      color 12
    elif i = 1
      color rgb(0, 255, 0)
    else
      color 9
    fi
    tcirc x, y, 200
  next
  showpage
  a += 7*pi/180
wend
 

B+

  • Guest
Re: Networking
« Reply #4 on: April 16, 2017, 09:11:37 PM »
Oh with new update to SmallBASIC, I can plug in my array directly to Drawpoly without a variable name plus fix with 3rd distance check:
Code: [Select]
' networking.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-14
' 2017-04-16 modified for new array method for drawpoly
'            plus needed to check another distance!!!

def rand(lo, hi) = (rnd * (hi - lo + 1)) \ 1 + lo
def rdir = iff(rnd < .5, -1, 1)
def distance(x1, y1, x2, y2) = ( (x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5

nP = 85
dim p(nP)

for i = 0 to nP
  p(i).x = rand(0, xmax)
  p(i).y = rand(0, ymax)
  p(i).dx = rdir * rnd * 6
  p(i).dy = rdir * rnd * 3.5
  p(i).c = rgb(rand(64, 255), rand(64, 255), rand(64, 255))
next

while 1
  cls
  for i = 0 to nP 'big show of points and triangle
    color p(i).c
    circle p(i).x, p(i).y, 2 filled
    for j = i + 1 to nP 'search for triangle points within 100 pixels
      if distance(p(i).x, p(i).y, p(j).x, p(j).y) < 130 then
        for k = j + 1 to NP
          if distance(p(k).x, p(k).y, p(j).x, p(j).y) < 130 then
            if distance(p(k).x, p(k).y, p(i).x, p(i).y) < 130 then
              drawpoly [p(i).x, p(i).y, p(j).x, p(j).y, p(k).x, p(k).y, p(i).x, p(i).y]
            fi
          fi
        next
      fi
    next
    'update points
    p(i).x = p(i).x + p(i).dx : p(i).y = p(i).y + p(i).dy
    if p(i).x < 0    then p(i).x = 0    : p(i).dx = p(i).dx * -1
    if p(i).x > xmax then p(i).x = xmax : p(i).dx = p(i).dx * -1
    if p(i).y < 0    then p(i).y = 0    : p(i).dy = p(i).dy * -1
    if p(i).y > ymax then p(i).y = ymax : p(i).dy = p(i).dy * -1
  next
  showpage
wend

B+

  • Guest
Re: Networking
« Reply #5 on: April 16, 2017, 09:34:47 PM »
And now with tinted triangles:
Code: [Select]
' ttri networking.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-16
' triangle fill.bas  SmallBASIC 0.12.2 [B+=MGA] 2016-05-28

def rand(lo, hi) = (rnd * (hi - lo + 1)) \ 1 + lo
def rdir = iff(rnd < .5, -1, 1)
def distance(x1, y1, x2, y2) = ( (x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5

'Fast Filled Triangle Sub by AndyAmaya
sub ttri(x1, y1, x2, y2, x3, y3)
  local x, y, sp, yy, length, slope1, slope2, slope3
  'triangle coordinates must be ordered: where x1 < x2 < x3
  if x2 < x1 then swap x1, x2 : swap y1, y2
  if x3 < x1 then swap x1, x3 : swap y1, y3
  if x3 < x2 then swap x2, x3 : swap y2, y3
  if x1 <> x3 then slope1 = (y3 - y1) / (x3 - x1)
  'draw the first half of the triangle
  length = x2 - x1
  if length <> 0 then
    slope2 = (y2 - y1) / (x2 - x1)
    for x = 0 to length step 3
      if int(x * slope1 + y1) < int(x * slope2 + y1) then sp = 4 else sp = -4
      for yy = int(x * slope1 + y1) to int(x * slope2 + y1) step sp
        pset x + x1, yy
      next
    next
  end if
  'draw the second half of the triangle
  y = length * slope1 + y1 : length = x3 - x2
  if length <> 0 then
    slope3 = (y3 - y2) / (x3 - x2)
    for x = 0 To length step 3
      if int(x * slope1 + y) < int(x * slope3 + y2) then sp = 4 else sp = -4
      for yy = int(x * slope1 + y) to int(x * slope3 + y2) step sp
        pset x + x2, yy
      next
    next
  end if
end

nP = 85
dim p(nP)

for i = 0 to nP
  p(i).x = rand(100, xmax-100)
  p(i).y = rand(100, ymax-100)
  p(i).dx = rdir * rnd * 6
  p(i).dy = rdir * rnd * 3.5
  p(i).c = rgb(rand(64, 255), rand(64, 255), rand(64, 255))
next

while 1
  cls
  for i = 0 to nP 'big show of points and triangle
    color p(i).c
    circle p(i).x, p(i).y, 2 filled
    for j = i + 1 to nP 'search for triangle points within 100 pixels
      if distance(p(i).x, p(i).y, p(j).x, p(j).y) < 130 then
        for k = j + 1 to NP
          if distance(p(k).x, p(k).y, p(j).x, p(j).y) < 130 then
            if distance(p(i).x, p(i).y, p(k).x, p(k).y) < 130 then             
              ttri p(i).x, p(i).y, p(j).x, p(j).y, p(k).x, p(k).y
            fi
          fi
        next
      fi
    next
    'update points
    p(i).x = p(i).x + p(i).dx : p(i).y = p(i).y + p(i).dy
    if p(i).x < 0    then p(i).x = 0    : p(i).dx = p(i).dx * -1
    if p(i).x > xmax then p(i).x = xmax : p(i).dx = p(i).dx * -1
    if p(i).y < 0    then p(i).y = 0    : p(i).dy = p(i).dy * -1
    if p(i).y > ymax then p(i).y = ymax : p(i).dy = p(i).dy * -1
  next
  showpage
wend

Rick3137

  • Guest
Re: Networking
« Reply #6 on: April 18, 2017, 01:02:03 PM »
 Nice Work.

 There ought to be a way to make 3D games with this stuff. ::)

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: Networking
« Reply #7 on: April 19, 2017, 04:59:28 PM »
Nice Work.

 There ought to be a way to make 3D games with this stuff. ::)

Hehe, something like ...

B+

  • Guest
Re: Networking
« Reply #8 on: April 19, 2017, 08:12:45 PM »
Not as good as Cybermonkey's but anyone can post a picture  ;-))

Code: [Select]
'3d q trex demo.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-19

def rand(lo, hi) = (rnd * (hi - lo + 1)) \ 1 + lo
def rdir = iff(rnd < .5, -1, 1)
def distance(x1, y1, x2, y2) = ( (x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5

sub trex(x, y, w, h)  'tinted rectangles
  for l.y = y to y + h step 2
    for l.x = x to x + w step 2
      pset l.x, l.y
    next
  next
end

nP = 40
dim p(nP)

for i = 0 to nP
  p(i).x = rand(50, xmax-50)
  p(i).y = rand(50, ymax-50)
  p(i).w = rand(70, 100)
  p(i).h = rand(70, 100)
  p(i).dx = rdir * rnd * 6
  p(i).dy = rdir * rnd * 3.5
  p(i).r = rand(20, 105)
  p(i).g = rand(20, 105)
  p(i).b = rand(20, 105)
next

while 1
  cls
  for i = 0 to nP
    color rgb(p(i).r, p(i).g, p(i).b)
    pset p(i).x, p(i).y
    for j = i + 1 to nP
      if distance(p(i).x, p(i).y, p(j).x, p(j).y) < 70 then
        for k = 0 to 10
          color rgb(p(i).r + k*15, p(i).g + k*15, p(i).b + k*15)
          trex p(i).x - .5*p(i).w + 3*k, p(i).y - .5*p(i).h + 3*k, p(i).w + 3*k, p(i).h + 3*k
          color rgb(p(j).r + k*10, p(j).g + k*10, p(j).b + k*10)
          trex p(j).x - .5*p(j).w + 3*k, p(j).y - .5*p(j).h + 3*k, p(j).w + 3*k, p(j).h + 3*k 
        next
      end if
    next
    'update points
    p(i).x = p(i).x + p(i).dx : p(i).y = p(i).y + p(i).dy
    if p(i).x < 50      then p(i).x = 50      : p(i).dx = p(i).dx * -1
    if p(i).x > xmax-50 then p(i).x = xmax-50 : p(i).dx = p(i).dx * -1
    if p(i).y < 50      then p(i).y = 50      : p(i).dy = p(i).dy * -1
    if p(i).y > ymax-50 then p(i).y = ymax-50 : p(i).dy = p(i).dy * -1
  next
  showpage
wend


« Last Edit: April 19, 2017, 08:31:24 PM by B+ »

B+

  • Guest
Re: Networking
« Reply #9 on: April 20, 2017, 01:08:43 AM »
dah, faster and more solid with built-in rect:
Code: [Select]
'3d network with regular rect.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-19

def rand(lo, hi) = (rnd * (hi - lo + 1)) \ 1 + lo
def rdir = iff(rnd < .5, -1, 1)
def distance(x1, y1, x2, y2) = ( (x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ .5

nP = 40
dim p(nP)

for i = 0 to nP
  p(i).x = rand(50, xmax-50)
  p(i).y = rand(50, ymax-50)
  p(i).w = rand(70, 100)
  p(i).h = rand(70, 100)
  p(i).dx = rdir * rnd * 6
  p(i).dy = rdir * rnd * 3.5
  p(i).r = rand(20, 105)
  p(i).g = rand(20, 105)
  p(i).b = rand(20, 105)
next

while 1
  cls
  for i = 0 to nP
    color 7
    circle p(i).x, p(i).y, 1 filled
    for j = i + 1 to nP
      if distance(p(i).x, p(i).y, p(j).x, p(j).y) < 70 then
        for k = 0 to 30
          color rgb(p(i).r + k*5, p(i).g + k*5, p(i).b + k*5)
          rect p(i).x - .5*p(i).w + 1*k, p(i).y - .5*p(i).h + 1*k step p(i).w + 1.1*k, p(i).h + 1.1*k filled
          color rgb(p(j).r + k*5, p(j).g + k*5, p(j).b + k*5)
          rect p(j).x - .5*p(j).w + 1*k, p(j).y - .5*p(j).h + 1*k step p(j).w + 1.1*k, p(j).h + 1.1*k filled
        next
      end if
    next
    'update points
    p(i).x = p(i).x + p(i).dx : p(i).y = p(i).y + p(i).dy
    if p(i).x < 50      then p(i).x = 50      : p(i).dx = p(i).dx * -1
    if p(i).x > xmax-50 then p(i).x = xmax-50 : p(i).dx = p(i).dx * -1
    if p(i).y < 50      then p(i).y = 50      : p(i).dy = p(i).dy * -1
    if p(i).y > ymax-50 then p(i).y = ymax-50 : p(i).dy = p(i).dy * -1
  next
  showpage
wend
 
« Last Edit: April 20, 2017, 01:19:31 AM by B+ »