私は問題を解決するように思われた簡単な質問Bezier curve algorithm in objective-cを尋ねました。私はこの新しい質問に、古いものを再利用するのではなく、十分に違うと思っています。目的関数-cのベジエ曲線アルゴリズムは微調整が必要
私はベジエ曲線のアルゴリズムのように思えますが、NSBezierPath
のバージョンに比べて大きな問題があります。ある種の曲線が非常に歪んでいるかのように見えます。あなたは上の画像から違いを見ることができる
は、赤い線が私の関数であり、明るい色は、バージョンに組み込まれています。私はではありません。ピクセルのためのピクセルですが、あなたが見ることができるように、時には赤い線がコースから離れます。
私がリストしている最初のメソッドは、2つのベジェメソッドを呼び出すもので、入力が両方のバージョンと同じであることを示しています。
- (void)MakeBezier
{
int x1 = [self getMegaNumber:2];
int y1 = self.frame.size.height - [self getMegaNumber:2];
int x2 = [self getMegaNumber:2];
int y2 = self.frame.size.height - [self getMegaNumber:2];
int x3 = [self getMegaNumber:2];
int y3 = self.frame.size.height - [self getMegaNumber:2];
int x4 = [self getMegaNumber:2];
int y4 = self.frame.size.height - [self getMegaNumber:2];
int cnt = [self getMegaNumber:2];
NSBezierPath *bezierPath = [[NSBezierPath alloc] init];
[bezierPath setLineWidth:1.0f];
[bezierPath moveToPoint:NSMakePoint(x1, y1)];
[bezierPath curveToPoint:NSMakePoint(x4, y4) controlPoint1:NSMakePoint(x2, y2) controlPoint2:NSMakePoint(x3, y3)];
// Draw path to image with build in NSBezierPath
[self drawPath:bezierPath fill:NO];
// Draw path with custom algorithm
[self drawBezierFrom:NSMakePoint(x1, y1) to:NSMakePoint(x4, y4) controlA:NSMakePoint(x2, y2) controlB:NSMakePoint(x3, y3) sections:cnt color:4];
}
この次のメソッドは、サンプル画像内の赤い線を描画するために使用されるカスタムアルゴリズムです。
- (void)drawBezierFrom:(NSPoint)from to:(NSPoint)to controlA:(NSPoint)a controlB:(NSPoint)b sections:(NSUInteger)cnt color:(NSUInteger)color
{
float qx, qy;
float q1, q2, q3, q4;
int lastx = - 1, lasty;
int plotx, ploty;
float t = 0.0;
while (t <= 1)
{
q1 = t*t*t*-1 + t*t*3 + t*-3 + 1;
q2 = t*t*t*3 + t*t*-6 + t*3;
q3 = t*t*t*-3 + t*t*3;
q4 = t*t*t;
qx = q1*from.x + q2*a.x + q3*to.x + q4*b.x;
qy = q1*from.y + q2*a.y + q3*to.y + q4*b.y;
plotx = round(qx);
ploty = round(qy);
if (lastx != -1)
[self drawLineFrom:NSMakePoint(lastx, lasty) to:NSMakePoint(plotx, ploty) color:color];
else
[self drawLineFrom:NSMakePoint(from.x, from.y) to:NSMakePoint(plotx, ploty) color:color];
lastx = plotx;
lasty = ploty;
t = t + (1.0/(cnt + 0.0f));
}
[self drawLineFrom:NSMakePoint(lastx, lasty) to:NSMakePoint(to.x, to.y) color:color];
}
私の質問は次のとおりです。カスタムアルゴリズムがオフになっているのですか、それとも、特定の種類の行やそれ以外のもののためのエッジケースが欠落していますか?どちらの方法であれ、アルゴリズムを修正する助けがあれば、非常に感謝しています。繰り返すと、私はではなく、ピクセルの完璧なマッチを探していますが、私は一緒にラインナップすることを期待しています。
Q1〜Q4は、私には正しく見えます。しかし、「b」と「to」は入れ替えるべきです。良いキャッチ:) – thundersteele
@thundersteele彼はベジエ曲線ではなく、Bスプラインをやっているのではないですか? –
私はここで混乱しているかもしれませんが、あなたがリンクしているwikiページを見て、q1-q4はキュービックベジェ曲線の式のP0-P3の係数に対応しています。 – thundersteele