質問の「一次関数」の応用問題をJavaで解いてみました。
ちなみに、3点の共線条件は、次の通りです。
※参考URL
●パラメトロン計算機: 同一直線上の3点
http://parametron.blogspot.jp/2008/04/3.html
● ProbOfLinearFunc1.java
/* * ProbOfLinearFunc1.java */ class ProbOfLinearFunc1 { // 共線条件 public static boolean collinearityEqu(Point p, Point q, Point r) { int d = (q.x-p.x)*(r.y-p.y) - (q.y-p.y)*(r.x-p.x); return (d==0) ? true : false; } public static void main(String[] args) { final int N = 12; Point[] p = new Point[N]; Point[] q = new Point[N]; Point r = new Point (1,5); for(int k=0, i=1; i<=5; i++){ for(int j=1+i%2; j<=5 && k< N; j+=2,k++){ p[k] = new Point(i,j); q[k] = p[k].getQ(); } } for(int i=0; i< N; i++){ System.out.printf("%2d : P%s → Q%s : ",i,p[i],q[i]); System.out.println(collinearityEqu(p[i], q[i], r) ? "○" : "×"); } } } class Point { int x = 0, y = 0; // コンストラクタ Point(){ } Point(int x, int y){ this.x = x; this.y = y; } // 題意の点Qを得る Point getQ(){ return (new Point(x+y, y-x)); } // 文字列表現を返却 public String toString() { return String.format("(%2d,%2d)",x,y); } }
●実行結果
0 : P( 1, 2) → Q( 3, 1) : × 1 : P( 1, 4) → Q( 5, 3) : × 2 : P( 2, 1) → Q( 3,-1) : × 3 : P( 2, 3) → Q( 5, 1) : × 4 : P( 2, 5) → Q( 7, 3) : × 5 : P( 3, 2) → Q( 5,-1) : ○ 6 : P( 3, 4) → Q( 7, 1) : × 7 : P( 4, 1) → Q( 5,-3) : × 8 : P( 4, 3) → Q( 7,-1) : × 9 : P( 4, 5) → Q( 9, 1) : × 10 : P( 5, 2) → Q( 7,-3) : × 11 : P( 5, 4) → Q( 9,-1) : ×