Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn two CGPoints into a CGRect

Tags:

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?

like image 841
christo16 Avatar asked Nov 11 '11 22:11

christo16


1 Answers

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)); 

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.

like image 126
Ben Zotto Avatar answered Sep 19 '22 13:09

Ben Zotto