如何找到适合预定尺寸的宽高比4:3的尺寸?

Ext*_*kun 2 math image qt4 aspect-ratio

这里的问题是我有一个x和y大小的显示窗口,我需要在窗口内显示一个没有任何滚动的图像,并保持4:3的宽高比.我有以下代码片段:

// Lock the current height, calculate new width of the canvas and scale the viewport.
// get width of the movie canvas
qreal width = canvas_->width();

// Find the height of the video
qreal height = (width/4.0) * 3;

// find original width and height for video to calculate scaling factor
qreal videoWidth = movieData_->GetWidth();
qreal videoHeight = movieData_->GetHeight();

// calculate scaling factor
qreal scaleFactorWidth = width/videoWidth;
qreal scaleFactorHeight = height/videoHeight;
Run Code Online (Sandbox Code Playgroud)

当然,通过使用高度或宽度作为"锚点",新图像将以这种方式或其他方式引起滚动(假设原始图像首先大于窗口).如何找到适合预定尺寸的宽高比4:3的尺寸?

编辑 我需要传递x和y的比例因子来进行缩放

canvas_->scale(scaleFactorWidth, scaleFactorHeight);
Run Code Online (Sandbox Code Playgroud)

Mar*_*rth 5

只需取两个计算值中的最小值:

scale = min(scaleFactorWidth, scaleFactorHeight)
Run Code Online (Sandbox Code Playgroud)

或(如果你想要外套)

scale = max(scaleFactorWidth, scaleFactorHeight)
Run Code Online (Sandbox Code Playgroud)