Drawing a single circle


#1

QUESTION: In this method, why did we have to divide both center.x & center.y by 2.0? I understand that we’re trying to find the center of the point, but the division is confusing me.

  • (void)drawRect:(CGRect)rect
    {
    CGRect bounds = self.bounds;

    // Figure out the center of the bounds rectangle
    CGPoint center;

    center.x = bounds.origin.x + bounds.size.width / 2.0;
    center.y = bounds.origin.y + bounds.size.height / 2.0;
    }


#2

[quote]QUESTION: In this method, why did we have to divide both center.x & center.y by 2.0? I understand that we’re trying to find the center of the point, but the division is confusing me.
[/quote]
The division by 2.0 is necessary to put the point at the center of the rectangle.

To understand this, consider the three cases below (in the rectangular coordinate system):

The point coincides with the the origin:

center.x = bounds.origin.x;
center.y = bounds.origin.y;

The point is at the corner diagonally opposite the origin:

center.x = bounds.origin.x + bounds.size.width;
center.y = bounds.origin.y + bounds.size.height;

The point is now at the center of the rectangle:

center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;

If this did not help, try answering the following question.

Imagine an ant at the the corner of a desk (with width W and height H.)
To move to the center of the desk, how much does the ant need to move horizontally across and then vertically up (or vertically up and then horizontally across)?