gpt4 book ai didi

Java变换矩阵运算

转载 作者:行者123 更新时间:2023-11-29 06:04:26 25 4
gpt4 key购买 nike

我正在尝试为计算机图形程序维护并使用变换矩阵。它是一个 3x3 矩阵,用于存储 (x,y) 点的缩放、旋转和平移信息。

我的程序可以处理任何个别情况...即如果我只缩放或只旋转它工作正常。但是,它在组合缩放和旋转时似乎不起作用,我认为这与我在代码中如何组合旋转和缩放有关。该矩阵称为变换,类型为 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, 西, 0)(0, 0, 1)

对于旋转,像这样:

(cos(θ), -sin(θ), 0)(sin(θ), cos(θ), 0)(0, 0, 1)

对于翻译,这个:

(1, 0, 发送)(0, 1, 泰)(0, 0, 1)

我认为我没有正确组合缩放和旋转。这是我实际应用转换的地方:

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;
}

其中x和y是预变换后的点数组,n是点的个数

感谢您的帮助!

最佳答案

我认为正确的转换应该是:

enter image description here

关于Java变换矩阵运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9010546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com