2011-10-25 23 views
3

我想要在這裏使用de casteljau算法爲我的任務之一找到生成貝塞爾曲線的方法。我能夠使用常規方法生成貝塞爾曲線,但無法使用上述算法開始生成。如果有人能夠建議我正確指導或分享您擁有的任何代碼,這將非常有幫助。我不是在問現狀。我在它上面做了很多工作,並自己編寫了下面的代碼來生成曲線。 我發現了一個完全我需要的web小程序。 (http://www2.mat.dtu.dk/people/J.Gravesen/cagd/decast.html)。建議我如何做到這一點在C++中使用De Casteljau算法繪製貝塞爾曲線OpenGL

#include <iostream> 
using std::cerr; 
using std::endl; 
#include <stdlib.h> 
//using std::exit; 
#include <GL/glut.h> // GLUT stuff, includes OpenGL headers as well 
#include <windows.h> 
#include <math.h> 
#include <gl/Gl.h> 
#include <gl/Glu.h> 


int SCREEN_HEIGHT = 480; 
// Keep track of times clicked, on 3 clicks draw. 
int NUMPOINTS = 0; 

// Point class to keep it a little cleaner. 
class Point { 
public: 
    float x, y, z; 
    void setxy(float x2, float y2) { x = x2; y = y2; } 
    const Point & operator=(const Point &rPoint) { 
     x = rPoint.x; 
     y = rPoint.y; 
     z = rPoint.z; 

     return *this; 
     } 

}; 

Point abc[4]; 

void myInit() { 
    glClearColor(0.0,0.0,0.0,0.0); 
    glColor3f(1.0,0.0,0.0); 
    glPointSize(4.0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0.0,640.0,0.0,480.0); 

} 

void drawDot(int x, int y) { 
    glBegin(GL_POINTS); 
    glVertex2i(x,y); 
    glEnd(); 
    glFlush(); 
} 

void drawLine(Point p1, Point p2) { 
    glBegin(GL_LINES); 
     glVertex3f(p1.x, p1.y, p1.z); 
     glVertex3f(p2.x, p2.y, p2.z); 

    glEnd(); 
    glFlush(); 
} 

// Calculate the next bezier point. 
Point drawBezier(Point A, Point B, Point C, Point D, double t) { 
    Point P; 




    P.x = pow((1 - t), 3) * A.x + 3 * t * pow((1 -t), 2) * B.x + 3 * (1-t) * pow(t, 2)* C.x + pow (t, 3)* D.x; 
    P.y = pow((1 - t), 3) * A.y + 3 * t * pow((1 -t), 2) * B.y + 3 * (1-t) * pow(t, 2)* C.y + pow (t, 3)* D.y; 
    P.z = pow((1 - t), 3) * A.z + 3 * t * pow((1 -t), 2) * B.z + 3 * (1-t) * pow(t, 2)* C.z + pow (t, 3)* D.z; 

    return P; 
} 

void myMouse(int button, int state, int x, int y) { 
    // If left button was clicked 
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { 
     // Store where the user clicked, note Y is backwards. 
    abc[NUMPOINTS].setxy((float)x,(float)(SCREEN_HEIGHT - y)); 
    NUMPOINTS++; 

    // Draw the red dot. 
    drawDot(x, SCREEN_HEIGHT - y); 

    // If 3 points are drawn do the curve. 
    if(NUMPOINTS == 4) { 
     glColor3f(1.0,1.0,1.0); 
     // Draw two legs of the triangle 
     drawLine(abc[0], abc[1]); 
     drawLine(abc[1], abc[2]); 
     drawLine(abc[2], abc[3]); 
     //drawLine(abc[3], abc[4]); 
     Point POld = abc[0]; 
     /* Draw each segment of the curve. Make t increment in 
        smaller amounts for a more detailed curve. */ 
     for(double t = 0.0;t <= 1.0; t += 0.1) { 
      Point P = drawBezier(abc[0], abc[1], abc[2], abc[3], t); 
      drawLine(POld, P); 
      POld = P; 
     } 
     glColor3f(1.0,0.0,0.0); 
     NUMPOINTS = 0; 
    } 
    } 
} 

void myDisplay() { 
    glClear(GL_COLOR_BUFFER_BIT); 
    glFlush(); 
} 

int main(int argc, char *argv[]) { 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); 
    glutInitWindowSize(640,480); 
    glutInitWindowPosition(100,150); 
    glutCreateWindow("Bezier Curve"); 

    glutMouseFunc(myMouse); 
    glutDisplayFunc(myDisplay); 

    myInit(); 
    glutMainLoop(); 

    return 0; 
} 
+4

您的曲線生成問題到底是什麼?也許你想分享一些特定輸入值的輸出結果,並將它與applet的結果進行比較? 或者你想要一個類似於applet的GUI? – ksming

回答

4

對我有用,是什麼問題?
Image

我不會張貼此作爲一個答案,但我不能評論

+0

這不是De Casteljau算法的實現。這是伯恩斯坦多項式的實現。 – anonymous

2

張貼圖片以下this link你可以發現一個互動的JavaScript實現。

只要注意點AB如何例如使用((1-t)*A + t*B)計算,看看其他點(BCCDABCBCD最後ABCD)也同樣計算。

您可以拖動ABCD和點AB看到建造工程,如何根據參數t