2017-05-05 5 views
0

Ich zeichne eine quadratische Bezier-Kurve, und ich bin mir nicht sicher, warum es sich so verhält, wie es ist. Es scheint fast so, als gäbe es einen anderen Kontrollpunkt, der die Kurve beeinflusst, aber es gibt nur 3 Kontrollpunkte, und die Formel ist nur für quadratische Kurven.Was ist los mit meiner Bezier-Kurve?

Hier sind einige Bilder von dem, was er tut: http://imgur.com/a/wOWSe

Und Code:

Point Bezier::evaluate(float t) 
{ 
    // [x,y]=(1–t)^2*2P0+2(1–t)t*P1+t^2*P2 

    Point P; 
    P.x = (1 - t)*(1 - t)*points[0].x + 
      2*(1 - t)*(1 - t)*t*points[1].x + 
      t*t*points[2].x; 

    P.y = (1 - t)*(1 - t)*points[0].y + 
      2*(1 - t)*(1 - t)*t*points[1].y + 
      t*t*points[2].y; 

    return P; 
} 

void Bezier::drawCurve() 
{ 
    glColor3d(red, green, blue); 
    Point lastPoint = points[0]; 

    for (float t = 0.0; t <= 1.0; t += 0.01) 
    { 
     Point currentPoint = evaluate(t); 
     drawLine(lastPoint.x, lastPoint.y, currentPoint.x, currentPoint.y); 
     lastPoint = currentPoint; 
    } 
} 

void Bezier::drawHandles() 
{ 
    glColor3d(red, green, blue); 

    for (int i = 0; i < 3; i++) 
    { 
     drawCircle(points[i].x, points[i].y, points[i].radius); 
    } 
} 

Kopf

class Point 
{ 
    public: 
     float x; 
     float y; 
     float radius = 5; 
}; 

class Bezier 
{ 
    public: 
     Bezier(float startX, float startY, float endX, float endY, float red, float green, float blue); 
     Point evaluate(float time); 
     void drawCurve(); 
     void drawHandles(); 

     Point points[3]; 

     float red; 
     float green; 
     float blue; 
}; 

Vielen Dank im Voraus für jede Hilfe!

Antwort

6

Die Formel, die Sie verwenden, ist falsch

P.x = (1 - t)*(1 - t)*points[0].x + 
     2*(1 - t)*t*points[1].x + 
     t*t*points[2].x; 

P.y = (1 - t)*(1 - t)*points[0].y + 
     2*(1 - t)*t*points[1].y + 
     t*t*points[2].y; 

Sie haben einen zusätzlichen (1-t) im zweiten Glied.

+0

agh Ich dachte, es war in der Formel und lesen Sie es dreimal vor dem Posting, nur um sicher zu sein. Irgendwie ist das vorbeigerutscht. Vielen Dank! – Grav

+0

Keine Sorge! Das passiert die ganze Zeit. –