Wednesday, November 28, 2012

Doppler efect

#Doppler efect
fastgraphics
x=0
loop:
for b = 1 to 10
color white
rect 0,0,300,300
x=x+1
a=.5
for n = 1 to 255 step 5
a=a*-1+.5
color rgb(255-a*n,255-a*n,255-a*n)
circle x + (n-b)*.5-255,150,255-(n-b)
next n
refresh
clg
next b
goto loop

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

Complex wave


Rem the purple wave is made out of two simple sinosoidal waves
fastgraphics
for t = 1 to 1000 step 0.1
clg
for n = 1 to 30  step 0.1
r=30*sin(t+n*0.2)
b=10*sin(t+n*0.8)
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

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

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

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

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

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

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$

Sphere

clg
For n = 1 to 150
color rgb(50+n,50+n,50+n)
circle rgb(150,150,150-n)
next n

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

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

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

Tuesday, December 21, 2010

Ramdom Walk

Rem http://en.wikipedia.org/wiki/Random_walk

clg
font "arial",20,100
graphsize 600,450
fastgraphics
stepsize=10
steps=0
For radius = 20 to 200 step 10
average=steps/10
rect 100+ radius*2,450-average,2,average
print "Radius="+radius;
print " Average number of steps="+average
steps=0
for trial = 1 to 10
x=0
y=0
color grey
rect 0,0,600,40
color yellow
circle 200,200,radius
color black
text 10,10,"Trial"+trial
while x^2+y^2 < radius^2
steps = steps+1
a=rand*2*pi
dx=cos(a)*stepsize
dy=sin(a)*stepsize
line 200+x,200+y,200+x+dx,200+y+dy
circle x+200,y+200,1
refresh
x=x+dx
y=y+dy
end while
next trial
next radius

Sunday, December 19, 2010

3D plotter


fastgraphics
clg : font "arial",15,100
graphsize 600,600
zoom=5 : max=5 : point=0
for z = -max to max step .1
for y = -max to max step .1
for x = -max to max step .1
Rem change this bit
if (x^2+y^2<1+z^2)then

if abs(x)>(max-.1) or abs(y)>(max-.1) or abs(z)>(max-.1) then rc=0
color rgb( 100+x*(100/max)+rand*rc,100+y*(100/max)+rand*rc,100+z*(100/max)+rand*rc)
point=point+1
gosub graph
else
if (abs(z)<0.01 and abs(y)<0.01) or (abs(x)<0.01 and abs(y)<0.01) or (abs(x)<0.01 and abs(z)<0.01) then
color black
if z>max-.1 then text sx+5,sy-10,"z="+max
if x>max-.1 then text sx+5,sy-10,"x="+max
if y>max-.1 then text sx+5,sy-10,"y="+max
refresh
gosub graph
end if
end if
next x
next y
refresh
next z
print "Aproximate volume ="+point/1000
graph:
prespective=1+(-3*max+x+y+z)/50
sx = 300+ (x*7-y*7)*zoom*prespective
sy = 300+ (-z*7+y*3+x*3)*zoom*prespective
circle sx,sy,2
rc=50
return

Saturday, December 18, 2010

Lorenz attractor


rem http://en.wikipedia.org/wiki/Lorenz_attractor
clg
fastgraphics
graphsize 900,300
s=10
p=28
b=8/3
x=10
y=0
z=10
zoom=6
dt=0.002
for n = 1 to 100000
dx=s*(y-x)
dy=x*(p-z)-y
dz=x*y-b*z
x=x+dx*dt
y=y+dy*dt
z=z+dz*dt
plot 150+zoom*y,300-zoom*z
plot 400+zoom*x,300-zoom*z
plot 650+zoom*x,150-zoom*y
refresh
next n

Wednesday, December 15, 2010

Rule 30

Rem http://en.wikipedia.org/wiki/Rule_30
clg
fastgraphics
graphsize 900,450
plot (450,1)
print pixel (10,10)
For y = 1 to 450
For x = 1 to 900
if pixel (x-1,y-1)=0 and pixel (x,y-1)<>0 and pixel (x+1,y-1)<>0 then plot(x,y)
if pixel (x-1,y-1)<>0 and pixel (x,y-1)=0 and pixel (x+1,y-1)=0 then plot(x,y)
if pixel (x-1,y-1)<>0 and pixel (x,y-1)=0 and pixel (x+1,y-1)<>0 then plot(x,y)
if pixel (x-1,y-1)<>0 and pixel (x,y-1)<>0 and pixel (x+1,y-1)=0 then plot(x,y)
next x
refresh
next y

100!

Rem Find the sum of the digits in the number 100! (Project Euler 20)
p=160
graphsize 1200,5
Dim Prod(p+1)
Prod[0]=1
for b = 1 to 100
for t = 0 to p-1
Prod[t]=Prod[t]*b
next t
Rem Carying
for x = 0 to p-2
While Prod[x]>9
Prod[x]=Prod[x]-10
Prod[x+1]=Prod[x+1]+1
end While
next x
Rem Printing
Print b+"! = ";
for x = 0 to p
Print Prod[p-x];
next x
print
next b
sum=0
for x = 0 to p-1
sum = sum + Prod[x]
next x
print sum

Tuesday, December 14, 2010

2^301

Rem 2^301 calculation

p=100
graphsize 1000,5
Dim Prod(p+1)
Prod[0]=1

