屏幕坐标到归一化坐标的转换

1.由屏幕坐标转换到归一化坐标

Point2 dc_to_ndc(const Point2& pt_dc, int width, int height) {
    double x = (pt_dc.x + 0.5) / (double)width;
    double y = (pt_dc.y + 0.5) / (double)height;
    return Point2(x * 2.0 - 1.0, -(y * 2.0 - 1.0));
}

2.由归一化坐标转换到屏幕坐标

Point2 ndc_to_dc(Point2 pt_ndc, int width, int height) {
    double x = (pt_ndc.x + 1.0) * 0.5;
    double y = (-pt_ndc.y + 1.0) * 0.5;
    x = x * (double)width - 0.5;
    y = y * (double)height - 0.5;

    return Point2((int)x, (int)y);
}

qDebug()<<"dc_to_ndc is:"<<dc_to_ndc(QPointF(49,49),100,100);
qDebug()<<"dc_to_ndc is:"<<ndc_to_dc(QPointF(0,0),100,100);

dc_to_ndc is: QPointF(-0.01,0.01)

dc_to_ndc is: QPointF(49,49)

猜你喜欢

转载自blog.csdn.net/fanhenghui/article/details/85603835