Saturday, December 29, 2012
Rosetta Code example
# This is a code example in basic 256 that you can find in Rosetta code website http://rosettacode.org/wiki/Greyscale_bars/Display. There you will find this program (and more) in many other languages such as Java Python and C, and that is a very good way to learn new programming languages.
h=ceil(graphheight/4)
for row=1 to 4
w=ceil(graphwidth/(8*row))
c=255/(8*row-1)
for n = 0 to (8*row-1)
color rgb(255-c*n,255-c*n,255-c*n)
if row/2 = int(row/2) then color rgb(c*n,c*n,c*n)
rect n*w,h*(row-1),w,h
next n
next row
Friday, December 28, 2012
Gears
clg
fastgraphics
dim x(3)
dim y(3)
dim rad(3)
dim init(3)
x[0]=150
y[0]=150
rad[0]=70
init[0]=0
x[1]=220
y[1]=220
rad[1]=35
init[1]=-.15
x[2]=90
y[2]=90
rad[2]=21
init[2]=.2
for rot = 0 to 100000
for w = 0 to 2
circle x[w],y[w],rad[w]-8
tri = {0, 0, -5, rad[w], 5, rad[w]}
for r = 0 to 2*pi step pi*7/rad[w]
if w=1 or w=2 then clock = -rot
if w=0 then clock = rot
stamp x[w],y[w],1,r+init[w]+clock/(7*rad[w]),tri
next r
next w
refresh
clg
next rot
Saturday, December 22, 2012
Flags 2
# Union jack
color blue
rect 0,0,300,300
color white
poly {0,20,0,0,20,0,300,280,300,300,280,300}
poly {280,0,300,0,300,20,20,300,0,300,0,280}
color red
poly {0,10,0,0,10,0,300,290,300,300,290,300}
poly {290,0,300,0,300,10,10,300,0,300,0,290}
color white
rect 120,0,60,300
rect 0,120,300,60
color red
rect 130,0,40,300
rect 0,130,300,40
pause 2
# Stars and Stripes
for n = 0 to 13 step 2
color red
rect 0,n*23,300,23
color white
rect 0,(n+1)*23,300,23
next n
font "arial",15,100
color blue
rect 0,0,120,7*23
color white
for x = 1 to 6
for y = 1 to 5
text x*18-8,y*34-27,"*"
next y
next x
for x = 1 to 5
for y = 1 to 4
text x*18,y*34-10,"*"
next y
next x
Rem Триколор
color white
rect 0,0,300,100
color blue
rect 0,100,300,100
color red
rect 0,200,300,100
Thursday, December 20, 2012
Train
# by C&F
fastgraphics
x=0
speed=1
loop:
x=x+speed
speed=speed+.03
if x>1 then speed=2
clg
pause .01
color black
if x > 400 then x=-100
rect 10+x,160,30,60
color orange
rect 10+x,195,120,50
color grey
circle 70+x,250,15
circle 110+x,250,15
circle 35+x,250,15
color black
circle 70+x,250,5
circle 110+x,250,5
circle 35+x,250,5
colour black
rect -30+x,220,40,10
color red
rect -100+x,195,75,50
color grey
circle -80+x,250,15
circle -40+x,250,15
color black
circle -80+x,250,5
circle -40+x,250,5
refresh
goto loop
fastgraphics
x=0
speed=1
loop:
x=x+speed
speed=speed+.03
if x>1 then speed=2
clg
pause .01
color black
if x > 400 then x=-100
rect 10+x,160,30,60
color orange
rect 10+x,195,120,50
color grey
circle 70+x,250,15
circle 110+x,250,15
circle 35+x,250,15
color black
circle 70+x,250,5
circle 110+x,250,5
circle 35+x,250,5
colour black
rect -30+x,220,40,10
color red
rect -100+x,195,75,50
color grey
circle -80+x,250,15
circle -40+x,250,15
color black
circle -80+x,250,5
circle -40+x,250,5
refresh
goto loop
Blimp
# Blimp by R
clg
fastgraphics
x=0
y=60
font "arial",15,50
loop:
x=x+.2
if x>300 then x = -220
color grey
circle 150-x,70+y,50
circle 120-x,70+y,50
circle 180-x,70+y,50
color black
rect 100-x,100+y,100,40
Text 120-x,70+y,"R&S Inc."
color white
circle 120-x,120+y,5
circle 150-x,120+y,5
circle 180-x,120+y,5
refresh
clg
goto loop
Cool person
# Cool person by R
clg
color 255,160,160
circle(150,50,50)
color 255,160,160
rect(146,100,10,10)
color blue
circle(130,40,5)
color blue
circle(170,40,5)
color yellow
x=120
y=15
poly {x+25,y+25,x+50,y+50,x+25,y+50}
color red
rect(140,80,30,10)
color green
rect(100,110,110,75)
color red
rect (130,75,10,10)
color red
rect (170,75,10,10)
Ambulance
# Ambulance by V
fastgraphics
x=0
y=0
loop:
x=x+1
if x>1000 then x = 0
colour darkgreen
rect 0,0,900,300
colour red
circle 950-x,225+y,10
color White
rect 910-x,270+y,60,20
rect 930-x,230+y,70,60
color red
rect 950-x,250+y,30,10
color red
rect 960-x,240+y,10,30
color black
circle 925-x,290+y,10
circle 990-x,290+y,10
refresh
clg
goto loop
fastgraphics
x=0
y=0
loop:
x=x+1
if x>1000 then x = 0
colour darkgreen
rect 0,0,900,300
colour red
circle 950-x,225+y,10
color White
rect 910-x,270+y,60,20
rect 930-x,230+y,70,60
color red
rect 950-x,250+y,30,10
color red
rect 960-x,240+y,10,30
color black
circle 925-x,290+y,10
circle 990-x,290+y,10
refresh
clg
goto loop
Sunday, December 2, 2012
Venny
#Venn diagrams and logical operators
Font "Arial",30,100
Font "Arial",30,100
For s = 1 to 5
clg
Text 20,20 ,"A"
Text 260,20 ,"B"
gosub circles
For n = 1 to 2000
x=rand*300
y=rand*300
A=0
B=0
gosub checkA
gosub checkB
if s = 1 then gosub one
if s = 2 then gosub two
if s = 3 then gosub three
if s = 4 then gosub four
if s = 5 then gosub five
next n
next s
end
one:
Text 140,250 ,"A"
if A=1 then circle x,y,2
return
two:
Text 100,250 ," A"+chr(1352)+"B"
if A=1 and B=1 then circle x,y,2
return
three:
Text 100,250 ," A"+chr(1357)+"B"
if A=1 or B=1 then circle x,y,2
return
four:
Text 120,250 ,"A'"
if not A=1 then circle x,y,2
return
five:
Text 100,250 ,"(A"+chr(1357)+"B)'"
if not (A=1 or B=1) then circle x,y,2
return
checkA:
if (x-100)^2+(y-150)^2<10000 then A=1
Return
checkB:
if (x-200)^2+(y-150)^2<10000 then B=1
Return
circles:
For x = 1 to 300
y=(-(x-200)^2+10000)^.5+150
circle x,y,2
circle x,-y+300,2
circle x-100,y,2
circle
x-100,-y+300,2
next x
return
Wednesday, November 28, 2012
Sunday, November 11, 2012
3D Bounce
graphsize 900,450
fastgraphics
vx=2
vy=3
vz=5
x=10
y=10
z=14
Loop:
color darkred
rect 0,0,900,450
color rgb(0,123,0)
poly{0,300,450,450,900,300,450,150}
color rgb(0,83,0)
poly{0,300,450,150,450,0,0,150}
color rgb(0,103,0)
poly{900,300,450,150,450,0,900,150}
color red
vz=vz-.05
x=x+vx
y=y+vy
z=z+vz
vz=vz-.05
h=(x+y)*cos(1/3)
v=300-(-x+y)*sin(1/3)
if x>465 then vx=-vx
if x<0 then vx=-vx
if y>465 then vy=-vy
if y<0 then vy=-vy
if z<0 then vz=-vz
color black
rect h,v,10,2
color red
circle h,v-z,10
refresh
clg
goto Loop
Friday, November 9, 2012
Golden Waves
graphsize 600,600
fastgraphics
for t=1 to 60 step .1
color darkred
rect 0,0,600,600
For y1 = 0 to 24
For x1 = 0 to 24
x=12*(24-x1)+12*y1
y=-6*(24-x1)+6*y1+300
d= ((10-x1)^2+(10-y1)^2)^.5
h=60*sin(x1/4+t)+65
if t>10 and t<20 then h=60*sin(y1/4+t)+65
if t>20 and t<30 then h=60*sin((x1-y1)/4+t)+65
if t>30 and t<40 then h=30*sin(x1/2+t)+30*sin(y1/2+t)+65
if t>40 and t<50 then h=60*sin((x1+y1)/4+t)+65
if t>50 and t<60 then h=60*sin(d*.3+t)+65
color rgb(100+h,100+h,h)
poly{x,y-h,x+10,y+5-h,x+20,y-h,x+10,y-5-h}
color rgb(60,60,0)
poly{x,y-h,x+10,y+5-h,x+10,y,x,y-5}
color rgb(150,150,0)
poly{x+10,y+5-h,x+10,y,x+20,y-5,x+20,y-h}
next x1
next y1
refresh
clg
next t
Thursday, November 1, 2012
Flags
# By C
color orange
rect (50,250,20,50)
color darkblue
rect (50,50,1000,220)
color orange
rect (100,270,20,30)
color red
rect (0,0,1000,750)
color white
rect (0,110,1000,100)
color blue
rect(0,200,1000,750)
Pause 2
color orange
rect (50,250,20,50)
color darkblue
rect (50,50,1000,220)
color orange
rect (100,270,20,30)
color red
rect (0,0,1000,750)
color white
rect (0,110,1000,100)
color red
rect(0,200,1000,750)
pause 2
color white
rect (0,0,10000,10000)
color red
rect 135,0,25,1000
color red
rect 0,135,2500,25
Pause 2
color black
rect (0,0,1000,750)
color red
rect (0,110,1000,100)
color yellow
rect(0,200,1000,750)
Pause 2
color green
rect 0,0,100,1000
color white
rect 100,0,100,1000
color orange
rect 200,0,100,1000
Pause 2
color darkgreen
rect 0,0,100,300
color red
rect 100,0,200,300
color yellow
circle 100,150,50
Wednesday, October 31, 2012
Walk on by
# A rough attempt to model human walking
clg
graphsize 900,300
Fastgraphics
Dim limb(8)
limb = {0, 0, 0, 100, 10, 100,10,0}
y=140
people=10
Dim x(people)
Dim dx(people)
Dim r(people)
Dim g(people)
Dim b(people)
For n=0 to people-1
x[n]=int(rand*900)
dx[n]=(-1)^int(rand*2+1)
r[n]=int(rand*255)
g[n]=int(rand*255)
b[n]=int(rand*255)
next n
For p=0 to 10000
gosub walkman
next p
walkman:
clg
For n=0 to people-1
color r[n],g[n],b[n]
x[n]=x[n]+dx[n]
if x[n]<0 then x[n]=900
if x[n]>900 then x[n]=0
ywable=-10*abs(sin(x[n]*.03+pi/2))
xwable=-30*abs(sin(x[n]*.03))
leftrot=sin(x[n]*.03)/2
rightrot=sin(x[n]*.03+pi)/2
circle x[n]-xwable+5,y+ywable-25,15
stamp x[n]-xwable,y+60+ywable,1,leftrot,limb
stamp x[n]-xwable,y+ywable,.8,leftrot,limb
color r[n],0,b[n]
rect x[n]-10-xwable,y+ywable,30,70
color rgb( r[n],g[n],b[n])
stamp x[n]-xwable,y+60+ywable,1,rightrot,limb
stamp x[n]-xwable,y+ywable,.8,rightrot,limb
next n
refresh
return
Saturday, October 20, 2012
Krypto - make the target number
# This program uses the new functions and subroutines from version 0.9.9.1
cls
clg
dim n(4)
dim op$(4)
target = int (rand*60)
n[0]=int (rand*10)+1
n[1]=int (rand*10)+1
n[2]=int (rand*10)+1
n[3]=int (rand*10)+1
graphsize 600,200
font "arial",40,100
text 0,20, "Target number is : "+ target
text 0,100, "Use only: "+ n[0]+", "+n[1]+", "+n[2]+", "+n[3]
counter=0
op$[0]="+"
op$[1]="-"
op$[2]="*"
op$[3]="/"
for turn = 1 to 2
For t1 = 0 to 3
For t2 = 0 to 3
For t3 = 0 to 3
For t4 = 0 to 3
For t5 = 0 to 3
For t6 = 0 to 3
For t7 = 0 to 3
if t1<>t3 and t3<>t5 and t5<>t7 and t1<>t5 and t3<>t7 and t1<>t7 then
a=eval(op$[t2],n[t1],n[t3])
c=eval(op$[t6],n[t5],n[t7])
ac=eval(op$[t4],a,c)
if ac = target and turn =2 then Print "("+ n[t1]+op$[t2]+n[t3]+")"+op$[t4]+"("+ n[t5]+op$[t6]+n[t7]+")="+ac
if ac = target and turn =1 then counter = counter +1
b=eval(op$[t4],n[t3],n[t5])
ba=eval(op$[t2],n[t1],b)
bac=eval(op$[t6],ba,n[t7])
if bac= target and turn =2 then Print "(("+ n[t1]+op$[t2]+"("+n[t3]+op$[t4]+n[t5]+"))"+op$[t6]+n[t7]+"="+bac
if bac = target and turn =1 then counter = counter +1
bc=eval(op$[t6],b,n[t7])
bca=eval(op$[t2],n[t1],bc)
if bca= target and turn =2 then Print n[t1]+op$[t2]+"(("+n[t3]+op$[t4]+n[t5]+")"+op$[t6]+n[t7]+")="+bca
if bca = target and turn =1 then counter = counter +1
ab=eval(op$[t4],a,n[t5])
abc=eval(op$[t6],ab,n[t7])
if abc= target and turn =2 then Print "(("+ n[t1]+op$[t2]+n[t3]+")"+op$[t4]+n[t5]+")"+op$[t6]+n[t7]+"="+abc
if abc = target and turn =1 then counter = counter +1
cb=eval(op$[t4],n[t3],c)
cba=eval(op$[t2],n[t1],cb)
if cba = target and turn =2 then Print n[t1]+op$[t2]+"("+n[t3]+op$[t4]+"("+n[t5]+op$[t6]+n[t7]+"))="+cba
if cba = target and turn =1 then counter = counter +1
end if
next t7
next t6
next t5
next t4
next t3
next t2
next t1
print "Number of solutions "+counter
input "press any key to get solutions",a
next turn
function eval(e$,x,y)
if e$="*" then eval = x*y
if e$="/" and y<>0 then eval = x/y
if e$="/" and y=0 then eval = 10000000
if e$="-" then eval = x-y
if e$="+" then eval = x+y
end function
Friday, October 12, 2012
Multiparticle collider
Rem 2D collisions
fastgraphics
m=6
n=m^2
Dim x(n)
Dim y(n)
Dim vx(n)
Dim vy(n)
Dim mass(n)
Dim colisionflag(n,n)
ed=1
u=0
rem initial variables
for a = 0 to m-1
for b = 0 to m-1
x[u]=50*a+10
y[u]=50*b+10
vx[u]=rand-.5
vy[u]=rand-.5
mass[u]=7
u=u+1
next b
next a
Rem main loop
While 1=1
for u = 0 to n-1
If x[u]<mass[u] or x[u]>(300-mass[u]) then vx[u]=-vx[u]
If y[u]<mass[u] or y[u]>(300-mass[u]) then vy[u]=-vy[u]
x[u]=x[u]+vx[u]
y[u]=y[u]+vy[u]
color rgb (u*7,0,255-u*7)
circle x[u],y[u],mass[u]
next u
refresh
clg
gosub colision
end while
colision:
rem collision detection
for u1 = 0 to n-2
for u2 = u1+1 to n-1
dx = x[u2]-x[u1]
dy = y[u2]-y[u1]
distance = (dx*dx+dy*dy)^.5
if distance < (mass[u1]+mass[u2]) then
if colisionflag[u1,u2]=0 then
rem vx and vy calc
ax=dx/distance
ay=dy/distance
va1=vx[u1]*ax+vy[u1]*ay
vb1=-vx[u1]*ay+vy[u1]*ax
va2=vx[u2]*ax+vy[u2]*ay
vb2=-vx[u2]*ay+vy[u2]*ax
vaP1=va1 + (1+ed)*(va2-va1)/(1+mass[u1]/mass[u2])
vaP2=va2 + (1+ed)*(va1-va2)/(1+mass[u2]/mass[u1])
vx[u1]=vaP1*ax-vb1*ay
vy[u1]=vaP1*ay+vb1*ax
vx[u2]=vaP2*ax-vb2*ay
vy[u2]=vaP2*ay+vb2*ax
colisionflag[u1,u2]=1
end if
else
colisionflag[u1,u2]=0
end if
next u2
next u1
return
Saturday, June 30, 2012
10 000 primes
clg
graphsize 500,500
color yellow
rect 0,0,500,500
color black
f=0
for h = 3 to 10000
if f=0 then
Prime = h-1
y = int(Prime/100)
x = Prime - y*100 -1
Rect 5*x,5*y,5,5
print Prime
end if
f=0
for i = 2 to int(h/2)+1 step 1
if int(h/i)=h/i then f=1
if int(h/i)=h/i then i=h/2
next i
next h
Friday, June 29, 2012
Collatz conjecture
Rem http://en.wikipedia.org/wiki/Collatz_conjecture#Methods_of_proof
graphsize 1000,400
for x = 1 to 1000
a=x
n=0
do
b=a
if a/2 = int(a/2) then
a = a/2
else
a=a*3+1
end if
n=n+8
line n-8,400-b/30,n,400-a/30
until a = 1
print "Initial number "+x+ " Iterations "+n/8
next x
Thursday, June 21, 2012
Image effects
# Image effects by aplying formula to RGB
# You need to have an image file 300 by 300 pixels
fastgraphics
clg
refresh
Graphsize 600,600
imgload 150,150,"Kennedy.png"
For x = 1 to 300
For y = 1 to 300
z = Pixel (x,y)
r = int(z/65536)
g = int ((z - r*65536)/256)
b = z - r*65536 - g*256 +1
gosub colors
gosub invert
gosub bw
next y
refresh
next x
colors:
r1=int(r/129)*255
g1=int(g/129)*255
b1=int(b/129)*255
color (r1,g1,b1)
plot x+300,y
return
invert:
r2=abs(255-r)
g2=abs(255-g)
b2=abs(255-b)
color (r2,g2,b2)
plot x,y+300
return
bw:
r3=(r+g+b)/3
g3=(r+g+b)/3
b3=(r+g+b)/3
color (r3,g3,b3)
plot x+300,y+300
return
Sunday, June 10, 2012
Tessellations
rem Tessellations program
graphsize 600,600
fastgraphics
l=180 : u=1: gosub tessellation
l=120 : u=1 :gosub tessellation
l=90 : u=2 :gosub tessellation
l=60 : u=2: gosub tessellation
l=60 : u=3: gosub tessellation
l=45 : u=4 : gosub tessellation
l=40 : u=3: gosub tessellation
l=36 : u=5: gosub tessellation
l=30 : u=4 :gosub tessellation
l=30 : u=6 :gosub tessellation
tessellation:
for i = 1 to 50
x1=300: y1=300: angle = 270
for b = 1 to 20
a = l*int(rand*2)-l/2
for k= 1 to u
angle = angle + a: gosub lines
next k
next b
refresh
next i
clg : pause 1: refresh
return
lines:
r = (angle/180)*pi
y2=y1 + (sin (r))*20: x2=x1 - (cos (r))*20
for n = 1 to 30
circle x1,y1,2: line x1,y1,x2,y2
next n
x1=x2: y1=y2
return
Wednesday, June 6, 2012
Cellular Automation
# This program was malfunctioning for a long time because I did not update the code to the New Basic 256 . It is very interesting to see how 2 very simple rules generate two very different patterns . One is a fractal and the other is chaotic
# You can change the rule by activating or deactivating the plotting instruction . See also http://mathworld.wolfram.com/ElementaryCellularAutomaton.html
fastgraphics
graphsize 800,400
For n = 1 to 2
plot (400,1)
For y = 1 to 400
For x = 1 to 800
a=0
if pixel(x-1,y)=black then a=a+1
if pixel(x,y)=black then a=a+10
if pixel(x+1,y)=black then a=a+100
if n=1 then gosub chaotic
if n=2 then gosub fractal
next x
refresh
next y
pause 1
clg
refresh
next n
chaotic:
if a=001 or a=110 or a =010 or a=100 then plot (x,y+1)
return
fractal:
if a=001 or a=010 or a=100 then plot (x,y+1)
return
Tuesday, September 27, 2011
Static Wave
Rem the purple wave is made out of two simple sinusoidal waves. Change the frequencies and amplitudes and see what happens .
fastgraphics
Amplitude1=30
Frequency1=2
Amplitude2=30
Frequency2=8
for t = 1 to 1000 step .1
clg
for n = 1 to 31.4 step 0.1
r=Amplitude1*sin(t )*Sin(n/(10/Frequency1))
b=Amplitude2*sin(t )*Sin(n/(10/Frequency2))
p= r+b
color red
circle 10*n,r+250,3
color blue
circle 10*n,b+200,3
color purple
circle 10*n,p+100,3
next n
refresh
next t
Tuesday, July 12, 2011
Sunday, June 19, 2011
Worksheet generator 1
Rem Algebra worksheet generator Linear equations
n=10
dim x(n)
dim a(n)
dim b(n)
dim c(n)
dim d(n)
for q = 0 to n-1
x[q]= int (rand*10+2)
a[q]= int (rand*10+5)
b[q]= int (rand*10+5)
c[q]= int (rand*5+2)
d[q]=a[q]*x[q]+b[q]-c[q]*x[q]
print (q+1)+") "+a[q]+"x+"+b[q]+" = "+c[q]+"x+"+d[q]
next q
input "press for solutions",a
for q = 0 to n-1
print (q+1)+") x="+x[q]
next q
Thursday, February 3, 2011
Enlargement
# Linear Transformation using a matrix T (enlargement by a scale factor of -1 )
n=20
fastgraphics
Dim v (2,n)
Dim f (2,n)
Dim t (2,2)
# Initial vectors V
for i = 1 to n-1
v[0,i]=rand*150:v[1,i]=rand*150
next i
# Changing a
for a = 1 to -1 step -.001
gosub rot
gosub mult
gosub plotting
next a
# Transformation Matrix
rot:
t[0,0]=a:t[0,1]=0
t[1,0]=0:t[1,1]=a
return
# F=V*T
mult:
for i = 1 to n-1
f[0,i]= v[0,i]*t[0,0]+v[1,i]*t[1,0]
f[1,i]= v[0,i]*t[0,1]+v[1,i]*t[1,1]
next i
return
# Ploting F
plotting:
clg
line 0,150,300,150
line 150,0,150,300
for i = 1 to n-1
circle f[0,i]+150,150-f[1,i],4
next i
refresh
return
n=20
fastgraphics
Dim v (2,n)
Dim f (2,n)
Dim t (2,2)
# Initial vectors V
for i = 1 to n-1
v[0,i]=rand*150:v[1,i]=rand*150
next i
# Changing a
for a = 1 to -1 step -.001
gosub rot
gosub mult
gosub plotting
next a
# Transformation Matrix
rot:
t[0,0]=a:t[0,1]=0
t[1,0]=0:t[1,1]=a
return
# F=V*T
mult:
for i = 1 to n-1
f[0,i]= v[0,i]*t[0,0]+v[1,i]*t[1,0]
f[1,i]= v[0,i]*t[0,1]+v[1,i]*t[1,1]
next i
return
# Ploting F
plotting:
clg
line 0,150,300,150
line 150,0,150,300
for i = 1 to n-1
circle f[0,i]+150,150-f[1,i],4
next i
refresh
return
Wednesday, February 2, 2011
Rotation
# Linear Tranformation (rotation)
n=20
fastgraphics
Dim v (2,n)
Dim f (2,n)
Dim t (2,2)
# Initial vectors V
for i = 1 to n-1
v[0,i]=rand*100:v[1,i]=rand*100
next i
# Changing a
for a = 1 to 2*pi step .001
gosub rot
gosub mult
gosub plotting
next a
# Transformation Matrix
rot:
t[0,0]=cos(a):t[0,1]=-sin(a)
t[1,0]=sin(a):t[1,1]=Cos(a)
return
# F=V*T
mult:
for i = 1 to n-1
f[0,i]= v[0,i]*t[0,0]+v[1,i]*t[1,0]
f[1,i]= v[0,i]*t[0,1]+v[1,i]*t[1,1]
next i
return
# Ploting F
plotting:
clg
line 0,150,300,150
line 150,0,150,300
for i = 1 to n-1
circle f[0,i]+150,150-f[1,i],4
next i
refresh
return
Sunday, January 30, 2011
Correlation analysis
# correlation analysis tool
n=7
decimal(3)
font "arial",10,100
graphsize 500,500
dim x(n) : dim y(n)
tx=0 : ty=0
x = {2,3,3,4,4,6,5}
y = {1,2,4,5,4,8,7}
for m = 0 to n-1
tx=tx+x[m] : ty=ty+y[m]
next m
meanx=tx/n : meany=ty/n
sdx=0 : sdy=0: sdxy=0
for m = 0 to n-1
sdx=sdx +(x[m]-meanx)^2
sdy=sdy +(y[m]-meany)^2
sdxy=sdxy +(x[m]-meanx)*(y[m]-meany)
next m
sx=(sdx/n)^.5
sy=(sdy/n)^.5
covxy=sdxy/n
r= covxy/(sx*sy)
m= covxy/sx^2
c= meany-m*meanx
# Standard deviation covariance correlation coefficient and line of best fit
Print "Sx="+sx
Print "Sy="+sy
Print "Sxy="+covxy
print "r="+r
print "y="+m+"x+"+c
# graph parameters
minx=-10 : maxx=10 : miny=-10 : maxy=40
rangex=(maxx-minx)
rangey=(maxy-miny)
for i= 0 to 10
line 0,i*50,500,i*50
line i*50,0,i*50,500
text i*50,480,minx+rangex*i/10
text 0,500-i*50,miny+rangey*i/10
next i
color red
for i= 0 to n-1
circle (x[i]-minx)*(500/rangex),500-(y[i]-miny)*(500/rangey),5
next i
color blue
for i= 0 to 500
circle i,500*(1+(-c+miny)/rangey)-m*(rangex/rangey)*(i+500*minx/rangex) ,2
next i
rangex=(maxx-minx)
rangey=(maxy-miny)
for i= 0 to 10
line 0,i*50,500,i*50
line i*50,0,i*50,500
text i*50,480,minx+rangex*i/10
text 0,500-i*50,miny+rangey*i/10
next i
color red
for i= 0 to n-1
circle (x[i]-minx)*(500/rangex),500-(y[i]-miny)*(500/rangey),5
next i
color blue
for i= 0 to 500
circle i,500*(1+(-c+miny)/rangey)-m*(rangex/rangey)*(i+500*minx/rangex) ,2
next i
Tuesday, January 25, 2011
Character frequency
dim a(123)
for n = 97 to 122
a[n]= count("The frequency of letters in text has often been studied for use in cryptography, and frequency analysis in particular. No exact letter frequency distribution underlies a given language, since all writers write slightly differently. Linotype machines sorted the letters' frequencies as etaoin shrdlu cmfwyp vbgkqj xz based on the experience and custom of manual compositors. Likewise, Modern International Morse code encodes the most frequent letters with the shortest symbols; arranging the Morse alphabet into groups of letters that require equal amounts of time to transmit, and then sorting these groups in increasing order, yields e it san hurdm wgvlfbk opjxcz yq. Similar ideas are used in modern data-compression techniques such as Huffman coding.", chr(n),true)
next n
for n = 97 to 122
print chr(n)+" "+a[n]
next n
Pie chart
rem favourite pet pie chart
font "arial",20,100
graphsize 600,300
n=4
total=0
dim freq(n)
dim cumfreq(n+1)
dim category$(n)
freq = {23,13,62,45}
category$ = {"Dog","Fish","Cat","Rabbit"}
dim arm(6)
arm = {0,0,1,150,-1,150}
for p = 0 to n-1
total = total + freq[p]
cumfreq[p+1]=total
next p
for i = 0 to 2*pi step .01
for p = 0 to n-1
if cumfreq[p]/total < i/(2*pi) then
color cumfreq[p]*500000
rect 400,p*30+20,20,20
text 440,p*30+15,category$[p]
end if
next p
stamp 150,150,1,i,arm
next i
font "arial",20,100
graphsize 600,300
n=4
total=0
dim freq(n)
dim cumfreq(n+1)
dim category$(n)
freq = {23,13,62,45}
category$ = {"Dog","Fish","Cat","Rabbit"}
dim arm(6)
arm = {0,0,1,150,-1,150}
for p = 0 to n-1
total = total + freq[p]
cumfreq[p+1]=total
next p
for i = 0 to 2*pi step .01
for p = 0 to n-1
if cumfreq[p]/total < i/(2*pi) then
color cumfreq[p]*500000
rect 400,p*30+20,20,20
text 440,p*30+15,category$[p]
end if
next p
stamp 150,150,1,i,arm
next i
Thursday, January 13, 2011
Times table tutor
graphsize 600,600
fastgraphics
font "arial",20,100
For question = 1 to 10
for x = 1 to 10
for y = 0 to 9
color blue
rect x*45,y*45,40,40
color white
text x*45,y*45,x+y*10
next y
next x
a=int (rand*10+1)
b=int (rand*10+1)
print a+"x"+b
refresh
say "Question"+question
say a+" ,times, "+b+","
while clickb = 0
pause .01
endwhile
answer = int(clickx/45) +10*int(clicky/45)
clickclear
if answer =a*b then
say "Yes, well done"
else
say "No, it is"+a*b
end if
next question
Monday, January 10, 2011
Line pattern
Graphsize 600,600
fastgraphics
clg
for pic = 1 to 30
a=122 : b=rand^2 : c=rand^2: d=rand^2
for x= 1 to 600
for y= 1 to 600
color a+a*sin(b*x),a+a*sin(c*x),a+a*sin(d*x)
plot x,y
next y
refresh
next x
next pic
fastgraphics
clg
for pic = 1 to 30
a=122 : b=rand^2 : c=rand^2: d=rand^2
for x= 1 to 600
for y= 1 to 600
color a+a*sin(b*x),a+a*sin(c*x),a+a*sin(d*x)
plot x,y
next y
refresh
next x
next pic
Saturday, January 1, 2011
Color plots
Graphsize 600,600
fastgraphics
clg
for pic = 1 to 2
for x= 1 to 600
for y= 1 to 600
# try different formulae for color using coordenates x and y
if pic = 1 then color ((x-300)^2+(y-300)^2)*3
if pic = 2 then color 300*abs((x-300)/(y-300))
plot x,y
next y
refresh
next x
next pic
fastgraphics
clg
for pic = 1 to 2
for x= 1 to 600
for y= 1 to 600
# try different formulae for color using coordenates x and y
if pic = 1 then color ((x-300)^2+(y-300)^2)*3
if pic = 2 then color 300*abs((x-300)/(y-300))
plot x,y
next y
refresh
next x
next pic
Friday, December 31, 2010
Caesar's cipher decryption
ciphr$="ňŭğŢűŸůųŮŦűŠůŧŸīğŠğłŠŤŲŠűğŢŨůŧŤűīğŠūŲŮğŪŭŮŶŭğŠŲğŠğłŠŤŲŠűĦŲğŢŨůŧŤűīğųŧŤğŲŧŨťųğŢŨůŧŤűīğłŠŤŲŠűĦŲğŢŮţŤğŮűğłŠŤŲŠűğŲŧŨťųīğŨŲğŮŭŤğŮťğųŧŤğŲŨŬůūŤŲųğŠŭţğŬŮŲųğŶŨţŤūŸğŪŭŮŶŭğŤŭŢűŸůųŨŮŭğųŤŢŧŭŨŰŴŤŲħŖŨŪŨůŤţŨŠĨ"
text$=""
shift=255
for n = 1 to length(ciphr$)
letter$= mid(ciphr$, n, 1)
m = asc(letter$)
text$=text$+chr(m-shift)
next n
print text$
Thursday, December 30, 2010
Caesar's cipher encryption
text$="In cryptography, a Caesar cipher, also known as a Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques(Wikipedia)"
shift=255
ciphr$=""
for n = 1 to length(text$)
letter$= mid(text$, n, 1)
m = asc(letter$)
ciphr$=ciphr$+chr(m+shift)
next n
print ciphr$
shift=255
ciphr$=""
for n = 1 to length(text$)
letter$= mid(text$, n, 1)
m = asc(letter$)
ciphr$=ciphr$+chr(m+shift)
next n
print ciphr$
Smarties
clg
ns=int(rand*500)
For n = 1 to ns
color int(rand*2)*255,int(rand*2)*255,int(rand*2)*255
circle rand*290+5,rand*290+5,5
next n
input "How many smarties?",wait
print ns
Wednesday, December 29, 2010
Loan payment plan
# For home buyers
# Loan payment plan ( fixed monthly)
# r ( anual interest rate 3.4% )
# l amount borrowed
# n duration in moths
r=0.034
l=180000
n=360
c=r/12
p=l*(c*(1+c)^n)/((1+c)^n-1)
amort = 0
tota=0
toti=0
for m = 1 to n
l=l-amort
inter = l*c
amort=p-inter
tota=tota+amort
toti=toti+inter
prest=inter+amort
print "Month ="+m+" Amort.="+int(amort*100)/100+" Intrest="+int(inter*100)/100+" Total="+int(prest*100)/100
next m
print
print "Monthly payment = "+p
Print "Total amortization = "+tota
Print "Total interest = "+toti
# Loan payment plan ( fixed monthly)
# r ( anual interest rate 3.4% )
# l amount borrowed
# n duration in moths
r=0.034
l=180000
n=360
c=r/12
p=l*(c*(1+c)^n)/((1+c)^n-1)
amort = 0
tota=0
toti=0
for m = 1 to n
l=l-amort
inter = l*c
amort=p-inter
tota=tota+amort
toti=toti+inter
prest=inter+amort
print "Month ="+m+" Amort.="+int(amort*100)/100+" Intrest="+int(inter*100)/100+" Total="+int(prest*100)/100
next m
print "Monthly payment = "+p
Print "Total amortization = "+tota
Print "Total interest = "+toti
Dodge Ball
# Use "a" and "d" to turn
# Avoid crashing the arrow
font "arial",20,100
fastgraphics
graphsize 600,600
dim a(6)
a = {3,0,5,10,0,10}
rad = -.6
px=50
py=550
x=550
y=50
turn =0
xspeed1=0
yspeed1=0
r=5
for t = 1 to 100000
dist=((x-px)^2 + (y-py)^2)^(1/2)
xspeed1=xspeed1-(x-px)*.01/abs(x-px)
yspeed1=yspeed1-(y-py)*.01/abs(y-py)
x=x+xspeed1 : y=y+yspeed1
z = key
a$="G"
if z = 65 then a$="L"
if z = 68 then a$="R"
if a$="L" and turn>-.2 then
turn =turn-.05
else
turn=turn*.95
end if
if a$="R" and turn<.2 then turn =turn+.05
rad = rad+turn
yspeed = sin (rad)*4
xspeed = cos (rad)*4
py = py + yspeed
px = px + xspeed
pause .01
clg
color yellow
rect 0,0,600,600
color black
text 100,10,"Points= "+t
stamp px, py, 2,rad+1.57, a
circle x,y,r
refresh
if px<0 or px > 600 or py < 0 or py > 600 or dist < r then
text 10,10,"Lost"
refresh
end
end if
if x<0 or x>600 then
xspeed1=-xspeed1
r=r+5
end if
if y < 0 or y > 600 then
yspeed1=-yspeed1
r=r+5
end if
next t
# Avoid crashing the arrow
font "arial",20,100
fastgraphics
graphsize 600,600
dim a(6)
a = {3,0,5,10,0,10}
rad = -.6
px=50
py=550
x=550
y=50
turn =0
xspeed1=0
yspeed1=0
r=5
for t = 1 to 100000
dist=((x-px)^2 + (y-py)^2)^(1/2)
xspeed1=xspeed1-(x-px)*.01/abs(x-px)
yspeed1=yspeed1-(y-py)*.01/abs(y-py)
x=x+xspeed1 : y=y+yspeed1
z = key
a$="G"
if z = 65 then a$="L"
if z = 68 then a$="R"
if a$="L" and turn>-.2 then
turn =turn-.05
else
turn=turn*.95
end if
if a$="R" and turn<.2 then turn =turn+.05
rad = rad+turn
yspeed = sin (rad)*4
xspeed = cos (rad)*4
py = py + yspeed
px = px + xspeed
pause .01
clg
color yellow
rect 0,0,600,600
color black
text 100,10,"Points= "+t
stamp px, py, 2,rad+1.57, a
circle x,y,r
refresh
if px<0 or px > 600 or py < 0 or py > 600 or dist < r then
text 10,10,"Lost"
refresh
end
end if
if x<0 or x>600 then
xspeed1=-xspeed1
r=r+5
end if
if y < 0 or y > 600 then
yspeed1=-yspeed1
r=r+5
end if
next t
Saturday, December 25, 2010
Mandelbrot
rem Zoomable Mandelbrot based in Joel Kahn's example
sz=600
font "arial",10,100
graphsize sz,sz
fastgraphics
kt=50:m=4.0
xmin=-2.1:xmax=.6:ymin=-1.5:ymax=1.5
gosub draw
input "press enter to zoom in",a$
xmin=-0.5:xmax=0:ymin=0.5:ymax=1
gosub draw
input "press enter to zoom in",a$
xmin=-0.15:xmax=-.05:ymin=0.9:ymax=1
gosub draw
input "press enter to zoom in ",a$
xmin=-0.14:xmax=-.13:ymin=0.98:ymax=.99
gosub draw
end
draw:
clg
refresh
dx=(xmax-xmin)/sz:dy=(ymax-ymin)/sz
for x=xmin to xmax step dx
for y=ymin to ymax step dy
k=0:a=0:b=0
do
# In the iteration z=z^2+c
#z=ai + b
#c=yi + x
tx=a^2-b^2+x
b=2*b*a+y
a=tx
d=a^2+b^2
k=k+1
until not (d <= m and k < kt)
color k*50000000
plot (x-xmin)/dx,(ymax-y)/dy
next y
refresh
next x
# grid
color black
for n= 0 to 9
line n*sz/10,sz,n*sz/10,0
text n*sz/10,5,xmin+(xmax-xmin)*n/10
text 5,n*sz/10,ymax-(ymax-ymin)*n/10
line sz,n*sz/10,0,n*sz/10
next n
refresh
return
Wednesday, December 22, 2010
Bifurcation Diagram
# http://en.wikipedia.org/wiki/Bifurcation_diagram
Graphsize 600,600
clg
fastgraphics
for c = 0 to 2 step 0.01
refresh
y=0
# Main sequence
for i=1 to 20
print "c="+c+" i="+i+" y="+y
y = y^2-c
circle 200*c,100*y+300,1
next i
next c
# graph c is the horizontal axis
for c = 0 to 600 step 40
for y = -300 to 300 step 50
text c,7,c/200
text 0,300-y,y/100
line c,0,c,600
line 0,y+300,600,y+300
next y
next c
refresh
end
Subscribe to:
Posts (Atom)

















