1.single Pentagram drawing
The last time I talked about using quartz2d in iOS to draw basic graphics, today to draw a more complex graph-pentagram, pentagram everyone is very familiar.
First to analyze the painting of the pentagram, a pentagram.
1. The average circumference is equal to five, then each angle is 360/5, then the dots are wired to draw the pentagram .
2. the angle of each connection is 360/5*2, but the sine cosine of iOS is calculated using radians. Here it is necessary to review the knowledge of the Radian, arc is the length of arcs, equal radius of radians defined as a radian. So the degree is 2pi.
3. after reviewing the radian knowledge, we can draw the pentagram.
Drawing Code and
-(void) Drawstar: (cgcontextref) context{
[[Uicolor Redcolor] set];
determine the center point
Cgpoint Centerpoint=cgpointmake (160, 230);
Determine Radius
CGFloat radius=100.0;
Pentagram to Vertex
Cgpoint P1=cgpointmake (Centerpoint.x, Centerpoint.y-radius);
Cgcontextmovetopoint (context, p1.x, p1.y);
the point angle between each point of the pentagram is calculated by radians. You can draw a pentagram without two dots.
Point -to-point angle between points is 2*m_pi/5.0,
CGFloat angle=4*m_pi/5.0;
for (int i=1; i<=5; i++) {
CGFloat X=centerpoint.x-sinf (i*angle) *radius;
CGFloat Y=CENTERPOINT.Y-COSF (i*angle) *radius;
Cgcontextaddlinetopoint (context, x, y);
}
Cgcontextdrawpath (context, kcgpathfillstroke);
}
2.multiple pentagram randomly plotted
1.API Introduction
Cgcontextsavegstate (context); Save Context
Cgcontextrestoregstate (context); Reply Context
CGCONTEXTTRANSLATECTM (context, x, y); Canvas Move
CGCONTEXTROTATECTM (context, angle); Canvas Rotation
CGCONTEXTSCALECTM (context, scale, scale); Canvas Zoom
2. Operation effect
-(void) Drawmultistar: (cgcontextref) Context Starcount: (int) count{
determine the center point
Cgpoint centerpoint=cgpointmake (0, 0);
Determine Radius
CGFloat radius=100.0;
Pentagram Five-point array
Cgpoint Points[5];
Points[0]=cgpointmake (Centerpoint.x,centerpoint.y-radius);
the point angle between each point of the pentagram is calculated by radians. You can draw a pentagram without two dots.
Point -to-point angle between points is 2*m_pi/5.0,
CGFloat angle=4*m_pi/5.0;
for (int i=1; i<5; i++) {
CGFloat X=centerpoint.x-sinf (i*angle) *radius;
CGFloat Y=CENTERPOINT.Y-COSF (i*angle) *radius;
Points[i]=cgpointmake (x, y);
}
for (int i=0; i<count; i++) {
Save Context
Cgcontextsavegstate (context);
int Randomx=arc4random_uniform (320);
int Randomy=arc4random_uniform (480);
randomly move the canvas position
CGCONTEXTTRANSLATECTM (context, randomx,randomy);
rotate the canvas randomly
CGFloat Roate=arc4random_uniform (*m_pi/180.0);
CGCONTEXTROTATECTM (context, roate);
randomly zoom the canvas
Float Scale=arc4random_uniform (5)/10.0+0.2;
CGCONTEXTSCALECTM (context, scale, scale);
[[Self randomcolor] set];
Cgcontextmovetopoint (context, POINTS[0].X,POINTS[0].Y);
for (int i=1; i<5; i++) {
Cgcontextaddlinetopoint (context, POINTS[I].X,POINTS[I].Y);
}
Cgcontextdrawpath (context, kcgpathfillstroke);
Reply Context
Cgcontextrestoregstate (context);
}
}
quartz2d drawing of iOS randomly drawn pentagram