gpt4 book ai didi

ios - 如何将 CGRect 的大小增加一定的百分比值?

转载 作者:IT王子 更新时间:2023-10-29 05:16:25 25 4
gpt4 key购买 nike

如何将 CGRect 的大小增加一定的百分比值?我应该使用某种形式的 CGRectInset 来做到这一点吗?

例子:

假设我有一个 CGRect:{10, 10, 110, 110}

我想将其大小(保留相同的中心点)增加 20% 以:

{0, 0, 120, 120}

最佳答案

如果你愿意,你可以使用CGRectInset:

double pct = 0.2;
CGRect newRect = CGRectInset(oldRect, -CGRectGetWidth(oldRect)*pct/2, -CGRectGetHeight(oldRect)*pct/2);

要减小大小,请删除 -

旁注:CGRect{10, 10, 100, 100} 大 20% 是 {0 , 0, 120, 120}.


编辑:如果打算按面积增加,那么这样做就可以了(即使对于非正方形的矩形也是如此):

CGFloat width = CGRectGetWidth(oldRect);
CGFloat height = CGRectGetHeight(oldRect);
double pct = 1.2; // 20% increase
double newWidth = sqrt(width * width * pct);
double newHeight = sqrt(height * height * pct);
CGRect newRect = CGRectInset(oldRect, (width-newWidth)/2, (height-newHeight)/2);

关于ios - 如何将 CGRect 的大小增加一定的百分比值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26918886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com