将屏幕坐标转换为位图坐标

时间:2018-05-30 16:43:41

标签: c bitmap

我的屏幕重量为320 * 240。和图像例如180 * 180。

我想将位置120,120转换为位图坐标,以便将其正确放置在320 * 240的屏幕坐标上。

这是我的尝试

// w, h width and height of an image
// position x,y on the screen coordinates

void Rasterizer::DrawBitmap(int w, int h, int x, int y, float angle)
{ 
    uint16_t pixel;

    float relx = w/ (float)320;
    float rely = h/ (float)240;
    uint16_t xgoal = (x * relx) + w/2;
    uint16_t ygoal = (y * rely) + h/2;

    float midX, midY;
    float deltaX, deltaY;
    int rotX, rotY;
    int i, j;
    midX = w / 2.0f;
    midY = h / 2.0f;
    for (i = 0; i < w; i++)
        for (j = 0; j < h; j++) {
            deltaX = i - midX;
            deltaY = j - midY;
            rotX = (int)(midX + deltaX * sin(angle) + deltaY * cos(angle));
            rotY = (int)(midY + deltaX * cos(angle) - deltaY * sin(angle));
            if (rotX >= 0 && rotX < w && rotY >= 0 && rotY < h) {
                SetPixel(xgoal + i,ygoal +j , bitmap[(rotX*w + rotY )]);
            }
        }

}

show

1 个答案:

答案 0 :(得分:0)

如果您想在屏幕中心绘制图像,则需要翻译所有像素。假设屏幕的左上角是(0,0)

    uint16_t relx = 320/2 - w/2 ;
    uint16_t rely = 240/2 - h/2;
    uint16_t xgoal = relx + x;
    uint16_t ygoal = rely + y;