将两个CGPoints转换为CGRect [英] Turn two CGPoints into a CGRect

查看:121
本文介绍了将两个CGPoints转换为CGRect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有两个不同的 CGPoints ,我怎么能把它们变成 CGRect

How can I, given two different CGPoints, turn them into an CGRect?

示例:

CGPoint p1 = CGPointMake(0,10);
CGPoint p2 = CGPointMake(10,0);

如何将其转换为 CGRect

推荐答案

这将占用两个任意点,并为您提供将它们作为对角的CGRect。

This will take two arbitrary points and give you the CGRect that has them as opposite corners.

CGRect r = CGRectMake(MIN(p1.x, p2.x), 
                      MIN(p1.y, p2.y), 
                      fabs(p1.x - p2.x), 
                      fabs(p1.y - p2.y));

较小的x值与较小的y值配对将始终是rect的原点(前两个)参数)。 x值之间的差值的绝对值将是宽度,y值之间的差值的绝对值是高度。

The smaller x value paired with the smaller y value will always be the origin of the rect (first two arguments). The absolute value of the difference between x values will be the width, and between y values the height.

这篇关于将两个CGPoints转换为CGRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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