6.
Write a C program to implement DDA Algorithm for drawing a line
segment between two given end points A (x1,y1) and B(x2, y2).
#include <graphics.h>
#include <conio.h>
#include <math.h>
void main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2;
int steps, k;
float xInc, yInc, x, y;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Input end points */
x1 = 50; y1 = 100;
x2 = 350; y2 = 250;
/* Calculate dx and dy */
int dx = x2 - x1;
int dy = y2 - y1;
/* Calculate steps */
if (abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
/* Calculate increments */
xInc = dx / (float) steps;
yInc = dy / (float) steps;
/* Initial point */
x = x1;
y = y1;
/* Draw line using DDA */
for (k = 0; k <= steps; k++)
{
putpixel((int)(x + 0.5), (int)(y + 0.5), WHITE);
x = x + xInc;
y = y + yInc;
}
getch();
closegraph();
}
Output—
A****************B
7. Write a program to implement Bresenham’s line algorithm for drawing a
line segment between two given endpoints A(x1, y1) and B(x2, y2)
#include <graphics.h>
#include <conio.h>
#include <math.h>
void main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2;
int dx, dy, p, x, y;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Given end points */
x1 = 60; y1 = 120;
x2 = 360; y2 = 220;
dx = abs(x2 - x1);
dy = abs(y2 - y1);
/* Initial decision parameter */
p = 2 * dy - dx;
x = x1;
y = y1;
/* Plot first point */
putpixel(x, y, WHITE);
/* Bresenham algorithm (slope < 1) */
while (x < x2)
{
x = x + 1;
if (p < 0)
{
p = p + 2 * dy;
}
else
{
y = y + 1;
p = p + 2 * (dy - dx);
}
putpixel(x, y, WHITE);
}
getch();
closegraph();
}
Output--
A***************B
8. Write a program to implement mid point circle generation Algorithm for
drawing a circle of given center (x, y) and radius r.
#include <graphics.h>
#include <conio.h>
void main()
{
int gd = DETECT, gm;
int xc, yc, r;
int x, y;
int p;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Given center and radius */
xc = 250;
yc = 180;
r = 70;
/* Initial values */
x = 0;
y = r;
p = 1 - r;
/* Midpoint Circle Algorithm */
while (x <= y)
{
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
if (p < 0)
{
p = p + (2 * x) + 3;
}
else
{
p = p + (2 * (x - y)) + 5;
y = y - 1;
}
x = x + 1;
}
getch();
closegraph();
}
Output-
**** ****
*** ***
** **
* *
* (250,180) *
* *
** **
*** ***
**** ****
9. Write a program to implement Bresenham’s circle generation algorithm for
drawing a circle of given center (x,y) and radius r.
#include <graphics.h>
#include <conio.h>
void main()
{
int gd = DETECT, gm;
int xc, yc, r;
int x, y;
int p;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Given center and radius */
xc = 300;
yc = 200;
r = 90;
/* Initial values */
x = 0;
y = r;
p = 3 - (2 * r);
/* Bresenham's Circle Algorithm */
while (x <= y)
{
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
if (p < 0)
{
p = p + (4 * x) + 6;
}
else
{
p = p + 4 * (x - y) + 10;
y = y - 1;
}
x = x + 1;
}
getch();
closegraph();
}
Output---
***** *****
*** ***
** **
* *
* (300,200) *
* *
** **
*** ***
***** *****
10. Write a C program for performing the basic 2D transformations such as
translation, Scaling, Rotation, shearing and reflection for a given 2D object.
#include <graphics.h>
#include <conio.h>
#include <math.h>
void drawTriangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void main()
{
int gd = DETECT, gm;
int x1=100, y1=100, x2=150, y2=50, x3=200, y3=100;
int tx=100, ty=50; /* Translation factors */
float sx=1.5, sy=1.5; /* Scaling factors */
float angle = 45 * (3.14/180); /* Rotation angle */
int shx = 1; /* Shearing factor */
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Original object */
setcolor(WHITE);
outtextxy(50,20,"Original Object");
drawTriangle(x1,y1,x2,y2,x3,y3);
/* Translation */
setcolor(RED);
outtextxy(50,140,"Translation");
drawTriangle(x1+tx,y1+ty, x2+tx,y2+ty, x3+tx,y3+ty);
/* Scaling */
setcolor(GREEN);
outtextxy(50,260,"Scaling");
drawTriangle(x1*sx,y1*sy, x2*sx,y2*sy, x3*sx,y3*sy);
/* Rotation */
setcolor(YELLOW);
outtextxy(300,20,"Rotation");
drawTriangle(
x1*cos(angle)-y1*sin(angle), x1*sin(angle)+y1*cos(angle),
x2*cos(angle)-y2*sin(angle), x2*sin(angle)+y2*cos(angle),
x3*cos(angle)-y3*sin(angle), x3*sin(angle)+y3*cos(angle)
);
/* Shearing */
setcolor(CYAN);
outtextxy(300,140,"Shearing");
drawTriangle(x1+y1*shx,y1, x2+y2*shx,y2, x3+y3*shx,y3);
/* Reflection (about X-axis) */
setcolor(MAGENTA);
outtextxy(300,260,"Reflection");
drawTriangle(x1,-y1+400, x2,-y2+400, x3,-y3+400);
getch();
closegraph();
}
Output—
Original Triangle Rotated Triangle
/\ /\
/ \ / \
/___ \ /____\
Translated Triangle Sheared Triangle
/\ /|
/ \ / |
/____\ /_. |
Scaled Triangle Reflected Triangle
/\ \/
/ \ / \
/____\ /____\
11. To develop programs for making simple animations using transformations
like rotation, scaling and translation like bouncing ball, walking man, moving
car.
#include <graphics.h>
#include <conio.h>
#include <dos.h>
void main()
{
int gd = DETECT, gm;
int x = 100, y = 100; /* Initial position */
int r = 20; /* Radius of ball */
int dx = 5, dy = 5; /* Translation factors */
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
while (!kbhit())
{
cleardevice();
/* Draw the ball */
setcolor(RED);
circle(x, y, r);
setfillstyle(SOLID_FILL, RED);
floodfill(x, y, RED);
/* Translation (movement) */
x = x + dx;
y = y + dy;
/* Boundary checking (bouncing effect) */
if (x >= getmaxx() - r || x <= r)
dx = -dx;
if (y >= getmaxy() - r || y <= r)
dy = -dy;
delay(30);
}
closegraph();
}
Output—
Frame 1: O
Frame 2: O
Frame 3: O
Frame 4: O
Frame 5: O
12. By using the concept of flood fill algorithm , write a C program for filling a
given object with color.
#include <graphics.h>
#include <conio.h>
Void main()
{
Int gd = DETECT, gm;
Initgraph(&gd, &gm, “C:\\TURBOC3\\BGI”);
/* Draw a closed object (rectangle) – changed digits */
Setcolor(WHITE);
Rectangle(200, 150, 450, 350);
/* Set fill style and color */
Setfillstyle(SOLID_FILL, BLUE);
/* Apply flood fill from a new seed point */
Floodfill(300, 250, WHITE);
Getch();
Closegraph();
}
Output—
______________________
| |
| ████████████ |
| ████████████ |
| ████████████ |
| |
|____________________ | |____________________|
13. By using the concept of Boundary fill algorithm, write a C- program for
filling a given object with color.
#include <graphics.h>
#include <conio.h>
/* Recursive boundary fill function */
void boundaryFill(int x, int y, int fill_color, int boundary_color)
{
int current_color;
current_color = getpixel(x, y);
if (current_color != boundary_color && current_color != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill(x + 1, y, fill_color, boundary_color);
boundaryFill(x - 1, y, fill_color, boundary_color);
boundaryFill(x, y + 1, fill_color, boundary_color);
boundaryFill(x, y - 1, fill_color, boundary_color);
}
}
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Draw a closed object (circle) */
setcolor(WHITE);
circle(300, 200, 80);
/* Apply boundary fill */
boundaryFill(300, 200, GREEN, WHITE);
getch();
closegraph();
}
Output---
*************
*** ***
** ███████ **
* █████████ *
** ███████ **
*** ***
*************
14. By using the concept of Scan line polygon fill algorithm , write a C-
program for filling a given object with color.
#include <graphics.h>
#include <conio.h>
void main()
{
int gd = DETECT, gm;
int poly[10] = {200, 150, 300, 100, 400, 150, 350, 250, 250, 250};
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Draw the polygon */
setcolor(WHITE);
drawpoly(5, poly);
/* Set fill style and color */
setfillstyle(SOLID_FILL, GREEN);
/* Fill polygon using scan line concept */
fillpoly(5, poly);
getch();
closegraph();
}
Output--
/\
/ \
/ \
|██████|
|██████|
\ ____ /
15. Write a program in C language to clip a line segment using Cohen –
Sutherland Algorithm.
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
float xmin = 150, ymin = 150, xmax = 400, ymax = 350;
/* Compute region code */
int computeCode(float x, float y)
{
int code = 0;
if (x < xmin)
code |= LEFT;
else if (x > xmax)
code |= RIGHT;
if (y < ymin)
code |= BOTTOM;
else if (y > ymax)
code |= TOP;
return code;
}
/* Cohen–Sutherland Line Clipping Algorithm */
void cohenSutherland(float x1, float y1, float x2, float y2)
{
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
int accept = 0;
while (1)
{
if ((code1 | code2) == 0)
{
accept = 1;
break;
}
else if (code1 & code2)
{
break;
}
else
{
float x, y;
int outCode = code1 ? code1 : code2;
if (outCode & TOP)
{
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
}
else if (outCode & BOTTOM)
{
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
}
else if (outCode & RIGHT)
{
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
}
else if (outCode & LEFT)
{
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (outCode == code1)
{
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else
{
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept)
{
setcolor(GREEN);
line(x1, y1, x2, y2);
}
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
/* Draw clipping window */
rectangle(xmin, ymin, xmax, ymax);
/* Original line */
setcolor(RED);
line(50, 200, 450, 300);
/* Clipped line */
cohenSutherland(50, 200, 450, 300);
getch();
closegraph();
return 0;
}
Output--
Y
↑
|
| ┌───────────────────────┐
| | GREEN LINE |
| RED---/------==================/----RED
| | |
| └───────────────────────┘
|
+------------------------------------------------→ X
16. Write a C program to clip a polygon using Cohen- Hodgeman Algorithm.
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#define MAX 20
float xmin = 150, ymin = 150, xmax = 450, ymax = 350;
typedef struct {
float x, y;
} Point;
Point poly[MAX], newpoly[MAX];
int n;
/* Clip against LEFT edge */
int clipLeft(Point in[], int cnt, Point out[])
{
int i, newcnt = 0;
Point S, P;
for (i = 0; i < cnt; i++)
{
S = in[i];
P = in[(i + 1) % cnt];
if (S.x >= xmin && P.x >= xmin)
out[newcnt++] = P;
else if (S.x >= xmin && P.x < xmin)
{
out[newcnt].x = xmin;
out[newcnt].y = S.y + (P.y - S.y) * (xmin - S.x) / (P.x - S.x);
newcnt++;
}
else if (S.x < xmin && P.x >= xmin)
{
out[newcnt].x = xmin;
out[newcnt].y = S.y + (P.y - S.y) * (xmin - S.x) / (P.x - S.x);
newcnt++;
out[newcnt++] = P;
}
}
return newcnt;
}
/* Clip against RIGHT edge */
int clipRight(Point in[], int cnt, Point out[])
{
int i, newcnt = 0;
Point S, P;
for (i = 0; i < cnt; i++)
{
S = in[i];
P = in[(i + 1) % cnt];
if (S.x <= xmax && P.x <= xmax)
out[newcnt++] = P;
else if (S.x <= xmax && P.x > xmax)
{
out[newcnt].x = xmax;
out[newcnt].y = S.y + (P.y - S.y) * (xmax - S.x) / (P.x - S.x);
newcnt++;
}
else if (S.x > xmax && P.x <= xmax)
{
out[newcnt].x = xmax;
out[newcnt].y = S.y + (P.y - S.y) * (xmax - S.x) / (P.x - S.x);
newcnt++;
out[newcnt++] = P;
}
}
return newcnt;
}
/* Clip against BOTTOM edge */
int clipBottom(Point in[], int cnt, Point out[])
{
int i, newcnt = 0;
Point S, P;
for (i = 0; i < cnt; i++)
{
S = in[i];
P = in[(i + 1) % cnt];
if (S.y >= ymin && P.y >= ymin)
out[newcnt++] = P;
else if (S.y >= ymin && P.y < ymin)
{
out[newcnt].y = ymin;
out[newcnt].x = S.x + (P.x - S.x) * (ymin - S.y) / (P.y - S.y);
newcnt++;
}
else if (S.y < ymin && P.y >= ymin)
{
out[newcnt].y = ymin;
out[newcnt].x = S.x + (P.x - S.x) * (ymin - S.y) / (P.y - S.y);
newcnt++;
out[newcnt++] = P;
}
}
return newcnt;
}
/* Clip against TOP edge */
int clipTop(Point in[], int cnt, Point out[])
{
int i, newcnt = 0;
Point S, P;
for (i = 0; i < cnt; i++)
{
S = in[i];
P = in[(i + 1) % cnt];
if (S.y <= ymax && P.y <= ymax)
out[newcnt++] = P;
else if (S.y <= ymax && P.y > ymax)
{
out[newcnt].y = ymax;
out[newcnt].x = S.x + (P.x - S.x) * (ymax - S.y) / (P.y - S.y);
newcnt++;
}
else if (S.y > ymax && P.y <= ymax)
{
out[newcnt].y = ymax;
out[newcnt].x = S.x + (P.x - S.x) * (ymax - S.y) / (P.y - S.y);
newcnt++;
out[newcnt++] = P;
}
}
return newcnt;
}
void drawPolygon(Point p[], int cnt, int color)
{
int i;
setcolor(color);
for (i = 0; i < cnt; i++)
line(p[i].x, p[i].y, p[(i + 1) % cnt].x, p[(i + 1) % cnt].y);
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
/* Polygon vertices (changed units) */
n = 5;
poly[0] = (Point){100, 200};
poly[1] = (Point){250, 100};
poly[2] = (Point){500, 200};
poly[3] = (Point){400, 400};
poly[4] = (Point){200, 400};
rectangle(xmin, ymin, xmax, ymax);
drawPolygon(poly, n, RED);
n = clipLeft(poly, n, newpoly);
n = clipRight(newpoly, n, poly);
n = clipBottom(poly, n, newpoly);
n = clipTop(newpoly, n, poly);
drawPolygon(poly, n, GREEN);
getch();
closegraph();
return 0;
}
Output--
Y
↑
| ┌─────────────────────────┐
| | GREEN POLYGON |
| RED | (CLIPPED RESULT) |
|---------/---------------------------------/--------------
| | |
| └─────────────────────────┘
|
+-----------------------------------------------------------------→ X
2. Write a program in C to understand Graphics Mode Initializations and draw
a line.
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
/* Initialize graphics mode */
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
/* Draw a line */
line(50, 150, 400, 300);
/* Display text */
outtextxy(60, 120, "Graphics Mode Initialization - Line Drawing");
getch();
closegraph();
return 0;
}
Output--
Y
↑
|
| Graphics Mode Initialization - Line Drawing
|
| -------------------------------
| /
| /
| /
|
+----------------------------------------→ X
INDEX
[Link] PROGRAM TITLE PAGE SIGN.
NO.
1. Write a computer graphics program in C Language to draw two
dimensional objects like a man ,hut etc.
2. Write a program in C to understand Graphics Mode Iniatializations and
draw a line.
3. Write a program in C to draw diagonal lines.
4. Write programs in C to implement built- in Graphics Functions.
5. Write a C program for displaying text in different sizes, different colors,
and different font styles by using graphics function.
6. Write a C program to implement DDA Algorithm for drawing a line
segment between two given end points A(x1, y1) and B(x2, y2).
7. Write a program to implement Bresenham’s line drawing algorithm for
drawing a line segment between two given end points A(x1, y1) and
B(x2, y2).
8. Write a program to implement midpoint circle generation algorithm for
drawing a circle of given center(x, y) and radius r.
9. Write a program to implement Bresenham’s circle generation algorithm
for drawing a circle of given center (x,y)and radius r.
10. Write a C program for performing the basic 2D transformations such as
translation, Scaling, Rotation, shearing and reflectionfor a given 2D
object.
11. To develop programs for making simple animations using
transformations like rotation, scaling, and translation like bouncing ball,
walking man , moving car.
12. By using the concept of flood fill algorithm, write a C program for filling a
given object with color.
13. By using the concept of Boundary fill algorithm, write a C program for
filling a given object with color.
14. By using the concept of Scan line polygon fill algorithm, write a C –
program for filling a given object with color.
15. Write a program in C language to clip a line segment using Cohen-
Sutherland Algorithm.
16. Write a C program to clip a polygon using Cohen- Hodgeman Algorithm.
SOUTH POINT GROUP OF INSTITUTIONS,
SONIPAT
Academic Year 2024-26
PRACTICAL FILE
ON
COMPUTER GRAPHICS
SUBMITTED BY- SUBMITTED TO-
NAME --- Komal Antil
ROLL NO.---
CLASS --- [Link] Mathematics