How can I, given two different CGPoints
, turn them into an CGRect
?
Example:
CGPoint p1 = CGPointMake(0,10);
CGPoint p2 = CGPointMake(10,0);
How can I turn this into a CGRect
?
How can I, given two different CGPoints
, turn them into an CGRect
?
Example:
CGPoint p1 = CGPointMake(0,10);
CGPoint p2 = CGPointMake(10,0);
How can I turn this into a CGRect
?
A slight modification of Ken's answer. Let CGGeometry "standardize" the rect for you.
CGRect rect = CGRectStandardize(CGRectMake(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y));
Assuming p1 is the origin and the other point is the opposite corner of a rectangle, you could do this:
This function takes any number of CGPoints and gives you the smallest CGRect back.
This will return a rect of width or height 0 if the two points are on a line
This will take two arbitrary points and give you the CGRect that has them as opposite corners.
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.