2012-01-25 9 views
1

私は、維持し、コンピュータグラフィックスプログラムの変換行列を使用しようとしています。これは、(x、y)点のスケール、回転、および情報の変換を格納する3x3の行列です。Java変換行列操作

私のプログラムでは、個々のケースを処理できます。つまり、スケールのみ、または回転のみの場合は問題ありません。しかし、スケールと回転を組み合わせるときにはうまくいかないようですが、コード内で回転とスケールをどのように組み合わせるかが関係していると思います。この行列は変換と呼ばれ、float型です。次のメソッドは、マトリックスをクリア、回転、拡大縮小、平行移動します。スケールについて

public void clearTransform() 
{ 
    transformation[0][0] = 1;transformation[0][1] = 0;transformation[0][2] = 0; 
    transformation[1][0] = 0;transformation[1][1] = 1;transformation[1][2] = 0; 
    transformation[2][0] = 0;transformation[2][1] = 0;transformation[2][2] = 1; 
} 

public void rotate (float degrees) 
{ 
    double r = degrees * (Math.PI/180); 
    float sin = (float)Math.sin(r); 
    float cos = (float)Math.cos(r); 
    transformation[0][0] *= cos; 
    transformation[1][1] *= cos; 
    if(transformation[0][1] == 0) 
     transformation[0][1] = -sin; 
    else 
     transformation[0][1] *= -sin; 
    if(transformation[1][0] == 0) 
     transformation[1][0] = sin; 
    else 
     transformation[1][0] *= sin; 
} 

public void scale (float x, float y) 
{ 
    transformation[0][0] *= x; 
    transformation[1][1] *= y; 
} 

public void translate (float x, float y) 
{ 
    transformation[0][2] += x; 
    transformation[1][2] += y; 
} 

、このようなマトリックスホールド情報:

(SX、0、0) (0、Syを、0) (0、0、1)回転のため

このような:

(COS(シータ)、-sin(シータ)、0) (罪(シータ)、COS(シータ)、0) (0、0、1)

Fまたは翻訳、この:

(1、0、テキサス州) (0、1、Tyの) (0、0、1)

私は正しくスケールを組み合わせると回転させてるとは思いません。

xとyは、事前に変換された点の配列です
public float[] transformX(float[] x, float[] y, int n) { 
    float[] newX = new float[n]; 
    for(int i = 0; i < n; i ++) { 
     newX[i] = (x[i] * transformation[0][0]) + (y[i] * transformation[0][1]) + transformation[0][2]; 
    } 
    return newX; 
} 

public float[] transformY(float[] x, float[] y, int n) { 
    float[] newY = new float[n]; 
    for(int i = 0; i < n; i ++) { 
     newY[i] = (x[i] * transformation[1][0]) + (y[i] * transformation[1][1]) + transformation[1][2]; 
    } 
    return newY; 
} 

、およびn点

の数は、任意の助けをありがとうれる:ここで私は実際に変換を適用する場所です!

+4

それはあなたが2つのだけのディメンションを変換しているようですので、あなたも2Dで 'AffineTransform'クラスを使用することができます。http://docs.oracle.com /javase/tutorial/2d/advanced/transforming.html –

+3

ニースですが、 'rotate'は古い値との行列乗算でなければなりません。 0のifはそこにいる権利を持っていません。通常、新しい値を書き込むには一時的な行列が必要です。 Javaから既存のコードを借りる可能性があります。 –

+0

私はもともとこれらの2つを(* = -sin)と(* = sin)として持っていましたが、0の場合(常に最初のローテーションを適用した場合)、値は0のままです。 – user1028885

答えて

2

私は正しい変換がされるべきだと思う:

enter image description here