RetroBASIC

Basicprogramming(.org) => Code and examples => Topic started by: B+ on April 19, 2017, 06:00:03 PM

Title: Star Vortex
Post by: B+ on April 19, 2017, 06:00:03 PM
Code: [Select]
'Star Vortex.sdlbas [B+=MGA] 2017-04-19
'Inspired by Andy Amaya's Neon Vortex
' do this with StarMaker adding spin and plasma to Vortex


' ++++++++++++     Instructions !   +++++++++++++
' Press Spacebar to change Plasma Coloring Setting
' Press Number keys 3 to 9 for that many pointed stars
'++++++++++++++++++++++++++++++++++++


option qbasic

const pi = acos(-1)
const radians = pi/180    'to convert an angle measured in degrees to and angle measure in radians, just mutiply by this
const xmax = 1200
const ymax = 700
const cx = xmax/2
const cy = ymax/2

common plasmaR, plasmaG, plasmaB, colorNumber
colorNumber = 0

setdisplay(xmax, ymax, 32, 1)
setcaption("Star Vortex:  press spacebar to change Plasma Color Scheme, press number key (3-9) for star Points")
autoback(-2)

function rrn(real)  ' sometimes you want a real number not just integer
rrn = real * (rnd(10001) -1)/10000
end function

function rdir()
if rnd(3)-1  then
rdir = 1
else
rdir = -1
end if
end function

function rand(lo, hi)
rand = rnd(hi - lo + 2) + lo - 1
end function

sub Star(x, y, rInner, rOuter, nPoints, angleOffset, TFfill)
    ' x, y are same as for circle,
    ' rInner is center circle radius
    ' rOuter is the outer most point of star
    ' nPoints is the number of points,
    ' angleOffset = angle offset IN DEGREES, it will be converted to radians in sub
    ' this is to allow us to spin the polygon of n sides
    ' TFfill filled True or False (1 or 0)
    pAngle = radians * (360 / nPoints)
radAngleOffset = radians * angleOffset
    x1 = x + rInner * cos(radAngleOffset)
    y1 = y + rInner * sin(radAngleOffset)
    for i = 0 to nPoints - 1
        x2 = x + rOuter * cos(i * pAngle + radAngleOffset + .5 * pAngle)
        y2 = y + rOuter * sin(i * pAngle + radAngleOffset + .5 * pAngle)
        x3 = x + rInner * cos((i + 1) * pAngle + radAngleOffset)
        y3 = y + rInner * sin((i + 1) * pAngle + radAngleOffset)
if TFfill then
triangle(x1, y1, x2, y2, x3, y3)
else
line(x1, y1, x2, y2)
line(x2, y2, x3, y3)
end if
        x1 = x3 : y1 = y3
    next
    if TFfill then
        fillcircle(x, y, rInner)
    end if
end sub

sub resetPlasma  'to work without palette array
    plasmaR = rrn(1) ^ 3 : plasmaG = rrn(1) ^ 3: plasmaB = rrn(1) ^ 3
end sub

sub setPlasma()
    'common colorNumber, plasmaR, plasmaG, plasmaB
    colorNumber = colorNumber + .75
    ink(rgb(127 + 127 * sin(plasmaR * colorNumber), 127 + 127 * sin(plasmaG * colorNumber), 127 + 127 * sin(plasmaB * colorNumber )))
end sub

'=================================================  main

nP = 5 : ao = 0
resetPlasma
while 1
    cls
if key(32) then : resetPlasma : end if
if key(k_3) then : nP = 3 :end if
if key(k_4) then : nP = 4 :end if
if key(k_5) then : nP = 5 :end if
if key(k_6) then : nP = 6 :end if
if key(k_7) then : nP = 7 :end if
if key(k_8) then : nP = 8 :end if
if key(k_9) then : nP = 9 :end if
    for i = 150 to 10 step -10
setPlasma
Star(cx, cy, i, 3 * i, nP, i + ao, 1)
    next
    screenswap
    'wait(80)
ao = ao + 3.5
wend

Title: Re: Star Vortex
Post by: B+ on April 19, 2017, 06:02:40 PM
SmallBASIC can do a finer toothed Star Vortex and maintain smooth rotation:
Code: [Select]
' Star Vortex.bas  SmallBASIC 0.12.9 [B+=MGA] 2017-04-19
' Inspired by Andy Amaya's recent Neon Vortex at SdlBasic

' ++++++++++++     Instructions !   +++++++++++++
' Press Spacebar to change Plasma Coloring Setting
' Press Number keys 3 to 9 for that many pointed stars
'++++++++++++++++++++++++++++++++++++

const cx = xmax/2
const cy = ymax/2

func rand(n1, n2)
  local l
  if n1 > n2 then l.hi = n1 : l.lo = n2 else l.hi = n2 : l.lo = n1
  rand = (rnd * (l.hi - l.lo + 1)) \ 1 + l.lo
end

func starArr(x, y, rInner, rOuter, nPoints, angleOffset, fillTF)
  ' x, y are same as for circle,
  ' rInner is center circle radius
  ' rOuter is the outer most point of star
  ' nPoints is the number of points,
  ' angleOffset = angle offset IN DEGREES, it will be converted to radians in sub
  ' this is to allow us to spin the polygon of n sides
  ' TFfill filled True or False (1 or 0)
  local p_angle, rad_angle_offset, x1, y1, i, arr
  dim arr()
  p_angle = rad(360 / nPoints)  :  rad_angle_offset = rad(angleOffset - 90)
  x1 = x + rOuter * cos(rad_angle_offset)
  y1 = y + rOuter * sin(rad_angle_offset)
  arr << x1 : arr << y1
  for i = 0 to nPoints - 1
    x1 = x + rInner * cos(i * p_angle + rad_angle_offset + .5 * p_angle)
    y1 = y + rInner * sin(i * p_angle + rad_angle_offset + .5 * p_angle)
    arr << x1 : arr << y1
    x1 = x + rOuter * cos((i + 1) * p_angle + rad_angle_offset)
    y1 = y + rOuter * sin((i + 1) * p_angle + rad_angle_offset)
    arr << x1 : arr << y1
  next
  if fillTF then drawpoly arr filled else drawpoly arr
  starArr = arr
end
 
sub resetPlasma() 'all globals
    plasmaR = rnd ^ 2 : plasmaG = rnd ^ 2 : plasmaB = rnd ^ 2 : plasmaN = 0
end

sub setPlasma() 'all globals
    plasmaN += .37
    cc = rgb(127+99*sin(plasmaR*plasmaN),127+99*sin(plasmaG*plasmaN),127+99*sin(plasmaB*plasmaN))
    color cc
end

nP = 5 : ao = 0
resetPlasma
while 1
  cls
  k = inkey
  if len(k) = 1 then
    if k = " " then resetPlasma
    if val(k) > 2 and val(k) <= 9 then nP = val(k)
  fi
  aoo = 0
  for i = 200 to 10 step -.5
    setPlasma
    s = StarArr( cx, cy, i, 3 * i, nP, i + ao, 1)
  next
  showpage
  'delay 80
  ao = ao - 3.5
wend