Implement an interactive Bezier curve drawing
tool where users can modify control points
S. Y. B. Tech Computer Engineering
Project 2
[BTECCE23401: Computer Graphics and
Gaming]
By
Atharva Galwade (31242599)
2024-2025
Pursuing in
Department of Computer Engineering
Faculty of Science & Technology
1
Vishwakarma University, Pune
Index
Sr. No. Content Page No.
1 Introduction 3
2 Algorithm & objectives 3-4
3 Code 5 – 10
4 Result 11-12
5 Conclusion 12
2
Introduction:
Bezier curves play a vital role in the field of computer graphics,
digital design, and animation. They are widely used in designing
smooth curves and paths that can be easily controlled and
adjusted using control points. This project focuses on
implementing an interactive Bezier curve drawing tool using C+
+ with BGI graphics. The tool allows users to visually manipulate
a cubic Bezier curve by dragging the control points with a
mouse, offering real-time feedback and visual learning of how
these curves behave.
Algorithm to draw Bezier curves:
Step 1: Input:
Control points P0, P1, P2, P3
Number of steps n (for smoothness)
Step 2: Initialize:
t = 0.0
step = 1.0 / n
Step 3: Loop from i = 0 to n:
Compute blending coefficients:
3
Calculate the point B(t):
Plot or store the point (x, y)
Increment t = t + step
Step 4: Repeat:
until t > 1.0
Objectives:
To implement a cubic Bezier curve generator
using C++.
To enable real-time interaction with the curve
through mouse input.
To provide a visual and intuitive way to learn and
understand Bezier curves.
To enhance skills in graphics programming and
low-level mouse handling using interrupts.
4
To develop a small utility tool that demonstrates
user interaction in computer graphics.
Code:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <iostream.h>
#define NUM_CTRL 4
#define RADIUS 6
#define STEPS 200
/* simple 2D point */
struct Point { int x, y; } ;
/* four global control points */
Point P[NUM_CTRL] = {
{100, 400},
{200, 100},
{400, 100},
{500, 400}
};
/* which point is currently “grabbed”? */
int sel = -1;
/* is left mouse button down? */
int leftDown = 0;
/* function prototypes */
int initMouse(void);
5
void showMouse(void);
void hideMouse(void);
void getMouse(int *mx, int *my, int *btns);
Point bezierPoint(double t);
void drawScene(void);
/* initialize/install mouse driver (int 33h) */
int initMouse(void)
{
union REGS in, out;
[Link] = 0; /* reset & install */
int86(0x33, &in, &out);
return ([Link] != 0); /* non-zero ⇒ OK */
}
/* show/hide mouse pointer */
void showMouse(void)
{
union REGS regs;
[Link] = 1;
int86(0x33, ®s, ®s);
}
void hideMouse(void)
{
union REGS regs;
[Link] = 2;
int86(0x33, ®s, ®s);
}
/* get mouse position & button state */
void getMouse(int *mx, int *my, int *btns)
{
6
union REGS in, out;
[Link] = 3;
int86(0x33, &in, &out);
*mx = [Link];
*my = [Link];
*btns = [Link] & 0x07;
}
/* compute a point on the cubic Bézier at parameter t∈[0,1] */
Point bezierPoint(double t)
{
double u = 1.0 - t;
double b0 = u*u*u;
double b1 = 3.0*u*u*t;
double b2 = 3.0*u*t*t;
double b3 = t*t*t;
Point Q;
Q.x = (int)(b0*P[0].x + b1*P[1].x + b2*P[2].x + b3*P[3].x + 0.5);
Q.y = (int)(b0*P[0].y + b1*P[1].y + b2*P[2].y + b3*P[3].y + 0.5);
return Q;
}
/* draw the curve + control polygon + points */
void drawScene(void)
{
int i;
Point prev, cur;
cleardevice();
/* draw Bézier curve in YELLOW */
7
setcolor(YELLOW);
prev = bezierPoint(0.0);
for (i = 1; i <= STEPS; i++)
{
cur = bezierPoint((double)i / STEPS);
line(prev.x, prev.y, cur.x, cur.y);
prev = cur;
}
/* draw control polygon in WHITE */
setcolor(WHITE);
for (i = 0; i < NUM_CTRL - 1; i++)
line(P[i].x, P[i].y, P[i+1].x, P[i+1].y);
/* draw control points */
for (i = 0; i < NUM_CTRL; i++)
{
setfillstyle(SOLID_FILL, (i == sel) ? RED : BLUE);
circle(P[i].x, P[i].y, RADIUS);
floodfill(P[i].x, P[i].y, WHITE);
}
}
int main(void)
{
int gd = DETECT, gm;
int mx, my, btns;
/* Initialize graphics mode */
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); // Path to your BGI
folder
8
// Error checking for graphics initialization
int err = graphresult();
if (err != grOk)
{
cout << "Graphics init error: " << grapherrormsg(err) << endl;
getch();
return 1;
}
/* Initialize mouse driver */
if (!initMouse())
{
closegraph();
cout << "Mouse driver not found." << endl;
getch();
return 1;
}
showMouse();
while (1)
{
getMouse(&mx, &my, &btns);
/* Left button just pressed? Pick a control point */
if ((btns & 1) && !leftDown)
{
leftDown = 1;
sel = -1;
for (int i = 0; i < NUM_CTRL; i++)
{
int dx = mx - P[i].x;
int dy = my - P[i].y;
9
if (dx * dx + dy * dy <= RADIUS * RADIUS)
{
sel = i;
break;
}
}
}
/* Released? Drop it */
else if (!(btns & 1) && leftDown)
{
leftDown = 0;
sel = -1;
}
/* Dragging? Update its position */
else if (leftDown && sel >= 0)
{
P[sel].x = mx;
P[sel].y = my;
}
/* Draw the scene (curve, polygon, points) */
drawScene();
delay(30);
/* ESC to quit */
if (kbhit() && getch() == 27)
break;
}
hideMouse();
closegraph();
10
return 0;
}
Result:
[Link] output shows the image created by the
code; it is a cubic Bezier curve which has 4
control points.
[Link] cubic curve is now moved using the
control points shown in the image.
11
[Link] can move the image anywhere using the
control points, shown in the below image
Conclusion:
12
This micro project successfully brings the
mathematical elegance of Bezier curves into a visual,
interactive form using C++ graphics. The tool allowed
users to manipulate the curve intuitively by dragging
its control points with the mouse, making it an
effective educational aid.
From a technical perspective, the project
accomplished the integration of mouse-driven input
with graphics rendering using DOS interrupts and BGI
functions. It reinforced concepts of curve evaluation,
geometric interpolation, and event-driven
programming.
Overall, the project not only achieved its objective but
also served as a strong foundation for understanding
more complex graphic tools such as vector design
software, animation systems, and path editors used in
modern applications.
13