ここでは、空間の座標を扱うクラスを作ります。
ソースです。
package myMath;
public class MyPoint3D {
public double x, y, z;
// コンストラクタ
public MyPoint3D() {
x = y = z = 0;
}
public MyPoint3D(double x, double y, double z) {
set(x, y, z);
}
// 座標を代入する
public MyPoint3D set(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
public MyPoint3D set(MyPoint3D p) {
return set(p.x, p.y, p.z);
}
// 移動
public void move(double p, double q, double r) {
x += p;
y += q;
z += r;
}
// 3次元回転
public void rotate(double angleX, double angleY, double angleZ) {
MyPoint2D p = new MyPoint2D();
p.set(y, z);
p.rotate(angleX);
y = p.x;
z = p.y;
p.set(z, x);
p.rotate(angleY);
z = p.x;
x = p.y;
p.set(x, y);
p.rotate(angleZ);
x = p.x;
y = p.y;
}
}
点クラスを作ります。名前は、MyPoint3D とします。
public class MyPoint3D {
}
メンバーは座標と座標変換です。座標 x、y、z は実数(double)とします。
public double x, y, z;
コンストラクタです。 メンバーの初期化です。
// コンストラクタ
public MyPoint3D() {
x = y = z = 0;
}
x、y、z へ値を代入するためのメソッドです。 一応、2種類作ります。 ところで、戻り値は、MyPoint3D(自分自身)です。
// 座標を代入する
public MyPoint3D set(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
public MyPoint3D set(MyPoint3D p) {
return set(p.x, p.y, p.z);
}
そして、点の移動のためのメソッドを用意しました。