Java 坐标旋转

Java 坐标旋转,java,coordinate,imagej,Java,Coordinate,Imagej,我需要旋转图像或矩阵像素的坐标。 我的目标是计算每个点在旋转后的位置,反之亦然。 我目前正在使用此过程: private void setRot(int i) { this.rot = -i / (180.0 / Math.PI); this.coseno = Math.cos(rot); this.seno = Math.sin(rot); } private Point ruota(int x, int y){ int centerY=h/2; i

我需要旋转图像或矩阵像素的坐标。 我的目标是计算每个点在旋转后的位置,反之亦然。 我目前正在使用此过程:

private void setRot(int i) {
    this.rot = -i / (180.0 / Math.PI);
    this.coseno = Math.cos(rot);
    this.seno = Math.sin(rot);
}

private Point ruota(int x, int y){

    int centerY=h/2;
    int centerX=w/2;


    int j = (int) (coseno * (x-centerX) - seno * (y-centerY) + centerX);
    int i = (int) (seno * (x-centerX) + coseno * (y-centerY) + centerY);

    return new Point(j,i);
}
但是,如果使用每个像素的方法应用测试,结果不会围绕中心旋转

    private void esegui() {

        for(int i=0;i<w;i++)
            for(int j=0;j<h;j++){
                Point p = ruota(i,j); //ip : origina image | ip2 destination image
                ip2.putPixel(p.x, p.y, ip.getPixel(i, j));
            }
        nu.setProcessor(ip); // destination image
        nu.repaintWindow();
    }

我建议改为使用仿射变换,这是Java2D变换坐标的默认方法。你说结果不是围绕中心旋转的,你能告诉我们它围绕某个角、某个随机点等旋转了什么吗@CrazyCasta旋转不是在精确的点上发生的。旋转中心似乎更靠近左上角,因此图像似乎不完全位于目标位置。我认为问题是因为两个图像的中心不同,目标图像更大。@Pierock我认为我们需要更多关于setRoa和ruota方法所在的类以及创建它的位置的信息。特别是我认为我们需要知道w/h来自哪里。@CrazyCasta我通过插入类构造函数编辑了这篇文章。h和w分别是原始图像的高度和深度。
private int getH(){ //height of destination image
    return (int) (this.h*Math.abs(coseno) + this.w*Math.abs(seno));
}
private int getW(){
    return (int) (this.plus.getHeight()*Math.abs(seno) + this.plus.getWidth()*Math.abs(coseno));
}

public Test(){ // class test
    IJ.open();
    this.plus = IJ.getImage();
    ip = plus.getProcessor();
    this.h = this.plus.getHeight(); // height of start image
    this.w = this.plus.getWidth();
    setRot(14);                     // rotation of 14°
    dHeight = this.getH();          // dimension of the target image
    dWidth = this.getW();
    System.out.println("h "+h+" gh  " + getH());
    System.out.println("w "+w+" gw  " + getW());

    // create an image target
    this.nu = NewImage.createRGBImage("rotate", getW(), getH(), 1, NewImage.FILL_BLACK);
    ip2 = nu.getProcessor();
    esegui();
    nu.setProcessor(ip2);
    nu.show();
}