/* Program to draw a Bezier Curve using graphics.h */
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>
void drawBezier(int x0, int y0, int x1, int y1,
int x2, int y2, int x3, int y3) {
double t;
int x, y;
for (t = 0.0; t <= 1.0; t += 0.001) {
// Cubic Bezier formula
x = pow(1 - t, 3) * x0 +
3 * t * pow(1 - t, 2) * x1 +
3 * t * t * (1 - t) * x2 +
pow(t, 3) * x3;
y = pow(1 - t, 3) * y0 +
3 * t * pow(1 - t, 2) * y1 +
3 * t * t * (1 - t) * y2 +
pow(t, 3) * y3;
putpixel(x, y, WHITE);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// Control points
int x0 = 100, y0 = 400;
int x1 = 150, y1 = 100;
int x2 = 450, y2 = 100;
int x3 = 500, y3 = 400;
// Draw control polygon (optional)
setcolor(LIGHTGRAY);
line(x0, y0, x1, y1);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
// Draw control points
setcolor(YELLOW);
fillellipse(x0, y0, 3, 3);
fillellipse(x1, y1, 3, 3);
fillellipse(x2, y2, 3, 3);
fillellipse(x3, y3, 3, 3);
// Draw Bezier Curve
setcolor(WHITE);
drawBezier(x0, y0, x1, y1, x2, y2, x3, y3);
getch();
closegraph();
return 0;
}