我很难理解如何计算圆周中心结的周长的数学背后。我希望你们能给我一些指针。
当前计算在饼图中间设置结img,想将它移动到更接近外部圆圈,如img 2
感谢您查看和评论,感谢任何评论。
我是多么渴望它。
/** Draw a white knob over the circle **/
-(void) drawTheHandle:(CGContextRef)ctx{
CGContextSaveGState(ctx);
NSLog(@"handleCenterA.x %f",handleCenterA.x);
NSLog(@" handleCenterA.y %f", handleCenterA.y);
[[UIColor colorWithWhite:1.0 alpha:1.0]set];
UIImage *myImage = [UIImage imageNamed:@"clock-marker.png"];
//this will give me the result of image 1
[myImage drawInRect:CGRectMake(handleCenterA.x-35, handleCenterA.y-40, TB_BUTTON_WIDTH, TB_BUTTON_WIDTH)];
//this will give me the result of image 2
[myImage drawInRect:CGRectMake(handleCenterA.x-35, handleCenterA.y-40, TB_BUTTON_WIDTH, TB_BUTTON_WIDTH)];
CGContextRestoreGState(ctx);
}
#pragma mark - Math -
/** Move the Handle **/
-(void)movehandle:(CGPoint)lastPoint{
//Get the center
CGPoint centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
//Calculate the direction from a center point and a arbitrary position.
//float currentAngle = AngleFromNorth(centerPoint, lastPoint, NO);
//int angleInt = floor(currentAngle);
//Calculate the direction from the center point to an arbitrary position.
float currentAngle = AngleFromNorth(centerPoint, lastPoint, NO);
int angleInt = 360 - floor(currentAngle);
if (sliderLock == SliderLockedStart) {
self.startAngle = angleInt;
}
//Redraw
[self setNeedsDisplay];
}
- (CGPoint)centerPointFromAngel:(int)angleInt {
CGPoint point = [self pointFromAngle:angleInt];
point.x += TB_BUTTON_WIDTH/2;
point.y += TB_BUTTON_WIDTH/2;
return point;
}
- (CGFloat)distanceBetween:(CGPoint)p1 and:(CGPoint)p2 {
CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
return sqrt((xDist * xDist) + (yDist * yDist));
}
/** Given the angle, get the point position on circumference **/
-(CGPoint)pointFromAngle:(int)angleInt{
//Circle center
CGPoint centerPoint = CGPointMake(self.frame.size.width/2 - (TB_BUTTON_WIDTH/2), self.frame.size.height/2 - (TB_BUTTON_WIDTH/2));
//The point position on the circumference
CGPoint result;
result.y = round(centerPoint.y + radius * sin(ToRad(-angleInt))) ;
result.x = round(centerPoint.x + radius * cos(ToRad(-angleInt)));
return result;
}
//Sourcecode from Apple example clockControl
//Calculate the direction in degrees from a center point to an arbitrary position.
static inline float AngleFromNorth(CGPoint p1, CGPoint p2, BOOL flipped) {
CGPoint v = CGPointMake(p2.x-p1.x,p2.y-p1.y);
float vmag = sqrt(SQR(v.x) + SQR(v.y)), result = 0;
v.x /= vmag;
v.y /= vmag;
double radians = atan2(v.y,v.x);
result = ToDeg(radians);
return (result >=0 ? result : result + 360.0);
}
答案 0 :(得分:0)
终于解决了它。
/ **给定角度,得到圆周上的点位置** /
-(CGPoint)pointFromAngle:(int)angleInt{
//Circle center//TB_BUTTON_WIDTH
CGPoint centerPoint = CGPointMake(self.frame.size.width/2 - (TB_BUTTON_WIDTH/2), self.frame.size.height/2 - (TB_BUTTON_WIDTH/2));
//The point position on the circumference radius
CGPoint result;
result.y = round(centerPoint.y + 120 * sin(ToRad(-angleInt))) ;
result.x = round(centerPoint.x + 120 * cos(ToRad(-angleInt)));
NSLog(@"centerPoint %@",NSStringFromCGPoint(centerPoint));
NSLog(@"result %@",NSStringFromCGPoint(result));
return result;
}