Courbe de Bézier en C

Un article de Wikipédia, l'encyclopédie libre.

Ce code permet de tracer une courbe de Bézier définie par 4 points de contrôle. C'est donc une courbe cubique. Cet algorithme est une implémentation de l'algorithme de de Casteljau.

Pour adapter ce code C à votre système, il vous suffit de remplacer la procédure de tracé de segment (ici en OpenGL).

La structure Global_Bezier contient les 4 points de contrôle de la courbe de Bézier à tracer. level contient le nombre d'appel récursif.

typedef struct {
        float x, y;
 } point;
 typedef struct {
       point P[4];
 } Bezier;
 /* Variables globales */
 Bezier Global_Bezier;
 int level=1;
 // Draw a line with color (r,v,b)
 void DrawLine(int x1, int y1, int x2, int y2,float r, float v, float b)
 {
   glColor3f(r,v,b);
   glBegin(GL_LINES);
   glVertex2i(x1,y1);
   glVertex2i(x2,y2);
   glEnd();
   glFlush();
 }
 void DrawBezierBase(Bezier p,float r,float v,float b)
 {
   DrawLine(p.P[0].x,p.P[0].y,p.P[1].x,p.P[1].y,r,v,b);
   DrawLine(p.P[1].x,p.P[1].y,p.P[2].x,p.P[2].y,r,v,b);
   DrawLine(p.P[2].x,p.P[2].y,p.P[3].x,p.P[3].y,r,v,b);
 }
 void DrawBezierRecursive (Bezier b, int level) 
 {
         if (level <= 0) {
          /* draw a line segment */
               DrawLine((int) (b.P[0].x + 0.5), (int) (b.P[0].y + 0.5),
                               (int) (b.P[3].x + 0.5), (int) (b.P[3].y + 0.5),1,1,1);
         } else {
                 Bezier left, right;                 
                 /* subdivide into 2 Bezier segments */
                 left.P[0].x = b.P[0].x;
                 left.P[0].y = b.P[0].y;
                 left.P[1].x = (b.P[0].x + b.P[1].x) / 2;
                 left.P[1].y = (b.P[0].y + b.P[1].y) / 2;
                 left.P[2].x = (b.P[0].x + 2*b.P[1].x + b.P[2].x) / 4;
                 left.P[2].y = (b.P[0].y + 2*b.P[1].y + b.P[2].y) / 4;
                 left.P[3].x = (b.P[0].x + 3*b.P[1].x + 3*b.P[2].x + b.P[3].x) / 8;
                 left.P[3].y = (b.P[0].y + 3*b.P[1].y + 3*b.P[2].y + b.P[3].y) / 8;
                 DrawBezierBase(left,0,1,0);
                 right.P[0].x = left.P[3].x;
                 right.P[0].y = left.P[3].y;
                 right.P[1].x = (b.P[1].x + 2*b.P[2].x + b.P[3].x) / 4;
                 right.P[1].y = (b.P[1].y + 2*b.P[2].y + b.P[3].y) / 4;
                 right.P[2].x = (b.P[2].x + b.P[3].x) / 2;
                 right.P[2].y = (b.P[2].y + b.P[3].y) / 2;
                 right.P[3].x = b.P[3].x;
                 right.P[3].y = b.P[3].y;
                 DrawBezierBase(right,0,0,1);
                 /* draw the 2 segments recursively */
                 DrawBezierRecursive (left, level -1);
                 DrawBezierRecursive (right, level -1);
         }
 }
 void DrawBezier()
 {
       DrawBezierBase(Global_Bezier,1,0,0);
       DrawBezierRecursive(Global_Bezier,level);
 }

Wikipedia HTML 2008 in other languages

100 000 +

Česká (Czech)  •  English  •  Deutsch (German)  •  日本語 (Japanese)  •  Français (French)  •  Polski (Polish)  •  Suomi (Finnish)  •  Svenska (Swedish)  •  Nederlands (Dutch)  •  Español (Spanish)  •  Italiano (Italian)  •  Norsk (Norwegian Bokmål)  •  Português (Portuguese)  •  Română (Romanian)  •  Русский (Russian)  •  Türkçe (Turkish)  •  Українська (Ukrainian)  •  中文 (Chinese)

10 000 +

العربية (Arabic)  •  Български (Bulgarian)  •  Bosanski (Bosnian)  •  Català (Catalan)  •  Cymraeg (Welsh)  •  Dansk (Danish)  •  Ελληνικά (Greek)  •  Esperanto  •  Eesti (Estonian)  •  Euskara (Basque)  •  Galego (Galician)  •  עברית (Hebrew)  •  हिन्दी (Hindi)  •  Hrvatski (Croatian)  •  Magyar (Hungarian)  •  Ido  •  Bahasa Indonesia (Indonesian)  •  Íslenska (Icelandic)  •  Basa Jawa (Javanese)  •  한국어 (Korean)  •  Latina (Latin)  •  Lëtzebuergesch (Luxembourgish)  •  Lietuvių (Lithuanian)  •  Latviešu (Latvian)  •  Bahasa Melayu (Malay)  •  Plattdüütsch (Low Saxon)  •  Norsk (Norwegian Nynorsk)  •  فارسی (Persian)  •  Sicilianu (Sicilian)  •  Slovenčina (Slovak)  •  Slovenščina (Slovenian)  •  Српски (Serbian)  •  Basa Sunda (Sundanese)  •  தமிழ் (Tamil)  •  ไทย (Thai)  •  Tiếng Việt (Vietnamese)

1 000 +

Afrikaans  •  Asturianu (Asturian)  •  Беларуская (Belarusian)  •  Kaszëbsczi (Kashubian)  •  Frysk (Western Frisian)  •  Gaeilge (Irish)  •  Interlingua  •  Kurdî (Kurdish)  •  Kernewek (Cornish)  •  Māori  •  Bân-lâm-gú (Southern Min)  •  Occitan  •  संस्कृत (Sanskrit)  •  Scots  •  Tatarça (Tatar)  •  اردو (Urdu) Walon (Walloon)  •  יידיש (Yiddish)  •  古文/文言文 (Classical Chinese)

100 +

Nehiyaw (Cree)  •  словѣньскъ (Old Church Slavonic)  •  gutisk (Gothic)  •  ລາວ (Laos)