Java 将较小的网格投影到较大的网格上

Java 将较小的网格投影到较大的网格上,java,math,image-manipulation,Java,Math,Image Manipulation,我一直在写一个有趣的图像编辑应用程序,一切都很好,但我遇到了缩放功能的问题。图像编辑器平面大512 x 512像素,但我要编辑的图像只有16 x 16。我想知道如何将鼠标坐标投影到较小的图像上,以便逐像素编辑 我设计了这个算法来解决这个问题 /** * * @param pointx The x position of the point thats being bound * @param pointy The y position of the point thats being b

我一直在写一个有趣的图像编辑应用程序,一切都很好,但我遇到了缩放功能的问题。图像编辑器平面大512 x 512像素,但我要编辑的图像只有16 x 16。我想知道如何将鼠标坐标投影到较小的图像上,以便逐像素编辑

我设计了这个算法来解决这个问题

/**
 * 
 * @param pointx The x position of the point thats being bound
 * @param pointy The y position of the point thats being bound
 * @param oldsizeX The old grid size x of which the point is currently in. ( eg  ==> 512*512)
 * @param oldsizeY The old grid size y of which the point is currently in. ( eg 512* ==> 512)
 * @param newsizeX The new grid size x for the new grid size of the point. ( eg  ==> 16*16)
 * @param newsizeY The new grid size y for the new grid size of the point. ( eg 16* ==> 16)
 * @param normalOffsetX The offset x, if any, the grid has in the normal plane ( eg ==> 32*32 @ (512*512))
 * @param normalOffsetY The offset y, if any, the grid has in the normal plane ( eg 32* ==> 32 @ (512*512)
 * @return A Vector2 containing the bound points in the new plane.
 */
public static Vector2 bindPoint(int pointx, int pointy, int oldsizeX, int oldsizeY, int newsizeX, int newsizeY,int normalOffsetX,int normalOffsetY) {
    Vector2 vec = new Vector2();
    int tileSizeX = oldsizeX / newsizeX;
    int tileSizeY = oldsizeY / newsizeY;

    int offsetX = normalOffsetX, offsetY = normalOffsetY;

    vec.x = (int) (pointx / 2) / (oldsizeX / tileSizeX) - (offsetX / tileSizeX);
    vec.y = (int) (pointy / 2) / (oldsizeY / tileSizeY) - (offsetY / tileSizeY);

    if(pointx >= normalOffsetX && pointx <= normalOffsetX + oldsizeX && pointy >= normalOffsetY && pointy <= normalOffsetY + oldsizeY) {
        return vec;
    }else {
        return new Vector2(-1,-1);
    }
}
只要较小的分辨率是16x16,这就行了,我发现如果我将pointX和pointY除法后的2改为0.5,32x32的图像就行了。我想知道的是,是否有更好的方法,以便我可以在任何缩放级别使用任何大小的图像?

您不应该使用整数来表示位置。在进行计算时,请使用double。最后,当您计算完所有内容并需要一个像素值时,将double四舍五入为整数。否则,你将失去解释你所看到的问题的精确性

根据使用方括号的方式,会得到不同的结果。例如,从数学角度来看,以下系统输出应给出相同的结果,但它们不会:

    int i = 700;
    int j = 70;
    int k = 30;
    System.out.println((i / 2) / (j / k)); --> 175
    System.out.println(i / 2 / j * k); --> 150

我自己想出来的,哈哈,对不起,时间太晚了,我发呆了,忘了如何计算比例

这是其他任何需要它的人的答案

/**
 * 
 * @param pointx The x position of the point thats being bound
 * @param pointy The y position of the point thats being bound
 * @param oldsizeX The old grid size x of which the point is currently in. ( eg  ==> 512*512)
 * @param oldsizeY The old grid size y of which the point is currently in. ( eg 512* ==> 512)
 * @param newsizeX The new grid size x for the new grid size of the point. ( eg  ==> 16*16)
 * @param newsizeY The new grid size y for the new grid size of the point. ( eg 16* ==> 16)
 * @param normalOffsetX The offset x, if any, the grid has in the normal plane ( eg ==> 32*32 @ (512*512))
 * @param normalOffsetY The offset y, if any, the grid has in the normal plane ( eg 32* ==> 32 @ (512*512)
 * @return A Vector2 containing the bound points in the new plane.
 */
public static Vector2 bindPoint(int pointx, int pointy, int oldsizeX, int oldsizeY, int newsizeX, int newsizeY,int normalOffsetX,int normalOffsetY) {
    Vector2 vec = new Vector2();
    int tileSizeX = oldsizeX / newsizeX;
    int tileSizeY = oldsizeY / newsizeY;
    int offsetX = normalOffsetX, offsetY = normalOffsetY;

    vec.x = (int) Math.floor(pointx * ((float) newsizeX) / (float) oldsizeX) - (offsetX / tileSizeX);
    vec.y = (int) Math.floor(pointy * ((float) newsizeY) / (float) oldsizeY) - (offsetY / tileSizeY);
    return vec;
}

很晚了,我累了,我用笔和纸把它弄明白了。