// D3Poly.java // 3次元 多角形 操作・描画 クラス // Copyright (C) 1996, 1997 Y.Nagatani // nagatani@eken.phys.nagoya-u.ac.jp import java.awt.Graphics; import java.awt.Color; import D3Vector; import D3VecToD2; public class D3Poly { public int points; public D3Vector vect[]; public Color color; D3Poly (int points) { this.points = points; vect = new D3Vector[points]; for (int n = 0 ; n < points ; n++) { double arg = 2.0 * Math.PI * n / points; vect[n] = new D3Vector (); set (n, 0.2 * Math.cos(arg), -0.2 * Math.sin(arg), 0.0); } color = Color.black; } public D3Poly set (int point, double x, double y, double z) { if (0 <= point && point < points) { vect[point].set (x,y,z); } else { System.out.println ("D3Poly: Point Domain Error\n"); } return this; } public D3Poly setColor (Color color) { this.color = color; return this; } public D3Poly translate (D3Vector vectTrans) { for (int n = 0 ; n < points ; n++) vect[n].translate (vectTrans); return this; } public D3Poly rotXY (double arg) { for (int n = 0 ; n < points ; n++) vect[n].rotXY (arg); return this; } public D3Poly rotYZ (double arg) { for (int n = 0 ; n < points ; n++) vect[n].rotYZ (arg); return this; } public D3Poly rotZX (double arg) { for (int n = 0 ; n < points ; n++) vect[n].rotZX (arg); return this; } public D3Vector center () { D3Vector cent = new D3Vector (0,0,0); for (int n = 0 ; n < points ; n++) cent.add (vect[n]); return cent.scalar (1.0/points); } public D3Poly rotXY (double arg, D3Vector cent) { translate (cent.scalar (-1)); rotXY (arg); translate (cent.scalar (-1)); return this; } public D3Poly rotYZ (double arg, D3Vector cent) { translate (cent.scalar (-1)); rotYZ (arg); translate (cent.scalar (-1)); return this; } public D3Poly rotZX (double arg, D3Vector cent) { translate (cent.scalar (-1)); rotZX (arg); translate (cent.scalar (-1)); return this; } public D3Poly rotXYcent (double arg) { rotXY (arg, center ()); return this; } public D3Poly rotYZcent (double arg) { rotYZ (arg, center ()); return this; } public D3Poly rotZXcent (double arg) { rotZX (arg, center ()); return this; } public void paint (Graphics g, D3VecToD2 D3D2) { int XY[] = new int [2]; int X[] = new int[points + 1], Y[] = new int[points + 1]; for (int n = 0 ; n < points ; n++) { XY = D3D2.transf (vect[n]); X[n] = XY [0]; Y[n] = XY [1]; } g.setColor (color); g.fillPolygon (X, Y, points); X[points] = X[0]; Y[points] = Y[0]; g.setColor (Color.black); g.drawPolygon (X, Y, points + 1); } }