如何调整一个四边形到最大尺寸,可能一个矩形范围内? [英] How do I scale one rectangle to the maximum size possible within another rectangle?

查看:185
本文介绍了如何调整一个四边形到最大尺寸,可能一个矩形范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个源矩形和目标矩形。我需要找到它的源代码可以在目标矩形内装修进行缩放,最大规模的保持其原始宽高比的。

谷歌发现 之一的方式来做到这一点,但我不知道这是否适用于所有情况。这里是我的家中自酿的解决方案:

  • 在计算高度/宽度为每个矩形。这给对角线 MSRC mdest 的山坡上。
  • 如果 MSRC< MDST ,衡源的宽度的符合目标的宽度的(和相同的比例尺度的高度)
  • 否则,衡源的的符合目标的的(和相同的比例尺度宽)

寻找其他可能的解决这个问题。我甚至不知道我的算法适用于所有的情况下!

解决方案

 规模=分钟(dst.width / src.width,dst.height / src.height)
 

这是你的方法,但写得更干净。

I have a source rectangle and a destination rectangle. I need to find the maximum scale to which the source can be scaled while fitting within the destination rectangle and maintaining its original aspect ratio.

Google found one way to do it but I'm not sure if it works in all cases. Here is my home-brewed solution:

  • Calculate Height/Width for each rectangle. This gives the slopes of the diagonals msrc and mdest.
  • If msrc < mdst, scale source width to fit the destination width (and scale height by the same ratio)
  • Otherwise, scale source height to fit the destination height (and scale width by the same ratio)

Looking for other possible solutions to this problem. I'm not even sure if my algorithm works in all cases!

解决方案

scale = min( dst.width/src.width, dst.height/src.height)

This is your approach but written more cleanly.

这篇关于如何调整一个四边形到最大尺寸,可能一个矩形范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