Java变换矩阵运算

发布于 2024-12-28 18:19:27 字数 1995 浏览 1 评论 0原文

我正在尝试维护然后使用计算机图形程序的变换矩阵。它是一个 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(theta), -sin(theta), 0) (正弦(θ),余弦(θ),0) (0, 0, 1)

用于翻译,这个:

(1, 0, Tx) (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 是点数

感谢您的帮助!

I'm trying to maintain and then use a transformation matrix for a computer graphics program. It's a 3x3 matrix that stores scale, rotate, and translate information for (x,y) points.

My program can handle any individual case... i.e. if I only scale or only rotate it works fine. However, it does not seem to work when combining scale and rotate, and I think that has to do with how I combine rotation and scale in the code. The matrix is called transformation and is of type float. The following methods clear, rotate, scale, and translate (in that order) the matrix.

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

For scale, the matrix hold info like this:

(Sx, 0, 0)
(0, Sy, 0)
(0, 0, 1)

for rotation, like this:

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

for translation, this:

(1, 0, Tx)
(0, 1, Ty)
(0, 0, 1)

I don't think I'm correctly combining scale and rotate. Here is where I actually apply the transformation:

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

Where x and y are the pre-transformed point arrays, and n is the number of points

Thank you for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○闲身 2025-01-04 18:19:27

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

在此处输入图像描述

I think the correct transformation should be:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文