for b = 0 to 300
for t = 0 to p-1
Prod[t]=Prod[t]*2
next t

Rem Carying
for x = 0 to p-2
While Prod[x]>9
Prod[x]=Prod[x]-10
Prod[x+1]=Prod[x+1]+1
end While
next x

Rem Printing
Print "2^"+(b+1)+" = ";
for x = 0 to p
Print Prod[p-x];
next x
print
next b

Monday, December 13, 2010

"Infinite" precision

Rem Infite multiplication precision algorithm http://en.wikipedia.org/wiki/Infinite_precision_arithmetic
max=10
p=2*max+2
graphsize 600,600
font "arial", 8,100
Dim n1(max+1)
Dim n2(max+1)
Dim Prod(p)
Rem {units ,tens ,hundreds ,etc }
n1={2,9,7,5,6,4,3,4,3,4}
n2={9,9,3,4,5,2,4,3,4,3}

Rem Multiplication
for y = 0 to max
for x = 0 to max
Prod[x+y]=Prod[x+y]+n1[x]*n2[y]
next x
next y

Rem Carying
for x = 0 to p-2
While Prod[x]>9
Prod[x]=Prod[x]-10
Prod[x+1]=Prod[x+1]+1
end While
next x

Rem Printing
for x = 0 to max
print n1[max-x];
next x
Print" x ";
for x = 0 to max
print n2[max-x];
next x
Print" = ";
p=p-1
for x = 0 to p
Print Prod[p-x];
next x

Wednesday, December 8, 2010

Euler 10

Rem Find the sum of all the primes below two million (Project Euler 10)
flag=0
sum=2
print 2+"....Sum="+sum
for h= 2 to 2000000
flag=0
i=1
while flag=0 and i < h^(1/2)
i=i+1
if int(h/i)=h/i then flag=1
end while
if flag=0 then
sum=sum+h
print (h)+"....Sum="+sum
end if
next h

Sunday, December 5, 2010

Black hole


graphsize 600,600
x=160
y=180
xspeed=2
yspeed=-1
r=2
n=400
dim x(n+1)
dim y(n+1)
dim xspeed(n+1)
dim yspeed(n+1)
dim dist(n+1)
dim dx(n+1)
dim dy(n+1)
dim xac(n+1)
dim yac(n+1)
dim gravity(n+1)
dim crash(n+1)
colision = 0
for p = 1 to n
x[p]=rand*600
y[p]=rand*600
next p
for s = 1 to n
xspeed[s]=rand*2-1
yspeed[s]=rand*2-1
next s
px=300
py=300
circle px,py,5
fastgraphics
loop:
clg

for t = 1 to n
dist[t]=((x[t]-px)^2 + (y[t]-py)^2)^(1/2)
gravity[t] = (r/dist[t])^2
dx[t]=x[t]-px
dy[t]=y[t]-py
xac[t]= (dx[t]/dist[t])*gravity[t]
yac[t]= (dy[t]/dist[t])*gravity[t]
if crash[t]=0 then xspeed[t]=xspeed[t]*.999-xac[t]
if crash[t]=0 then yspeed[t]=yspeed[t]*.999-yac[t]
if crash[t]=0 then x[t]=x[t]+xspeed[t]
if crash[t]=0 then y[t]=y[t]+yspeed[t]
if dist[t] < r and crash[t]=0 then
r=r+8/r^2
colision = colision+1
crash[t]=1
end if
text 10,10,"Colisions="+colision
circle x[t],y[t],2
next t
circle px,py,r
refresh
goto loop

Friday, December 3, 2010

Forest fire


fastgraphics
graphsize 600,600
font "Arial", 20,100
dim cell(101,101)
cell[50,50]=2
burned = 1
Rem probabilitity to catch fire
p=.3
for turn = 1 to 1000
For x = 2 to 99
For y = 2 to 99
if cell[x-1,y]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x,y-1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x,y+1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x+1,y]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x-1,y-1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x-1,y+1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x+1,y-1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x+1,y+1]=2 and cell[x,y]=0 and rand < p then cell[x,y]=3
if cell[x,y]=3 then burned = burned + 1
next y
next x
For x = 2 to 99
For y = 2 to 99
if cell[x,y]=0 then color green
if cell[x,y]=1 then color darkorange
if cell[x,y]=2 then color red
if cell[x,y]=3 then color yellow
if cell[x,y]>1 then cell[x,y]=cell[x,y]-1
rect 6*x,6*y,6,6
next y
next x
color blue
Text 10,10,"Cicle="+turn+" % burned trees=" +(burned/100)
refresh
next turn

Dragon curve

graphsize 900,900
color yellow
rect 0,0,900,900
color black
fastgraphics
order=15
Dim s(2^order+1)
s[1]=1 : s[2]=1
for n=2 to order
a= 2^n-1 : b=2^(n-1)+1
for g= a to b step -1
s[g]= abs(s[2^n-g]-1)
next g
s[2^n]=1
next n
Zoom =3 : angle=180
x1=110*Zoom : y1=90*Zoom
for g= 1 to 2^(order)-1
refresh
gosub lines
if s[g]=1 then angle =angle+90
if s[g]=0 then angle =angle-90
next g
lines:
r = (angle/180)*pi
y2=y1 + (sin (r))*Zoom
x2=x1 - (cos (r))*Zoom
line x1,y1,x2,y2
x1=x2
y1=y2
return