2011年10月6日木曜日

円弧のNSBezierPath

- (void) appendBezierPathWithArcWithCenter:radius:startAngle:endAngle:でいいじゃんとか言わない.

double CX= __CENTER_X_;
double CY= __CENTER_X_;
double r= __RADIUS__
double b= __RATIO__0_TO_1_;

NSBezierPath *path= [NSBezierPath bezierPath];
double a= 4.0/3.0 * (sqrt(2)-1) * r;
double c= 0.0;
[path moveToPoint:NSMakePoint(+r, 0)];
if (b>0.25) {
[path curveToPoint:NSMakePoint(0, +r)
controlPoint1:NSMakePoint(+r, +a)
controlPoint2:NSMakePoint(+a, +r)];
b-= 0.25;
c+= 90.0;
}
if (b>0.25) {
[path curveToPoint:NSMakePoint(-r, 0)
controlPoint1:NSMakePoint(-a, +r)
controlPoint2:NSMakePoint(-r, +a)];
b-= 0.25;
c+= 90.0;
}
if (b>0.25) {
[path curveToPoint:NSMakePoint(0, -r)
controlPoint1:NSMakePoint(-r, -a)
controlPoint2:NSMakePoint(-a, -r)];
b-= 0.25;
c+= 90.0;
}
if(b==0.25) {
[path curveToPoint:NSMakePoint(+r, 0)
controlPoint1:NSMakePoint(+a, -r)
controlPoint2:NSMakePoint(+r, -a)];
//b-= 0.25;
//c+= 90.0;
}
else if (b<0.25) {
NSAffineTransform *affine= [NSAffineTransform transform];
[affine rotateByDegrees:c];
double t= b/0.25;
double it= (1.-t);
[path curveToPoint:[affine transformPoint:NSMakePoint(+cos(t*M_PI_2)*r, +sin(t*M_PI_2)*r)]
controlPoint1:[affine transformPoint:NSMakePoint(+r, +a*t)]
controlPoint2:[affine transformPoint:NSMakePoint(+(it*it*r+2*it*t*r+t*t*a), +(it*it*0+2*it*t*a+t*t*r))]
];
}
NSAffineTransform *affine= [NSAffineTransform transform];
[affine translateXBy:CX yBy:CY];
//[affine rotateByDegrees:45];
[path transformUsingAffineTransform:affine];


開始点を変更したい場合は最後の平行移動前(書く時は後ろ)にて回転させればよい.(コメント部)
描画ならCGContextAddArcか.

参考: s.h's page - [graphic] ベジエ曲線

0 件のコメント: