trex demo.bas

Here is networking with trex:

'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

From tinted rectangles to tinted circles:

'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

A little better stepping by 3's

' 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 3
l.max = sqr(l.r2 - l.x * l.x)
for l.y = 0 to l.max step 3
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

Well step 2 is less dark but step 3 draws faster, less blinky and you can draw more area per time unit.

Try 'em both ways!


' ttri networking.bas SmallBASIC 0.12.9 [B+=MGA] 2017-04-16
' tinted 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

'modified 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

Networking of course! ;-))

'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

Very cool effect... You are giving my cpu's a workout with this one, but a cool effect, none the less. Nicely done.

J

dah! much faster with built-in.

'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