/* iアプリ, MIDP でのsin, cos. 拡張 API または三角関数の数表 Time-stamp: "2004/11/02 Tue 22:48 hig" Saburo Higuchi 2004 http://www.math.ryukoku.ac.jp/~hig/ プログラム解説等 http://sparrow.math.ryukoku.ac.jp/~hig/course/juniors_2004/06/ */ #if DOJA import com.nttdocomo.ui.*; #elif MIDP import javax.microedition.midlet.*; import javax.microedition.lcdui.*; #endif #if DOJA #elif MIDP #else // DoJa/MIDP Builder を使われる方へ: // まず, プログラムの先頭で, #define DOJA // などと定義する必要があります. // 以下の define 文は, #if - #endif の中に書かれていますので, // DoJa/MIDP Builder のプリプロセッサでは処理できません. // そこで, #if - #endif はやめて, // いらない方をJava の意味でコメント文にしてください. #endif #if DOJA #define MYALIGN +0 #define MYAP IApplication #define MYSTART start #define MYBLACK Graphics.getColorOfName(Graphics.BLACK) #define MYWHITE Graphics.getColorOfName(Graphics.WHITE) #define MYBLUE Graphics.getColorOfName(Graphics.BLUE) #define MYRED Graphics.getColorOfName(Graphics.RED) #define MYGREEN Graphics.getColorOfName(Graphics.GREEN) #define MYYELLOW Graphics.getColorOfName(Graphics.YELLOW) #define MYMAGENTA Graphics.getColorOfName(Graphics.MAROON) #define MYCYAN Graphics.getColorOfName(Graphics.AQUA) #define MYDIALOG Dialog #define MYLABEL Label #define MYADD add #define MYTEXT Text #elif MIDP #define MYALIGN ,Graphics.LEFT|Graphics.BOTTOM #define MYAP MIDlet #define MYSTART startApp #define MYBLACK 0 #define MYWHITE ( (255<<16) + (255<<8) + (255)) #define MYBLUE ( (0<<16) + (0<<8) + (255)) #define MYRED ( (255<<16) + (0<<8) + (0)) #define MYGREEN ( (0<<16) + (255<<8) + (0)) #define MYYELLOW ( (255<<16) + (255<<8) + (0)) #define MYMAGENTA ( (255<<16) + (0<<8) + (255)) #define MYCYAN ( (0<<16) + (255<<8) + (255)) #define MYDIALOG Alert #define MYLABEL StringItem #define MYADD append #define MYTEXT String #endif /** IApplication または MIDlet を継承するクラス */ public class ArcSample extends MYAP { /** 起動するときに呼ばれる メソッド. 必須. */ #if DOJA public void start(){ ArcCanvas cc=new ArcCanvas(this); Display.setCurrent(cc); } #elif MIDP public void startApp(){ ArcCanvas cc = new ArcCanvas(this); Display.getDisplay(this).setCurrent(cc); } public void pauseApp(){} public void destroyApp(boolean unconditional){} #endif } class ArcCanvas extends Canvas implements Runnable { ArcSample parent; int radius; int fheight; int width; int height; int originx; int originy; int thetamax=0; int deltatheta=5; int deltathetamax=10; ArcCanvas(ArcSample parent){ this.parent=parent; fheight=Font.getDefaultFont().getHeight(); width=getWidth(); height=getHeight(); originx=width/2; originy=height/2; if( originx < originy ){ radius=originx; } else { radius=originy; } Thread th = new Thread(this); th.start(); } public void paint(Graphics g){ g.setColor(MYWHITE); g.fillRect(0,0,getWidth(),getHeight()); // 画面を消す g.setColor(MYBLACK); // 数表による円弧 for(int theta=0; theta < thetamax ; theta+=deltatheta){ /* (x1,y1) と (x2,y2) の間に線分をひく */ g.drawLine( originx + (radius * cos(theta)) /magfactor,//x1 originy - (radius * sin(theta)) /magfactor,//y1 originx + (radius * cos(theta+deltatheta)) /magfactor,//x2 originy - (radius * sin(theta+deltatheta)) /magfactor//y2 ); /* g.drawLine( originx + radius * (cos(theta) /magfactor),//x1 originy - radius * (sin(theta) /magfactor),//y1 originx + radius * (cos(theta+deltatheta) /magfactor),//x2 originy - radius * (sin(theta+deltatheta) /magfactor)//y2 ); だとどうなるかな */ } g.fillRect(originx + (radius * cos(thetamax)) / magfactor, originy - (radius * sin(thetamax)) / magfactor, 4,4); for(int theta=0; theta < thetamax ; theta+=deltatheta){ /* (x1,y1) と (x2,y2) の間に線分をひく */ g.drawLine( originx + f(theta)*cos(theta)/magfactor,//x1 originy - f(theta)*sin(theta)/magfactor, originx + f(theta+deltatheta)*cos(theta+deltatheta)/magfactor,//x2 originy - f(theta+deltatheta)*sin(theta+deltatheta)/magfactor //y2 ); // 本当はキャッシュに入れておいたほうがいいでしょう } } int f(int theta){ return (radius*sin(theta*5))/magfactor; // cos(theta), sin(theta) は, /magfactor との組で使う. 最大で radius に収まるくらいに. } /** スレッドが start されると このメソッドに制御が移る */ public void run(){ while(true){ // 無限ループ thetamax=(thetamax+deltathetamax)%360; // 角度を進め repaint(); // paint() を再度呼び出す. try { Thread.sleep(100); // 100ミリ秒休む. } catch ( Exception e ) { // 割り込みがあっても何もしない. } repaint(); } } // Vodafone の場合, FixedPoint クラスの中で sin, cos などは提供されている. int sin(int n){ while( n<0 ){ n+=period; } return sin[n%period]; } int cos(int n){ while( n<0 ){ n+=period; } return cos[n%period]; } /** 2pi に相当する 度 */ private final int period=360; /** 分母 */ private final int magfactor=100000; // 100000.0*sin(n度) を長さ 360 の整数配列にしたもの // 本当は, sin の始め90個から sin, cos すべてを簡単に計算できるはず. private final int sin[]={ 0,1745,3489,5233,6975,8715,10452,12186,13917,15643, 17364,19080,20791,22495,24192,25881,27563,29237,30901,32556, 34202,35836,37460,39073,40673,42261,43837,45399,46947,48480, 49999,51503,52991,54463,55919,57357,58778,60181,61566,62932, 64278,65605,66913,68199,69465,70710,71933,73135,74314,75470, 76604,77714,78801,79863,80901,81915,82903,83867,84804,85716, 86602,87461,88294,89100,89879,90630,91354,92050,92718,93358, 93969,94551,95105,95630,96126,96592,97029,97437,97814,98162, 98480,98768,99026,99254,99452,99619,99756,99862,99939,99984, 100000,99984,99939,99862,99756,99619,99452,99254,99026,98768, 98480,98162,97814,97437,97029,96592,96126,95630,95105,94551, 93969,93358,92718,92050,91354,90630,89879,89100,88294,87461, 86602,85716,84804,83867,82903,81915,80901,79863,78801,77714, 76604,75470,74314,73135,71933,70710,69465,68199,66913,65605, 64278,62932,61566,60181,58778,57357,55919,54463,52991,51503, 49999,48480,46947,45399,43837,42261,40673,39073,37460,35836, 34202,32556,30901,29237,27563,25881,24192,22495,20791,19080, 17364,15643,13917,12186,10452,8715,6975,5233,3489,1745, 0,-1745,-3489,-5233,-6975,-8715,-10452,-12186,-13917,-15643, -17364,-19080,-20791,-22495,-24192,-25881,-27563,-29237,-30901,-32556, -34202,-35836,-37460,-39073,-40673,-42261,-43837,-45399,-46947,-48480, -50000,-51503,-52991,-54463,-55919,-57357,-58778,-60181,-61566,-62932, -64278,-65605,-66913,-68199,-69465,-70710,-71933,-73135,-74314,-75470, -76604,-77714,-78801,-79863,-80901,-81915,-82903,-83867,-84804,-85716, -86602,-87461,-88294,-89100,-89879,-90630,-91354,-92050,-92718,-93358, -93969,-94551,-95105,-95630,-96126,-96592,-97029,-97437,-97814,-98162, -98480,-98768,-99026,-99254,-99452,-99619,-99756,-99862,-99939,-99984, -100000,-99984,-99939,-99862,-99756,-99619,-99452,-99254,-99026,-98768, -98480,-98162,-97814,-97437,-97029,-96592,-96126,-95630,-95105,-94551, -93969,-93358,-92718,-92050,-91354,-90630,-89879,-89100,-88294,-87461, -86602,-85716,-84804,-83867,-82903,-81915,-80901,-79863,-78801,-77714, -76604,-75470,-74314,-73135,-71933,-70710,-69465,-68199,-66913,-65605, -64278,-62932,-61566,-60181,-58778,-57357,-55919,-54463,-52991,-51503, -50000,-48480,-46947,-45399,-43837,-42261,-40673,-39073,-37460,-35836, -34202,-32556,-30901,-29237,-27563,-25881,-24192,-22495,-20791,-19080, -17364,-15643,-13917,-12186,-10452,-8715,-6975,-5233,-3489,-1745}; // 100000.0*cos(n度) を長さ 360 の整数配列にしたもの private final int cos[]={ 100000,99984,99939,99862,99756,99619,99452,99254,99026,98768, 98480,98162,97814,97437,97029,96592,96126,95630,95105,94551, 93969,93358,92718,92050,91354,90630,89879,89100,88294,87461, 86602,85716,84804,83867,82903,81915,80901,79863,78801,77714, 76604,75470,74314,73135,71933,70710,69465,68199,66913,65605, 64278,62932,61566,60181,58778,57357,55919,54463,52991,51503, 50000,48480,46947,45399,43837,42261,40673,39073,37460,35836, 34202,32556,30901,29237,27563,25881,24192,22495,20791,19080, 17364,15643,13917,12186,10452,8715,6975,5233,3489,1745, 0,-1745,-3489,-5233,-6975,-8715,-10452,-12186,-13917,-15643, -17364,-19080,-20791,-22495,-24192,-25881,-27563,-29237,-30901,-32556, -34202,-35836,-37460,-39073,-40673,-42261,-43837,-45399,-46947,-48480, -49999,-51503,-52991,-54463,-55919,-57357,-58778,-60181,-61566,-62932, -64278,-65605,-66913,-68199,-69465,-70710,-71933,-73135,-74314,-75470, -76604,-77714,-78801,-79863,-80901,-81915,-82903,-83867,-84804,-85716, -86602,-87461,-88294,-89100,-89879,-90630,-91354,-92050,-92718,-93358, -93969,-94551,-95105,-95630,-96126,-96592,-97029,-97437,-97814,-98162, -98480,-98768,-99026,-99254,-99452,-99619,-99756,-99862,-99939,-99984, -100000,-99984,-99939,-99862,-99756,-99619,-99452,-99254,-99026,-98768, -98480,-98162,-97814,-97437,-97029,-96592,-96126,-95630,-95105,-94551, -93969,-93358,-92718,-92050,-91354,-90630,-89879,-89100,-88294,-87461, -86602,-85716,-84804,-83867,-82903,-81915,-80901,-79863,-78801,-77714, -76604,-75470,-74314,-73135,-71933,-70710,-69465,-68199,-66913,-65605, -64278,-62932,-61566,-60181,-58778,-57357,-55919,-54463,-52991,-51503, -50000,-48480,-46947,-45399,-43837,-42261,-40673,-39073,-37460,-35836, -34202,-32556,-30901,-29237,-27563,-25881,-24192,-22495,-20791,-19080, -17364,-15643,-13917,-12186,-10452,-8715,-6975,-5233,-3489,-1745, 0,1745,3489,5233,6975,8715,10452,12186,13917,15643, 17364,19080,20791,22495,24192,25881,27563,29237,30901,32556, 34202,35836,37460,39073,40673,42261,43837,45399,46947,48480, 50000,51503,52991,54463,55919,57357,58778,60181,61566,62932, 64278,65605,66913,68199,69465,70710,71933,73135,74314,75470, 76604,77714,78801,79863,80901,81915,82903,83867,84804,85716, 86602,87461,88294,89100,89879,90630,91354,92050,92718,93358, 93969,94551,95105,95630,96126,96592,97029,97437,97814,98162, 98480,98768,99026,99254,99452,99619,99756,99862,99939,99984}; } /* Local Variables: mode: java End: */