2017-12-01 8 views
-3

재미있는 이미지 편집 응용 프로그램을 작성했으며 모두 잘되고 있지만 확대/축소 기능에 문제가 있습니다. 이미지 편집기 평면은 512 x 512 픽셀이지만 편집하려는 이미지는 16 x 16뿐입니다. 마우스 좌표를 작은 이미지로 투영하여 픽셀 단위로 편집하는 방법을 알고 싶습니다.더 큰 하나에 작은 격자를 투영하십시오.

나는이 알고리즘을 고안했습니다. 작은 해상도가 16 × 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); 
    } 
} 

이것은 오랫동안 작동하는 전 0.5로 pointX 뾰족한 분할 및 32 × 32 일의 화상 후 2 바뀌면. 내가 알고 싶은 것은 그렇게하는 더 좋은 방법이 있다면, 어떤 줌 레벨에서 어떤 크기의 이미지라도 사용할 수 있다는 것입니다.

답변

0

위치를 나타내는 데 integers을 사용하지 않아야합니다. 대신 계산을 할 때 double을 사용하십시오. 결국 모든 것을 계산하고 픽셀 값이 필요하면 doubleinteger으로 반올림합니다. 그렇지 않으면 장소 전체에 정밀도를 잃게됩니다 (문제를 설명 함).

대괄호 사용 방법에 따라 결과가 달라집니다. 예를 들어, Systems out '아래보기의 수학 지점에서의 당신에게 동일한 결과를 제공해야하지만 그렇지 않은 :

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 
+0

나는 늦었고 피곤해서 펜과 종이를 알아 냈습니다. –

0

내가 내 자신에 그것을 알아 냈 롤, 죄송합니다, 그 말을하고 난 간격 비례하는 법을 잊어 버렸습니다.

여기 필요한 사람을위한 답변입니다.

/** 
* 
* @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; 
} 
관련 문제