2013年6月23日日曜日

サンプル:バネの物理的挙動をシミュレートするプログラム?

バネの物理的挙動をシミュレートするプログラム・・・だそうです。
// http://d.hatena.ne.jp/x68000forever/20101123/1290520018 より
// 画面初期化 / screen initialize
apage(0)
wipe()
vpriority(TPAGE,SPAGE,GPAGE0,GPAGE1,GPAGE2,GPAGE3)
vpage(B_TPAGE+B_SPAGE+B_GPAGE0)
gborder(1,CTHYELLOW)
int by=0
switch deviceType()
 case DEVICE_IPODTOUCH: // iPod touch
 case DEVICE_IPHONE: // iPhone
 case DEVIDE_IPHONE_SIMURATOR
  width(40)
  by=32
endswitch
//
int c1,c2,t
float l1,l2,l3,k,m
float a,b,c,w1,w2,w3,xx1,xx2,xx3,x1,x2,x3,r2=1.4142135623731#
//
print localizedString("変位1=","Displacement1=");:input l1
print localizedString("変位2=","Displacement2=");:input l2
print localizedString("変位3=","Displacement2=");:input l3
print localizedString("バネ定数=","Rate of spring=");:input k
repeat
 print localizedString("質量(>0)=","Mass(>0)=");:input m
