// 直方体を xy-平面上に描くクラス // 平行移動メソッド graphics.Parallel を適用 // x,y,z 軸回りに回転させる (graphics.Rotate を適用) // パラメータは, 平行移動距離 d_x, d_y, d_z, 軸 char axis1, double theta1, // char axis2, 回転角度 double theta2, char axis3, double theta3, 大きさ double scale import java.applet.Applet; import java.awt.*; public class cuboid4c extends Applet { public static void paint(Graphics g, int d_x, int d_y, int d_z, char axis1, double theta1, char axis2, double theta2, char axis3, double theta3, double scale) { final double RADIAN = Math.PI/180.0; // int d_x = 100; // 平行移動距離(x 軸方向) // int d_y = 100; // 平行移動距離(y 軸方向) // int d_z = 100; // 平行移動距離(z 軸方向) // char axis1 ='z'; // char axis2 ='y'; // char axis3 ='x'; // double theta1 = 10; // double theta2 = 20; // double theta3 = 30; // double scale = 1.0; // 大きさ int a = 15; // 大きさ a×b×c の直方体 int b = 30; int c = 45; _xyz abc = new _xyz(); _xyz lmn = new _xyz(); lmn.x = d_x; lmn.y = d_y; lmn.z = d_z; // 座標軸 graphics.SetColor(g,0x000000); graphics.DrawLine(g,-200,0,200,0); graphics.DrawLine(g,0,-200,0,200); // 稜 graphics.SetColor(g,0x0000ff00); // 下底 draw_line(g,a,0,0,a,0,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); draw_line(g,0,0,c,a,0,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); draw_line(g,0,0,0,0,0,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); draw_line(g,0,0,0,a,0,0,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); //上底 draw_line(g,a,b,0,a,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); draw_line(g,0,b,c,a,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); draw_line(g,0,b,0,0,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); draw_line(g,0,b,0,a,b,0,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); //右側面 draw_line(g,a,0,c,a,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); draw_line(g,0,0,c,0,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); //左側面 draw_line(g,a,0,0,a,b,0,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); draw_line(g,0,0,0,0,b,0,lmn,axis1,theta1,axis2,theta2,axis3,theta3,scale); } static void draw_line(Graphics g, int x1, int y1, int z1, int x2, int y2, int z2, _xyz lmn, char axis1, double theta1, char axis2, double theta2, char axis3, double theta3, double scale) { // 点 (x1,y1,z1) と (x2,y2,z2) を lmn だけ平行移動してから axis1 軸の回りに theta1 度, axis2 軸の回りに theta2 度, axis3 軸の回りに theta3 度 この順に回転させ,さらに scale 倍して, この2点間の線分を描く _xyz xyz = new _xyz(); xyz.x = x1; xyz.y = y1; xyz.z = z1; x1 = (int)(graphics.Rotate(graphics.Rotate(graphics.Rotate(graphics.Parallel(xyz,lmn),axis1,theta1),axis2,theta2),axis3,theta3).x * scale); y1 = (int)(graphics.Rotate(graphics.Rotate(graphics.Rotate(graphics.Parallel(xyz,lmn),axis1,theta1),axis2,theta2),axis3,theta3).y * scale); xyz.x = x2; xyz.y = y2; xyz.z = z2; x2 = (int)(graphics.Rotate(graphics.Rotate(graphics.Rotate(graphics.Parallel(xyz,lmn),axis1,theta1),axis2,theta2),axis3,theta3).x * scale); y2 = (int)(graphics.Rotate(graphics.Rotate(graphics.Rotate(graphics.Parallel(xyz,lmn),axis1,theta1),axis2,theta2),axis3,theta3).y * scale); graphics.DrawLine(g,x1,y1,x2,y2); } }