サンプルプログラム

 

一次元セルオートマトン
/*
2006/11/21 一次元セルオートマトンによる図形描画
3ビットによる8個のパターン(000 001 010 ・・・・)
自分の真上とその左右を含めた3ビットの状態により自分のビットが決まると言う単純なもの
rule2(?)=0 or 1 の部分で 自分のビットの決め方を決めている
repeat〜loop のところで 自分より上のビットを読んで 8個のうちのどのパターンなのかを読み取り
対応するrule2()から1か0かのデータを読み取り書きこむというもの

*/

	window_x=400		;画面のサイズが変わるよ
	window_y=400

	screen 0,window_x,window_y
	title "フラクタル図形ーセルオートマトン"
	color 0,0,0 : boxf : color 255,255,255
	
	rule_many = 8
;************** ここ大事! Y=0のところに 点を打つことで図形が変わる
		
	pset 200,0

;	pset 40,0


;**********************************************************************

;element="000,001,010,011,100,101,110,111"
	
	dim rule2,rule_many

	rule2(0)=0			;この辺を変えるとめちゃ面白くなる
	rule2(1)=1
	rule2(2)=0
	rule2(3)=1
	rule2(4)=1
	rule2(5)=0
	rule2(6)=1
	rule2(7)=0
	
	repeat window_y-1						;実際に描画を行うところ
	y=cnt+1
		repeat window_x-2
		a=0 : b=0 : c=0
		x=cnt+1
		pget x-1,y-1
		if ginfo_r=255 : a=1
		pget x,y-1
		if ginfo_r=255 : b=1
		pget x+1,y-1
		if ginfo_r=255 : c=1

		if a+b+c==0 : inp = 0 : goto*tobasi
		if a+b+c==3 : inp = 7 : goto*tobasi
		if a+b+c==1 : {
			if a == 1 :inp=4 :goto*tobasi
			if b==1 : inp=2 :goto*tobasi
			if c==1 : inp=1:goto*tobasi
		}
		if (a=1) and (b=1) : inp=6 : goto*tobasi
		if (a=1) and (c=1) : inp=5 : goto*tobasi
		inp=3
		
*tobasi
		color rule2(inp)*255,rule2(inp)*255,rule2(inp)*255
		pset x,y
	
		loop
		await 5
	loop
	
	stop


 

3次元的にグラフを表示する
	randomize
	dim x,10,300
	dim b,10,300
	a=50 : wariai=13 : xx=2 : yy=2 : w1=1
	screen 0,400,400 : title "3次元乱数グラフ"
	cls 2

	objsize 48,25
	pos 302,10 : button gosub "sizex+",*xx1
	pos 350,10 : button gosub "sizex-",*xx2
	pos 302,35 : button gosub "sizey+",*yy1
	pos 350,35 : button gosub "sizey-",*yy2
	pos 302,60 : button gosub "up",*zz1
	pos 350,60 : button gosub "down",*zz2
	objsize 80,25
	pos 302,85 : button gosub "負極on/off",*zz
	pos 302,110 : button gosub "line on/off",*wi
	pos 302,135 : button gosub "block on/off",*w2

*main
	redraw 2
	color 0,0,0 : boxf 0,0,300,400 : color 255,255,255
	pos 0,0 : color 200,200,200 :print "sizex:"+xx+" sizey:"+yy+" 上昇率:"+(13-wariai)
repeat 10
e=cnt

	a(e)=a(e)+rnd(26)-wariai
	if z=0 : if a(e)<1 : a(e)=1
	b(e,0)=a(e)
		repeat 256
		b(e,cnt+1)=x(e,cnt)			;次に渡すために保存
		x(e,cnt)=b(e,cnt)			;前のをもらう
		color 256-cnt,e*25,255-e*25
		if w1=1:line cnt+40-e*xx,200-x(e,cnt)+cnt/4+e*yy,cnt+40-e*xx,200+cnt/4+e*yy
		if w=1 :line cnt+40-e*xx,200-x(e,cnt)+cnt/4+e*yy,cnt+40-e*xx+1,200-b(e,cnt+1)+(cnt+1)/4+e*yy
		loop
	
