I hope it is possible to draw a line using center point and angle. I need to draw the line to move along with a circle.But I couldn’t make it work. I am not getting any idea how could do this!I tried the following code to rotate the line by specified angle, it didn’t work. As I am fairly new, I couldn’t understand where I am making mistake!! This is how it looks if I use the below code
- (void)drawThumbAtPoint:(CGPoint)sliderButtonCenterPoint inContext:(CGContextRef)context {
//Vertical line
CGPoint newPoint = [self convertPoint:sliderButtonCenterPoint toView:self];
CGFloat angleInRadians = (CGFloat)M_PI/180.0f * currentAngle;
CGFloat distance = 15;
CGPoint point = CGPointMake(newPoint.x + distance * sinf(angleInRadians), newPoint.y + distance * cosf(angleInRadians));
UIGraphicsPushContext(context);
CGContextBeginPath(context);
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 10.f;
[path moveToPoint:sliderButtonCenterPoint];
[path addLineToPoint:point];
[[UIColor redColor] set];
[path stroke];
// CGAffineTransform rot = CGAffineTransformMakeRotation(angleInRadians);
// [path applyTransform:rot];
UIGraphicsPopContext();
}
And,
CGPoint thumbCenterPoint = CGContextGetPathCurrentPoint(context);
[self drawThumbAtPoint:thumbCenterPoint inContext:context];
A unit vector with angle
alphato the x-axis isNow you want to draw a line that is tangential to the circle line, so it is perpendicular to the line from the center of the circle to the point on the circle line.
To get a perpendicular vector, you add 90º = π/2 to the angle:
using basic trigonometric identities.
This (hopefully) explains why the endpoint of the line must be computed as