Turn two CGPoints into a CGRect

Swift extension:

extension CGRect {
    init(p1: CGPoint, p2: CGPoint) {
        self.init(x: min(p1.x, p2.x),
                  y: min(p1.y, p2.y),
                  width: abs(p1.x - p2.x),
                  height: abs(p1.y - p2.y))
    }
}

Assuming p1 is the origin and the other point is the opposite corner of a rectangle, you could do this:

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

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


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.