Java には、Pointクラスがあります。 ここでは、実数を座標とするクラスを自作します。
ソースです。
package myMath; public class MyPoint2D { public double x, y; // コンストラクタ public MyPoint2D() { x = y = 0; } public MyPoint2D(double x, double y) { set(x, y); } // 座標を代入する public MyPoint2D set(double x, double y) { this.x = x; this.y = y; return this; } public MyPoint2D set(MyPoint2D p) { return set(p.x, p.y); } // 拡大・縮小 public MyPoint2D scale(double k) { x *= k; y *= k; return this; } // 平行移動 public MyPoint2D move(double p, double q) { x += p; y += q; return this; } // 原点の回りの回転 public MyPoint2D rotate(double angle) { double x1, y1; x1 = x * Math.cos(Math.toRadians(angle)) - y * Math.sin(Math.toRadians(angle)); y1 = x * Math.sin(Math.toRadians(angle)) + y * Math.cos(Math.toRadians(angle)); return set(x1, y1); } }
点クラスを作ります。名前は、MyPoint2D とします。
public class MyPoint2D { }
メンバーは座標と座標変換です。座標 x、y は実数(double)とします。
public double x, y;
コンストラクタです。 メンバーの初期化です。
// コンストラクタ public MyPoint2D() { x = y = 0; }
x、y へ値を代入するためのメソッドです。 一応、2種類作ります。 ところで、戻り値は、MyPoint2D(自分自身)です。
public MyPoint2D p(double x, double y) { this.x = x; this.y = y; return this; } public MyPoint2D p(MyPoint2D p) { return set(p.x, p.y); }
そして、点の移動のためのメソッドを3種類用意しました。