コード public class Applet3DSakura extends Applet implements Runnable{ int AppletX = 500; //アプレットサイズ(x方向) int AppletY = 500; //アプレットサイズ(y方向) double Vertex_data[][] = {{-1, 1, 0}, //点P0 { 0, -1, 0}, //点P1 { 1, 1, 0} };//点P2 double Screen_data[][] = new double[3][3];//表示頂点 int scale = 100; //描画倍数 double phi; //x軸周りの回転角 double theta; //y軸周りの回転角 double rx, ry, rz; //回転後の頂点座標 Thread thread; Point point; //表示したい点の座標 /*初期メソッド*/ public void init(){ setSize(AppletX, AppletY); phi = 0; theta = 0; point = new Point(AppletX / 2, AppletY / 2); } /*runメソッド呼び出し*/ public void start(){ thread = new Thread(this); thread.start(); } /*runメソッド*/ public void run(){ for(int i = 0; i < 100; i++){ try{ Thread.sleep(100); } catch(InterruptedException e){ } phi += Math.PI / 10; //theta += Math.PI / 10; setVertexPosition(); repaint(); } } /*頂点の座標を更新するメソッド*/ public void setVertexPosition(){ for(int i = 0; i < Vertex_data.length; i++){ //点を回転させる rx = Vertex_data[i][0] * Math.cos(theta) + Vertex_data[i][2] * Math.sin(theta); ry = Vertex_data[i][0] * Math.sin(phi) * Math.sin(theta) + Vertex_data[i][1] * Math.cos(phi) - Vertex_data[i][2] * Math.cos(phi) * Math.cos(theta); rz = - Vertex_data[i][0] * Math.cos(phi) * Math.sin(theta) + Vertex_data[i][1] * Math.sin(phi) + Vertex_data[i][2] * Math.cos(phi) * Math.cos(theta); Screen_data[i][0] = point.x + (rx * scale); Screen_data[i][1] = point.y - (ry * scale); Screen_data[i][2] = rz; } } /*描画メソッド*/ public void paint(Graphics g1){ Graphics2D g2 = (Graphics2D)g1; QuadCurve2D.Double curve1 = new QuadCurve2D.Double( Screen_data[0][0], Screen_data[0][1], Screen_data[1][0], Screen_data[1][1], Screen_data[2][0], Screen_data[2][1]); g2.setColor(Color.pink); g2.fill(curve1); } }
上のプログラムはある図形(ここでは半円のようなもの)をその場で回転させるプログラムです。
runメソッドの
phi += Math.PI / 10;
theta += Math.PI / 10;
で回転角を決めています
y軸方向の回転はうまくいったのですが、x軸方向の回転がうまくいかず、半円の直線の部分を軸にして回転しています。
x軸方向の回転その場で行うようにするにはどこを変更すればいいのでしょうか?
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/30 02:04