until m>0
//
w1=pow((2-r2)*k/m,0.5#)
w2=pow(2*k/m,0.5#)
w3=pow((2+r2)*k/m,0.5#)
a=(l1+r2*l2+l3)/4
b=(l1-l3)/2
c=(l1-r2*l2+l3)/4
//
c1=x68Color2iOSColor(61376):// hsv(30,31,31))
c2=x68Color2iOSColor(55358):// hsv(100,31,31))
gColor(c2)
line(  0,288+by,511,288+by)
gColor(c1)
line(130,285+by,130,291+by)
line(255,285+by,255,291+by)
line(380,285+by,380,291+by)
sp_clr()
sp_disp(0)
sprite_pattern():sprite_pallet()
sp_disp(1)
//
repeat
 x1=a*cos(w1*t)+b*cos(w2*t)+c*cos(w3*t)
 x2=r2*a*cos(w1*t)-r2*c*cos(w3*t)
 x3=a*cos(w1*t)-b*cos(w2*t)+c*cos(w3*t)
 locate(0,5):print "t=";t
 locate(0,6):print "x1=";x1,"x2=";x2,"x3=";x3
 xx1=130+x1:xx2=255+x2:xx3=380+x3
 locate(0,7):print "xx1=";xx1,"xx2=";xx2,"xx3=";xx3
 sp_halt(YES)
 sp_move( 0,xx1-16,256+by, 0)
 sp_move( 1,xx1   ,256+by, 1)
 sp_move( 2,xx1   ,272+by, 2)
 sp_move( 3,xx1-16,272+by, 3)
 sp_move( 4,xx2-16,256+by, 4)
 sp_move( 5,xx2   ,256+by, 5)
 sp_move( 6,xx2   ,272+by, 6)
 sp_move( 7,xx2-16,272+by, 7)
 sp_move( 8,xx3-16,256+by, 8)
 sp_move( 9,xx3   ,256+by, 9)
 sp_move(10,xx3   ,272+by,10)
 sp_move(11,xx3-16,272+by,11)
 sp_halt(NO)
 if t=0 then wait(1)
 t=t+1
 wait(0.1)
until t>1000
end
//
func sprite_pattern()
 int i,j
 dim char sp1(255),sp2(255),sp3(255),sp4(255)
 sp1={
  0,0,0,0,0,0,0,0,0,0,4,4,4,4,5,5,
  0,0,0,0,0,0,0,0,4,5,5,5,5,5,6,6,
  0,0,0,0,0,0,0,4,5,6,6,6,6,6,6,6,
  0,0,0,0,0,4,5,6,6,6,6,7,7,7,7,7,
  0,0,0,0,4,5,6,6,7,7,7,7,8,8,8,8,
  0,0,0,4,5,6,7,7,7,8,8,8,8,8,9,9,
  0,0,4,4,6,6,7,8,8,8,8,9,9,9,9,10,
  0,0,4,5,6,7,8,8,9,9,9,9,10,10,10,10,
  0,4,4,6,6,7,8,9,9,10,10,10,10,11,11,11,
  0,4,5,6,7,8,8,9,10,10,11,11,11,11,12,12,
  4,5,6,6,7,8,9,9,10,11,11,12,12,12,12,13,
  4,5,6,7,7,8,9,10,10,11,12,12,13,13,13,13,
  4,5,6,7,8,8,9,10,10,11,12,13,13,14,14,14,
  4,5,6,7,8,8,9,10,11,11,12,13,14,14,15,15,
  4,6,6,7,8,9,9,10,11,12,12,13,14,15,15,15,
  5,6,6,7,8,9,10,10,11,12,13,13,14,15,15,15
 }
 sp4={
  5,6,6,7,8,9,10,10,11,12,13,13,14,15,15,15,
  4,6,6,7,8,9,9,10,11,12,12,13,14,15,15,15,
  4,5,6,7,8,8,9,10,11,11,12,13,14,14,15,15,
  4,5,6,7,8,8,9,10,10,11,12,13,13,14,14,14,
  4,5,6,7,7,8,9,10,10,11,12,12,13,13,13,13,
  4,5,6,6,7,8,9,9,10,11,11,12,12,12,12,13,
  0,4,5,6,7,8,8,9,10,10,11,11,11,11,12,12,
  0,4,4,6,6,7,8,9,9,10,10,10,10,11,11,11,
  0,0,4,5,6,7,8,8,9,9,9,9,10,10,10,10,
  0,0,4,4,6,6,7,8,8,8,8,9,9,9,9,10,
  0,0,0,4,5,6,7,7,7,8,8,8,8,8,9,9,
  0,0,0,0,4,5,6,6,7,7,7,7,8,8,8,8,
  0,0,0,0,0,4,5,6,6,6,6,7,7,7,7,7,
  0,0,0,0,0,0,0,4,5,6,6,6,6,6,6,6,
  0,0,0,0,0,0,0,0,4,5,5,5,5,5,6,6,
  0,0,0,0,0,0,0,0,0,0,4,4,4,4,5,5
 }
 for i=0 to 255
  sp3(i)=sp1(255-i)
  sp2(i)=sp4(255-i)
 next
 sp_def(0 ,sp1)
 sp_def(1 ,sp2)
 sp_def(2 ,sp3)
 sp_def(3 ,sp4)
 sp_def(4 ,sp1)
 sp_def(5 ,sp2)
 sp_def(6 ,sp3)
 sp_def(7 ,sp4)
 sp_def(8 ,sp1)
 sp_def(9 ,sp2)
 sp_def(10,sp3)
 sp_def(11,sp4)
endfunc
//
func sp_color2(pal;int,colX68;int,pb;int)
// X68の色コードでスプライトの色を設定する / set sprite color by X68 color
 sp_color(pal,x68Color2iOSColor(colX68),pb)
endfunc
//
func sprite_pallet()
 sp_color(  0,    0,1)://sp_color2( 0,    1,1)
 sp_color2( 1, 4229,1)
 sp_color2( 2, 8457,1)
 sp_color2( 3,12685,1)
 sp_color2( 4,19027,1)
 sp_color2( 5,23255,1)
 sp_color2( 6,27483,1)
 sp_color2( 7,31711,1)
 sp_color2( 8,35939,1)
 sp_color2( 9,40167,1)
 sp_color2(10,44395,1)
 sp_color2(11,48623,1)
 sp_color2(12,52851,1)
 sp_color2(13,57079,1)
 sp_color2(14,61307,1)
 sp_color2(15,65535,1)
endfunc

func str localizedString(js;str,es;str)
 if isLocalizeJapan() then return(js)
 return (es)
endfunc

Zip archive file: XBetc.zip

0 件のコメント:

コメントを投稿