INDEX
SL. ASSIGNMENT PAGE SUBMISSION TEACHER’S
NO. NAME NO. DATE SIGNATURE
Write a C program to Draw
01. a specified figure supplied 1-2 16/04/26
by Instructor.
Write a C program to
02. draw a polygon design 3-4 28/04/26
03. Write a C program to 5-7 30/04/26
implement DDA Line
Drawing Algorithm.
04. Write a C program to 8-10 05/05/26
implement Bresenham’s
Line Drawing Algorithm.
05. Write a C program to 11-13 07/05/26
implement Midpoint circle
generating Algorithm.
06. Write a C program to 14-16 12/05/26
implement Bresenham’s
Circle generating Algorithm.
07. Write a C program to 17-20 14/05/26
implement Mid-point Ellipse
generating Algorithm.
08. Write a C program to 21-23 19/05/26
implement Boundary Filling
Algorithm using 4
connected pixel.
09. Write a C program to 24-26 26/05/26
implement Boundary Filling
Algorithm using 8
connected pixel.
10. Write a C program to 27-29 02/06/26
implement Flood Filling
Algorithm _4/8 connected
pixel.
11. Write a menu-driven 30-45 16/06/26
program in C to implement
2-D Transformation on
polygon.
ASSIGNMENT-1
(1) Write a C program to Draw a specified figure supplied by
Instructor.
Program:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int gd=DETECT,gm;
initgraph (&gd,&gm,"..\\bgi");
setbkcolor(BLACK);
printf("\t\t\t\n\nLINE");
line(50,40,190,40);
printf("\t\t\n\n\n\nRECTANGLE");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(120,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30);
printf("\t\n\n\n\nECLIPSE");
ellipse(120,350,0,360,30,20);
getch();
closegraph();
return 0;
}
1
Input and Output Section:
2
ASSIGNMENT-2
(2). Write a C program to draw a Polygon design using basic
graphics primitives (line() and circle()).
Program:
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("*****Polygon Design using (line() and circle())*****");
/* Draw a hexagon */
line(200,100,300,100);
line(300,100,350,200);
line(350,200,300,300);
line(300,300,200,300);
line(200,300,150,200);
line(150,200,200,100);
/* Draw circles at vertices */
circle(200,100,20);
circle(300,100,20);
circle(350,200,20);
circle(300,300,20);
circle(200,300,20);
circle(150,200,20);
/* Draw center circle */
3
circle(250,200,40);
getch();
closegraph();
return 0;
}
Input and Output Section:
4
ASSIGNMENT-3
(3). Write a C program to implement DDA Line Drawing
Algorithm.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void DDA(int x0,int y0,int x1,int y1)
{
float Xinc,Yinc,dx,dy,step;
int i;
dx=x1-x0;
dy=y1-y0;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
Xinc=dx/step;
Yinc=dy/step;
for(i=0;i<=step;i++)
{
putpixel(x0,y0,RED);
x0=x0+Xinc;
y0=y0+Yinc;
}
}
int main()
{
int gd=DETECT,gm,x0,y0,x1,y1;
initgraph(&gd,&gm,"..\\BGI");
5
printf("Enter initial point: ");
scanf("%d%d",&x0,&y0);
printf("Enter ending point: ");
scanf("%d%d",&x1,&y1);
DDA(x0,y0,x1,y1);
getch();
closegraph();
return 0;
}
6
Input and Output Section:
7
ASSIGNMENT-4
(4). Write a C program to implement Bresenham’s Line Drawing
Algorithm.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void Bresenham(int x1,int y1,int x2,int y2)
{
int x,y,dx,dy,p;
x=x1;
y=y1;
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;
while(x<=x2)
{
putpixel(x,y,YELLOW);
x++;
if(p<0){
p=p+2*dy;
}
else
{
p=p+2*dy-2*dx;
8
y++;
}
delay(50);
}
}
int main(){
int gd=DETECT, gm, x1, y1, x2, y2;
initgraph(&gd, &gm, "..\\bgi");
printf("Enter the co-ordinates of First point : ");
scanf("%d%d", &x1, &y1);
printf("Enter the co-ordinates of Second point : ");
scanf("%d%d", &x2, &y2);
Bresenham(x1,y1,x2,y2);
getch();
closegraph();
return 0;
}
9
Input and Output Section:
10
ASSIGNMENT-5
(5). Write a C program to implement Midpoint circle generating
Algorithm.
Program:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int x,y,x_mid,y_mid,radius,dp;
int g_mode,g_driver=DETECT;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
printf("\n******** MID POINT CIRCLE ********\n");
printf("\n Enter the Co_ordinates: ");
scanf("%d %d",&x_mid,&y_mid);
printf("\n Enter the Radius : ");
scanf("%d",&radius);
x=0;
y=radius;
dp=1-radius;
do
{
putpixel(x_mid+x,y_mid+y,YELLOW);
putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);
putpixel(x_mid-x,y_mid+y,YELLOW);
11
putpixel(x_mid-x,y_mid-y,YELLOW);
putpixel(x_mid-y,y_mid-x,YELLOW);
putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);
if(dp<0)
{
dp+=(2*x)+1;
}
else
{
y=y-1;
dp+=(2*x)-(2*y)+1;
}
x=x+1;
}
while(y>x);
getch();
return 0;
}
12
Input and Output Section:
13
ASSIGNMENT-6
(6). Write a C program to implement Bresenham’s Circle
generating Algorithm.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int main()
{
int xc,yc,r,p,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\BGI");
clrscr();
printf("\nEnter the Co-Ordinates of Center : ");
scanf("%d %d",&xc,&yc);
printf("\nEnter the Radius: ");
scanf("%d",&r);
x=0;
y=r;
p=3-(2*r);
for(x=0;x<=y;x++)
{
if(p < 0)
{
14
p=p+(4 * x)+6;
}
else
{
y=y-1;
p=p +4 *(x-y)+10;
}
putpixel(xc+x,yc-y,DARKGRAY);
putpixel(xc-x,yc-y,RED);
putpixel(xc+x,yc+y,GREEN);
putpixel(xc-x,yc+y,LIGHTRED);
putpixel(xc+y,yc-x,BROWN);
putpixel(xc-y,yc-x,BLACK);
putpixel(xc+y,yc+x,YELLOW);
putpixel(xc-y,yc+x,MAGENTA);
delay(50);
}
getch();
closegraph();
return 0;
}
15
Input and Output Section:
16
ASSIGNMENT-7
(7). Write a program in C to implement Mid-point Ellipse
generating algorithm.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
int main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
long x,y,xc,yc,asq,bsq,fx,fy,a,b,d,temp1,temp2;
printf("Enter Coordinates x and y : ");
scanf("%ld%ld",&xc,&yc);
printf("Enter Constants a and b : ");
scanf("%ld%ld",&a,&b);
x=0;
y=b;
asq=a*a;
bsq=b*b;
fx=2*bsq*x;
fy=2*asq*y;
17
d=bsq-(asq*b)+(asq*0.25);
do
{
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,15);
if(d<0)
d=d+fx+bsq;
else
{
y=y-1;
d=d+fx-fy+bsq;
fy=fy-(2*asq);
}
x=x+1;
fx=fx+(2*bsq);
}
while(fx<fy);
temp1=(x+0.5)*(x+0.5);
temp2=(y-1)*(y-1);
d=bsq*temp1+asq*temp2-(asq*bsq);
do
{
putpixel(xc+x,yc+y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+x,yc-y,15);
18
putpixel(xc-x,yc+y,15);
if(d>=0)
d=d-fy+asq;
else
{
x=x+1;
d=d+fx-fy+asq;
fx=fx+(2*bsq);
}
y=y-1;
fy=fy-(2*asq);
}while(y>0);
getch();
closegraph();
return 0;
}
19
Input and Output Section:
20
ASSIGNMENT-8
(8). Write a C program to implement Boundary Filling Algorithm
using 4 connected pixel.
Program:
//(8).Boundary Filling 4 connected Pixel:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void bf(int x, int y, int fc, int bc)
{
int c;
c=getpixel(x,y);
if(c!=bc && c!=fc)
{
putpixel(x,y,fc);
bf(x+1,y,fc,bc);
bf(x-1,y,fc,bc);
bf(x,y+1,fc,bc);
bf(x,y-1,fc,bc);
}
}
int main()
{
int gd=0,gm;
initgraph(&gd,&gm,"..\\bgi");
21
setcolor(4);
rectangle(50,50,100,100);
rectangle(100,50,125,100);
rectangle(125,50,175,100);
bf(55,55,14,4);
bf(120,60,15,4);
bf(130,65,5,4);
getch();
closegraph();
return 0;
}
22
Input and Output Section:
23
ASSIGNMENT-9
(9). Write a C program to implement Boundary Filling Algorithm
using 8 connected pixel.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void boundaryFill8(int x, int y, int f_color,int b_color)
{
int current=getpixel(x,y);
if(current!= b_color && current != f_color)
{
putpixel(x, y, f_color);
boundaryFill8(x + 1, y, f_color, b_color);
boundaryFill8(x, y + 1, f_color, b_color);
boundaryFill8(x - 1, y, f_color, b_color);
boundaryFill8(x, y - 1, f_color, b_color);
boundaryFill8(x - 1, y - 1, f_color, b_color);
boundaryFill8(x - 1, y + 1, f_color, b_color);
boundaryFill8(x + 1, y - 1, f_color, b_color);
boundaryFill8(x + 1, y + 1, f_color, b_color);
}
}
int main()
24
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "..\\bgi");
// Rectangle function
rectangle(50, 50, 75, 100);
rectangle(75,50,100,100);
rectangle(100,50,125,100);
rectangle(125,50,150,100);
// Function calling
boundaryFill8(60, 60, 1,15);
boundaryFill8(80, 80, 2,15);
boundaryFill8(110, 90, 4,15);
boundaryFill8(130, 60, 6,15);
getch();
closegraph();
return 0;
}
25
Input and Output Section:
26
ASSIGNMENT-10
(10). Write a C program to implement Flood Filling Algorithm _4/8
connected pixel.
Program:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
void floodfill8(int x, int y, int oldcolor, int newcolor)
{
if(getpixel(x, y) == oldcolor)
{
putpixel(x, y, newcolor);
/* 8-connected neighbors */
floodfill8(x+1, y, oldcolor, newcolor);
floodfill8(x-1, y, oldcolor, newcolor);
floodfill8(x, y+1, oldcolor, newcolor);
floodfill8(x, y-1, oldcolor, newcolor);
floodfill8(x+1, y+1, oldcolor, newcolor);
floodfill8(x-1, y+1, oldcolor, newcolor);
floodfill8(x+1, y-1, oldcolor, newcolor);
floodfill8(x-1, y-1, oldcolor, newcolor);
}
}
27
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Draw a closed figure */
setcolor(WHITE);
rectangle(150, 100, 350, 250);
/* Fill the inside region */
floodfill8(200, 150, BLACK, RED);
outtextxy(10, 10, "Flood Fill Algorithm (8-Connected)");
getch();
closegraph();
return 0;
}
28
Input and Output Section:
29
ASSIGNMENT-11
(11). Write a menu-driven program in C to implement 2-D
Transformation on polygon.
Program:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159
int x[20], y[20], X[20], Y[20];
int a, b, n;
/* function declarations */
void output(int n);
void translation(int tx,int ty);
void scalling_origin(int sx,int sy);
void scalling_arbitary(int sx,int sy);
void rotation_origin(float theta);
void rotation_arbitary(float theta);
void reflection_x_axis(int n);
void reflection_y_axis(int n);
void reflection_origin(int n);
void reflection_yETngtvx(int n);
void reflection_yETpstvx(int n);
void shearing_x_axis(int shx);
30
void shearing_y_axis(int shy);
int main(){
int gd = DETECT, gm;
int i, j, ch = 0;
int tx, ty, sx, sy, shx, shy;
float theta;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("****** 2D TRANSFORMATION MENU ******\n");
printf("Enter number of coordinates: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
printf("Enter point %d: ", i);
scanf("%d %d", &x[i], &y[i]);
}
do
{
cleardevice();
a = getmaxx() / 2;
b = getmaxy() / 2;
/* axes */
setcolor(WHITE);
line(a, 0, a, 2*b);
line(0, b, 2*a, b);
/* original shape */
setcolor(GREEN);
for(i = 1; i <= n; i++)
31
{
j = i + 1;
if(i < n)
line(a + x[i], b - y[i], a + x[j], b - y[j]);
else
line(a + x[i], b - y[i], a + x[1], b - y[1]);
}
printf("\n\n===== MENU =====");
printf("\n1. Translation");
printf("\n2. Scaling (Origin)");
printf("\n3. Scaling (Arbitrary)");
printf("\n4. Rotation (Origin)");
printf("\n5. Rotation (Arbitrary)");
printf("\n6. Reflection X-axis");
printf("\n7. Reflection Y-axis");
printf("\n8. Reflection Origin");
printf("\n9. Reflection y=x");
printf("\n10. Reflection y=-x");
printf("\n11. Shearing X-axis");
printf("\n12. Shearing Y-axis");
printf("\n13. Exit");
printf("\nEnter choice: ");
fflush(stdin);
scanf("%d", &ch);
switch(ch){
case 1:
printf("Enter tx ty: ");
32
scanf("%d%d", &tx, &ty);
translation(tx, ty);
output(n);
getch();
break;
case 2:
printf("Enter sx sy: ");
scanf("%d%d", &sx, &sy);
scalling_origin(sx, sy);
output(n);
getch();
break;
case 3:
printf("Enter sx sy: ");
scanf("%d%d", &sx, &sy);
scalling_arbitary(sx, sy);
output(n);
getch();
break;
case 4:
printf("Enter angle: ");
scanf("%f", &theta);
rotation_origin(theta);
output(n);
33
getch();
break;
case 5:
printf("Enter angle: ");
scanf("%f", &theta);
rotation_arbitary(theta);
output(n);
getch();
break;
case 6:
reflection_x_axis(n);
output(n);
getch();
break;
case 7:
reflection_y_axis(n);
output(n);
getch();
break;
case 8:
reflection_origin(n);
output(n);
getch();
34
break;
case 9:
reflection_yETpstvx(n);
output(n);
getch();
break;
case 10:
reflection_yETngtvx(n);
output(n);
getch();
break;
case 11:
printf("Enter shx: ");
scanf("%d", &shx);
shearing_x_axis(shx);
output(n);
getch();
break;
case 12:
printf("Enter shy: ");
scanf("%d", ­);
shearing_y_axis(shy);
output(n);
35
getch();
break;
}
} while(ch != 13);
closegraph();
return 0;
}
/* -------- OUTPUT -------- */
void output(int n)
{
int i, j;
setcolor(RED);
for(i = 1; i <= n; i++) {
j = i + 1;
if(i < n)
line(a + X[i], b - Y[i], a + X[j], b - Y[j]);
else
line(a + X[i], b - Y[i], a + X[1], b - Y[1]);
}
}
/* -------- TRANSFORMATIONS -------- */
void translation(int tx,int ty){
int i;
for(i = 1; i <= n; i++)
{
X[i] = x[i] + tx;
36
Y[i] = y[i] + ty;
}
}
void scalling_origin(int sx,int sy){
int i;
for(i = 1; i <= n; i++)
{
X[i] = x[i] * sx;
Y[i] = y[i] * sy;
}
}
void scalling_arbitary(int sx,int sy)
{
int i, xf, yf;
printf("Enter fixed point: ");
scanf("%d %d", &xf, &yf);
for(i = 1; i <= n; i++)
{
X[i] = xf + (x[i] - xf) * sx;
Y[i] = yf + (y[i] - yf) * sy;
}
}
void rotation_origin(float theta)
{
int i;
float rad = theta * PI / 180;
37
for(i = 1; i <= n; i++)
{
X[i] = x[i]*cos(rad) - y[i]*sin(rad);
Y[i] = x[i]*sin(rad) + y[i]*cos(rad);
}
}
void rotation_arbitary(float theta){
int i, xf, yf;
float rad = theta * PI / 180;
printf("Enter fixed point: ");
scanf("%d %d", &xf, &yf);
for(i = 1; i <= n; i++)
{
X[i] = xf + (x[i]-xf)*cos(rad) - (y[i]-yf)*sin(rad);
Y[i] = yf + (x[i]-xf)*sin(rad) + (y[i]-yf)*cos(rad);
}
}
void reflection_x_axis(int n)
{
int i;
for(i = 1; i <= n; i++)
{
X[i] = x[i];
Y[i] = -y[i];
}
}
38
void reflection_y_axis(int n){
int i;
for(i = 1; i <= n; i++)
{
X[i] = -x[i];
Y[i] = y[i];
}
}
void reflection_origin(int n){
int i;
for(i = 1; i <= n; i++)
{
X[i] = -x[i];
Y[i] = -y[i];
}
}
void reflection_yETngtvx(int n)
{
int i;
for(i = 1; i <= n; i++)
{
X[i] = -y[i];
Y[i] = -x[i];
}
}
void reflection_yETpstvx(int n)
{
39
int i;
for(i = 1; i <= n; i++)
{
X[i] = y[i];
Y[i] = x[i];
}
}
void shearing_x_axis(int shx){
int i;
for(i = 1; i <= n; i++)
{
X[i] = x[i] + y[i]*shx;
Y[i] = y[i];
}
}
void shearing_y_axis(int shy){
int i;
for(i = 1; i <= n; i++){
X[i] = x[i];
Y[i] = x[i]*shy + y[i];
}
}
40
Input and Output Section:
(a) Translation :
(b) Scaling :
i. Scaling About Origin :-
41
ii. Scaling About Arbitrary :-
(c) Rotation :
i. Rotation About Origin :-
ii.
iii. Rotation About Arbitrary :-
i. Rotation About Arbitrary :-
42
(d) Reflection :
i. Reflection About X-Axis :-
ii. Reflection About Y-Axis :-
iii. Reflection About Origin :-
43
iv. Reflection About y = x
v. Reflection About y = -x
44
(e) Shearing :
i. Shearing Along X-Axis :-
ii. Shearing Along Y-Axis :-
(f) Exit :
45