Using Quartz we can draw a line even ellipse easily but drawing a circle is really hard.

I tried to draw a circle using quartz and succeed.

Here are the code:

#define Xmin 30.0
#define Xmax 290.0
#define Ymin 100.0
#define Ymax 360.0
#define redious 130.0

struct coord {
CGFloat x,y;
};

– (void)drawRect:(CGRect)rect {
// Drawing code

CGFloat baseX = 160.0f;
CGFloat baseY = 230.0f;

self.backgroundColor = [UIColor whiteColor];

CGContextRef context = UIGraphicsGetCurrentContext();

self.colr = [UIColor blueColor];

CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, self.colr.CGColor);

CGContextSetFillColorWithColor(context, self.colr.CGColor);

struct coord oldPoint1,oldPoint2,newPoint1,newPoint2;

oldPoint1.x = Xmin;
oldPoint2.x = Xmin;
oldPoint1.y = baseY;
oldPoint2.y = baseY;

for(CGFloat i= Xmin; i<=Xmax ;  )
{
i+=0.1;
newPoint1.x = i;
newPoint2.x = i;

CGFloat sol = redious;
sol = (sol*sol)-((i-baseX)*(i-baseX));

sol = fabs(sol);

CGFloat sq = sqrt((double)sol);

newPoint1.y = baseY+sq;
newPoint2.y = baseY-sq;

CGContextMoveToPoint(context, oldPoint1.x, oldPoint1.y);
CGContextAddLineToPoint(context, newPoint1.x, newPoint1.y);
CGContextStrokePath(context);

CGContextMoveToPoint(context, oldPoint2.x, oldPoint2.y);
CGContextAddLineToPoint(context, newPoint2.x, newPoint2.y);
CGContextStrokePath(context);

oldPoint1.x = newPoint1.x;
oldPoint1.y = newPoint1.y;

oldPoint2.x = newPoint2.x;
oldPoint2.y = newPoint2.y;
}

CGContextMoveToPoint(context, oldPoint1.x, oldPoint1.y);
CGContextAddLineToPoint(context, oldPoint2.x, oldPoint2.y);
CGContextStrokePath(context);

self.colr = [UIColor whiteColor];
CGContextSetStrokeColorWithColor(context, self.colr.CGColor);
CGContextSetFillColorWithColor(context, self.colr.CGColor);

for(double i=0.0;i<360.0;i+=30.0)
{
CGFloat x = fabs(160.0 – 150.0*cos(DegreesToRadians(i)));
CGFloat y = fabs(230.0 – 150.0*sin(DegreesToRadians(i)));

int value = 9 + (int)i/30;
if(value%12 == 0)
value = 12;
else
value = value%12;

[[NSString stringWithFormat:@”%i”,value] drawAtPoint:CGPointMake(x,y) withFont:[UIFont systemFontOfSize:10.5]];
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s