main.m
AmericaAppDelegate
View
.
The
f
in
sinf
stands for
float
,
a.k.a.
CGFloat
.
patton.jpg
(100 × 100):
Apple would prefer that you use png.
I got the little
.jpg
file from the
Internet Movie Database,
stored it on my Macintosh Desktop,
and named it
patton.jpg
.
(Be careful of
.jpg
vs.
.jpeg
.)
Drag the image file from the desktop to the Supporting Files folder
in the Xcode Project Navigator.
Xcode will say
Choose options for adding these files
Destination ☑ Copy items into destination group’s folder
(if needed)
Add to targets ☑ America
Finish
To verify that
patton.jpg
was added to your project,
open the Supporting Files folder.
You should see
patton.jpg
listed in black.
It’s simplest to let the background be white, and draw the red and blue (in that order) on top of it. We start the star with the vertex (point) pointing towards the right. In trigonometry, adding a positive number to an angle makes the angle go counterclockwise. But when we add a positive number to θ we go clockwise around the star, because our Y axis points down. If the Y axis pointed up, we would have gone counterclockwise.
for
loop.
In the
drawRect:
method of class
View
,
change
CGContextAddRect(c, CGRectMake( 0 * w / 13, 0, w / 13, h)); CGContextAddRect(c, CGRectMake( 2 * w / 13, 0, w / 13, h)); CGContextAddRect(c, CGRectMake( 4 * w / 13, 0, w / 13, h)); CGContextAddRect(c, CGRectMake( 6 * w / 13, 0, w / 13, h)); CGContextAddRect(c, CGRectMake( 8 * w / 13, 0, w / 13, h)); CGContextAddRect(c, CGRectMake(10 * w / 13, 0, w / 13, h)); CGContextAddRect(c, CGRectMake(12 * w / 13, 0, w / 13, h));to
for (int i = 0; i <= 12; i = i + 2) { CGContextAddRect(c, CGRectMake(i * w / 13, 0, w / 13, h)); }You can also telescope the
i = i + 2
i += 2
.CGContextAddRect
into two statements?
for (int i = 0; i <= 12; i += 2) { CGRect r = CGRectMake(i * w / 13, 0, w / 13, h); CGContextAddRect(c, r); }
for
loop.
In the
drawRect:
method of class
View
,
change
CGFloat theta = 0; //Start with vertext pointing to right. CGContextMoveToPoint(c, center.x + radius * cosf(theta), center.y + radius * sinf(theta) ); //means theta = theta + 2 * M_PI * 2 / 5; theta += 2 * M_PI * 2 / 5; //vertex pointing to lower left CGContextAddLineToPoint(c, center.x + radius * cosf(theta), center.y + radius * sinf(theta) ); theta += 2 * M_PI * 2 / 5; //vertex pointing to upper right CGContextAddLineToPoint(c, center.x + radius * cosf(theta), center.y + radius * sinf(theta) ); theta += 2 * M_PI * 2 / 5; //vertex pointing to lower right CGContextAddLineToPoint(c, center.x + radius * cosf(theta), center.y + radius * sinf(theta) ); theta += 2 * M_PI * 2 / 5; //vertex pointing to upper left CGContextAddLineToPoint(c, center.x + radius * cosf(theta), center.y + radius * sinf(theta) );to the following. You could telescope the
i = i + 2
i += 2
.for (int i = 0; i <= 10; i = i + 2) { CGFloat theta = 2 * M_PI * i / 5; CGFloat x = center.x + radius * cosf(theta); CGFloat y = center.y + radius * sinf(theta); if (i == 0) { CGContextMoveToPoint(c, x, y); } else { CGContextAddLineToPoint(c, x, y); } }
Sine
.)