Page 1 of 1

Rotating rods

Posted: Tue Dec 07, 2021 8:24 am
by Henko
This is a variation of the spirograph program. The connected rods themselves are added to the screen. Furthermore, the various parameters can be set at the start of the program.
You may configure the rods yourself, or have them randomly generated.
The lengths of the rods are normalized in such a way that the total lenght of the rods equals half of the screen width.
The "speed" of a rod is the angular speed around the connecting point of that rod.
CF580F0C-1DFB-45AB-85AD-B2B71EAC82C0.png
CF580F0C-1DFB-45AB-85AD-B2B71EAC82C0.png (101.6 KiB) Viewed 2175 times

Code: Select all

' *** Connected, rotating rods ***
'     (touch screen to stop)
'y'
nr=8          ' number of (random) rods
size_min=0.4  ' minimum relative length of a rod (max.=1)
speed_max=3   ' maximum angular speed in degrees per iteration
rr=12         ' thickness of the rods
random=0      ' random rods (=0 : user defined rods with READ DATA)
orient=0      ' orientation is portrait (=1 : landscape)
iter=25        ' duration of each iteration in msec

' *** this section is used only when variable random=0)
data 0.8, 1   ' size and speed of the first (inner) rod
data 0.4, -3 ' 2nd (middle) rod
data 0.2, 5    ' 3rd (outer) rod
data 0.08, -7
data 0.08, 6.5
data 0.3, 8.5
'' ***

option angle degrees ! option base 1
if orient then set orientation landscape else set orientation portrait
set toolbar off
graphics ! graphics clear .8,.8,.8
draw size 1 ! draw color 0,0,0
get screen size sw,sh ! cx=sw/2 ! cy=sh/2

sum=0 ! iter/=1000 ! if not random then nr=4
dim size(nr),speed(nr),angle(nr)
for i=1 to nr       ' get sizes and rotation speeds of the rods
  
  if random then 
      size(i)=size_min+rnd(1-size_min)
      speed(i)=speed_max-2*rnd(1)*speed_max
    else ! read size(i),speed(i)
    end if
  stick("stick"&i,size(i),rr)
  sum+=size(i)
  next i
f=floor(nr^0.25)
scal=f*min(cx,cy)/sum  '  scaling factor
for i=1 to nr
  size(i)*=scal ! stick("stick"&i,size(i),rr)
  next i
draw to cx+scal*sum,cy

do                     '  let the rods rotate
  rot()
  draw line to rot.x , rot.y
  pause iter
  'slowdown
  until touch_x(0)>0
end

def stick(sn$,sl,t)
r=t/2
sprite sn$ begin sl,t
fill color .65,.35,.35
fill rect r,0 to sl,r
fill color .5,.2,.2
fill rect r,r to sl,t
fill color 0,0,0
fill circle r,t/2 size r
draw color 1,1,0 ! draw size 2
draw line r,.5*r to r,1.5*r
draw line .5*r,r to 1.5*r,r
sprite sn$ end
draw size 1 ! draw color 0,0,0 ! fill color 0,0,0
end def

def rot()
r=.rr/2 ! x=.cx ! y=.cy
for i=1 to .nr
  .angle(i)+=.speed(i) ! a=.angle(i) ! l=.size(i)/2-r
  dx=l*(1-cos(a)) ! dy=l*sin(a)
   sprite "stick"&i at x-dx,y-dy angle -.angle(i)
  sprite "stick"&i show
  x+=.size(i)*cos(.angle(i)) ! y-=.size(i)*sin(.angle(i))
  next i
end def

Re: Rotating rods

Posted: Tue Dec 07, 2021 12:15 pm
by smbstarv
A very funny program. Particularly with the random option !