loop
	redraw 1
	wait 1
	goto*main

*xx1:xx+:return
*xx2:xx-:return
*yy1:yy+:return
*yy2:yy-:return
*zz1:wariai-:return
*zz2:wariai+:return
*zz:if z=0 : z=1 :else :z=0
	return
*wi:if w=0 : w=1 : else : w=0
	return
*w2:if w1=0 : w1=1 : else : w1=0
	return

 

 

滑らかに移動
		/*	目的地入力 現在地との差を計算 一定時間での移動で加速度計算   ※最高速度の設定
		2005/9/7       */
	
	time=30					//移動時間 ただし 半分までの移動速度
	Vmax=5					//最高速度
	//現在地設定
	x=400 : y=200
	//目的地設定
	lastx=400 : lasty=225
	repeat
	redraw 2
	color 255,255,255:boxf:color 0,0,0
	pos x,y
	print "slow move"

//キー入力 キー入力時に加速度を計算
	getkey key,1
	if (key=1) and (k=0) :{
		lastx=mousex : lasty=mousey				;遊び
		k=1
		t=0 : ax=0.0 : ay=0.0 :sx=0.0 : sy=0.0 : tousokux=0 : tousokuy=0 	;各種初期化
		x0=x : y0=y
		xx=0 : yy=0							;運動物体の移動距離 初期は0 最後にはsx,syの値になる
		sx=0.0+lastx-x						;移動距離 x & y
		sy=0.0+lasty-y

		ax=sx/(time*time)						;時間tでS移動するために必要な加速度a
		ay=sy/(time*time)
	/*
		Vmax_x=ax*time							;最大速度
		Vmax_y=ay*time
		if Vmax_x>Vmax : tousokux=1			;最大速度が 最大設定速度より大きければ 等速運動する
		if Vmax_y>Vmax : tousokuy=1
	*/
		}

	if k!0 : t+		
	if k=1: {			//x方向への加速運動
		xx=ax*t*t/2							;初速度0 加速度による加速運動のみ
		x=x0+xx

		yy=ay*t*t/2							;初速度0 加速度による加速運動のみ
		y=y0+yy
		if t>=time :vx=ax*t : vy=ay*t : x0=x : y0=y : k=2 : t=0	;目的時間だけ移動したら 加速度を逆向きに直し 減加速度運動へ
		}

	if k=2 : {			//x,y方向への減加速運動
		xx=vx*t-ax*t*t/2					;前回の速度を初速度に 逆方向に加速度をはたらかせる
		x=x0+xx

		yy=vy*t-ay*t*t/2
		y=y0+yy
		if t>=time : k=0 :x=lastx : y= lasty  ;割りきれなかった場合にちゃんと 最終目的地に着くようにする
	}
	
	
	await 5
	redraw 1
	loop

 

滑らかに移動 sinバージョン
	/*	目的地入力 現在地との差を計算 sinを利用した移動バージョン   ※最高速度の設定
		2005/9/7       */

	x=300 : y=300			//現在位置
	lastx=100 : lasty=100	//目的地
	p=3.141592				//円周率
	z=0.0 : a=0.0 : t=0.0	//実数を使うので 実数として使えるように 実数の数字を無意味でも入れておく
	repeat
	redraw 2
	color 255,255,255 : boxf :color 0,0,0
	pos x,y
	print "slow move"

	if k!0 : t=t+0.015			;多くすれば早くなるよ
	getkey key,1
	if (key=1) and (k=0):{
		lastx=mousex : lasty=mousey		;遊び
		x0=x							;移動する前の場所を保存しておく
		y0=y
		xx=lastx-x				;移動しなきゃいけない距離がxxとyyです
		yy=lasty-y
		
		k=1
		}
	if k=1 : {
		x=sin(t*p-p/2)*xx/2+xx/2+x0		;-1→+1へとsinの値が変化する それに移動量xxをかけて 移動量xxの半分をたす
		y=sin(t*p-p/2)*yy/2+yy/2+y0
		if t>=1.0 :x=lastx :y=lasty:t=0.0:k=0
	}

	await 5
	redraw 1
	loop

 

戻る ホームへ