Turn two CGPoints into a CGRect?

This will take two arbitrary points and give you the CGRect that bounds them both 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.

This will take two arbitrary points and give you the CGRect that bounds them both. 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.

Thanks, very clean solution! – christo16 Nov 11 at 23:13.

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

If you already know that p1 is the origin (it's not in the OP's example), you don't need the fabs. But then you're kind of making the caller do the hard work in advance. – quixoto Nov 11 at 22:56.

This will return a rect of width or height 0 if the two points are on a line float x,y,h,w; if (p1. X > p2. X) { x = p2.

X; w = p1. X-p2. X; } else { x = p1.

X; w = p2. X-p1. X; } if (p1.

Y > p2. Y) { y = p2. Y; h = p1.

Y-p2. Y; } else { y = p1. Y; h = p2.

Y-p1. Y; } CGRect newRect = CGRectMake(x,y,w,h).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions