// 直方体を xy-平面上に描く // 平行移動メソッド graphics.Parallel を適用 // x,y,z 軸回りに回転させる (graphics.Rotate を適用) import java.applet.Applet; import java.awt.*; public class cuboid4 extends Applet { public void paint(Graphics g) { final double RADIAN = Math.PI/180.0; double scale = 5.0; // 大きさ int a = 15; // 大きさ a×b×c の直方体 int b = 30; int c = 45; int A = 100; // 平行移動距離(x 軸方向) int B = 100; // 平行移動距離(y 軸方向) int C = 100; // 平行移動距離(z 軸方向) _xyz abc = new _xyz(); _xyz lmn = new _xyz(); lmn.x = A; lmn.y = B; lmn.z = C; double theta1 = 10; double theta2 = 20; double theta3 = 30; char axis1 ='z'; char axis2 ='y'; char axis3 ='x'; // 座標軸 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); draw_line(g,0,0,c,a,0,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3); draw_line(g,0,0,0,0,0,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3); draw_line(g,0,0,0,a,0,0,lmn,axis1,theta1,axis2,theta2,axis3,theta3); //上底 draw_line(g,a,b,0,a,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3); draw_line(g,0,b,c,a,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3); draw_line(g,0,b,0,0,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3); draw_line(g,0,b,0,a,b,0,lmn,axis1,theta1,axis2,theta2,axis3,theta3); //右側面 draw_line(g,a,0,c,a,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3); draw_line(g,0,0,c,0,b,c,lmn,axis1,theta1,axis2,theta2,axis3,theta3); //左側面 draw_line(g,a,0,0,a,b,0,lmn,axis1,theta1,axis2,theta2,axis3,theta3); draw_line(g,0,0,0,0,b,0,lmn,axis1,theta1,axis2,theta2,axis3,theta3); } 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) { // 点 (x1,y1,z1) と (x2,y2,z2) を lmn だけ平行移動してから axis1 軸の回りに theta1 度, axis2 軸の回りに theta2 度, // axis3 軸の回りに theta3 度 この順に回転させ, この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; y1 = (int)graphics.Rotate(graphics.Rotate(graphics.Rotate(graphics.Parallel(xyz,lmn),axis1,theta1),axis2,theta2),axis3,theta3).y; 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; y2 = (int)graphics.Rotate(graphics.Rotate(graphics.Rotate(graphics.Parallel(xyz,lmn),axis1,theta1),axis2,theta2),axis3,theta3).y; graphics.DrawLine(g,x1,y1,x2,y2); } }