SlideShare a Scribd company logo
Computer graphiCs
Practical file
AMITY UNIVERSITY
HARYANA
Submitted by : MANOJ KUMAR
B.tech(cse)
Sem-4
LIST OF CONTENTS
S.N
O
Programs pag
e
no
date T.sign
1 Write a program to Draw Graphics
Objects
3
2 Write a program to draw 2D bar 5
3 Write a program to draw Pattern
fill
7
4 Write a program to draw PIE Chart 8
5 Write a program for Boundary Fill 9
6 Write a program for Flood fill 11
7 Write a program for Translatation
of object
13
8 Write a program for Scaling of
object
15
9 Write a program for Rotation of
object
17
10 Write a program for Window to
viewport transformation
19
11 Write a program for Shearing of
object
23
2
1)Write a program to Draw Graphics Objects.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
void bfill4(int x,int y)
{int c,f=2,b=0;
c=getpixel(x,y);
if((c!=b)&&(c!=f))
{
putpixel(x,y,f);
delay(1);
bfill4(x+1,y);
bfill4(x-1,y);
bfill4(x,y+1);
bfill4(x,y-1);
}
}
void bfill4(int,int);
void main()
{
int gd=DETECT,gm;
int i=0,m=0;
initgraph(&gd,&gm,"..bgi");
while(i<500)
{
3
cleardevice();
i++;
line(80+i,300,90+i,270);
line(90+i,270,120+i,270);
line(120+i,270,160+i,240);
line(160+i,240,230+i,240);
line(230+i,240,275+i,270);
line(275+i,270,310+i,270);
line(310+i,270,335+i,290);
line(335+i,290,335+i,300);
line(255+i,300,335+i,300);
line(180+i,300,210+i,300);
line(80+i,300,135+i,300);
circle(232+i,300,18);
circle(157+i,300,18);
bfill4(232+i,300);
delay(5);
}
getch();
closegraph();
}
4
2)Write a program to draw 2D bar.
#include<graphics.h>
#include<conio.h>
Void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
setcolor(YELLOW);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"Bar Chart");
setlinestyle(SOLID_LINE,0,2);
line(100,420,100,60);
line(100,420,600,420);
line(90,70,100,60);
line(110,70,100,60);
line(590,410,600,420);
line(590,430,600,420);
outtextxy(95,35,"Y");
outtextxy(610,405,"X");
outtextxy(85,415,"O");
setfillstyle(LINE_FILL,BLUE);
bar(150,100,200,419);
setfillstyle(XHATCH_FILL,RED);
bar(225,150,275,419);
setfillstyle(WIDE_DOT_FILL,GREEN);
5
bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL,MAGENTA);
bar(375,125,425,419);
setfillstyle(HATCH_FILL,BROWN);
bar(450,175,500,419);
getch();
}
6
3)Write a program for Pattern fill.
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;
initgraph(&gdriver, &gmode, "c:tcbgi");
errorcode = graphresult();
if (errorcode != grOk
{
printf("Graphics error: %sn", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
for (i=0; i<12; i++)
{
setfillstyle(i, RED);
bar(midx-50, midy-50, midx+50,midy+50);
rectangle(midx-50,midy-50,midx+50,midy+50);
}
7
getch();
}
4)Write a program to draw PIE Chart.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, midx, midy;
initgraph(&gd, &gm, "C:TCBGI");
setcolor(MAGENTA);
rectangle(0,40,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,10,"Pie Chart");
midx = getmaxx()/2;
midy = getmaxy()/2;
setfillstyle(LINE_FILL,BLUE);
pieslice(midx, midy, 0, 75, 100);
outtextxy(midx+100, midy - 75, "20.83%");
setfillstyle(XHATCH_FILL,RED);
pieslice(midx, midy, 75, 225, 100);
outtextxy(midx-175, midy - 75, "41.67%");
8
setfillstyle(WIDE_DOT_FILL,GREEN);
pieslice(midx, midy, 225, 360, 100);
outtextxy(midx+75, midy + 75, "37.50%");
getch();
return 0;
}
9
5)Write a program for Boundary Fill of polygons.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void bfill4(int x,int y,int f,int b)
{int c;
c=getpixel(x,y);
if((c!=b)&&(c!=f))
{
putpixel(x,y,f);
delay(1);
bfill4(x+1,y,f,b);
bfill4(x-1,y,f,b);
bfill4(x,y+1,f,b);
bfill4(x,y-1,f,b);
}
}
void bfill4(int,int,int,int);
void bfill8(int x,int y,int f,int b)
{
int d;
d=getpixel(x,y);
if((d!=b)&&(d!=f))
{
putpixel(x,y,f);
10
delay(1);
bfill8(x-1,y+1,f,b);
bfill8(x+1,y,f,b);
bfill8(x-1,y,f,b);
bfill8(x+1,y+1,f,b);
bfill8(x-1,y-1,f,b);
bfill8(x+1,y-1,f,b);
bfill8(x,y+1,f,b);
bfill8(x,y-1,f,b);
}
}
void bfill8(int,int,int,int);
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:turboc3bgi");
settextstyle(2,0,6);
outtextxy(5,20,"4-connected b fill");
rectangle(10,50,70,100);
bfill4(55,55,4,15);
outtextxy(205,20,"8-connected b fill");
rectangle(220,50,280,100);
bfill8(255,55,2,15);
getch();
}
11
6) Write a program for flood Fill of polygons.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood_fill(int x, int y, int ncolor, int ocolor)
{
if (getpixel(x, y) == ocolor)
{ putpixel(x, y, ncolor);
delay(0);
flood_fill(x + 1, y, ncolor, ocolor);
flood_fill(x, y - 1, ncolor, ocolor);
flood_fill(x, y + 1, ncolor, ocolor);
flood_fill(x - 1, y, ncolor, ocolor);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:turboc3bgi");
int ncolor, ocolor;
printf("Enter old color : ");
scanf("%d", &ocolor);
printf("Enter new color : ");
scanf("%d", &ncolor);
12
setcolor(RED);
line(550,100,600,100);
setcolor(GREEN);
line(550,100,550,150);
setcolor(BLUE);
line(600,100,600,150);
setcolor(WHITE);
line(550,150,600,150);
flood_fill(560,130, ncolor, ocolor);
getch();
}
13
7)Write a program for Translatation of objects.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
void draw(int,int,int,int,int,int,int,int);
void translation(int [][3],int[][3]);
void main()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"c:turboc3bgi");
int x1,y1,x2,y2,x3,y3,x4,y4;
printf("entre four coordinate");
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
draw(x1,y1,x2,y2,x3,y3,x4,y4);
int obj[5][3]={
{x1,y1,1},
{x2,y2,1},
{x3,y3,1},
{x4,y4,1},
{x1,y1,1}
};
int tx,ty;
printf(" enter translation factor");
scanf("%d%d",&ty,&tx);
int trans[3][3]={ {1,0,0},
{0,1,0},
{-tx,-ty,1},
14
};
translation(obj,trans);
getch();
}
void draw(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
line(x4,y4,x1,y1);
}
void translation(int obj[][3],int trans[][3])
{
int c[5][3];
for(int i=0;i<=4;i++)
{ for(int j=0;j<=2;j++)
{
c[i][j]=0;
for(int k=0;k<=2;k++)
{
c[i][j]=c[i][j]+obj[i][k]*trans[k][j];
}
}
}
draw(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1],c[3][0],c[3][1]);
}
15
16
8) Write a program for scaling of objects.
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void draw(int,int,int,int,int,int);
void scaling(int[][2],int[][2]);
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2,x3,y3,sx,sy;
initgraph(&gd,&gm,"..bgi");
cout<<"enter coordinate for first vertex of triangle:";
cin>>x1>>y1;
cout<<"enter for second vertex:";
cin>>x2>>y2;
cout<<"enter for third :";
cin>>x3>>y3;
draw(x1,y1,x2,y2,x3,y3);
int object[4][2]={ {x1,y1},
{x2,y2},
{x3,y3},
{x1,y1}
};
cout<<"n enter x scaling:";
cin>>sx;
cout<<"n y scaling";
cin>>sy;
17
int scale[2][2]={ {sx,0},
{0,sy}
};
scaling(object,scale);
getch();
closegraph();
}
void draw(int x1,int y1,int x2,int y2,int x3,int y3)
{
cout<<x1<<y1<<x2<<y2<<x3<<y3;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scaling(int object[][2],int scale[][2])
{ int c[4][2];
for(int i=0;i<=3;i++)
{
for(int j=0;j<=1;j++)
{ c[i][j]=0;
for(int k=0;k<=1;k++)
{
c[i][j]=c[i][j]+object[i][k]*scale[k][j];
}
}
}
draw(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1]);
}
18
19
9) write a program for rotation of objects
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>
#include<stdio.h>
void draw(float [][2]);
void rotation(float[][2],float[][2]);
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2,x3,y3;
float theta,a1,a2,a3;
initgraph(&gd,&gm,"c:turboc3bgi");
cout<<"enter coordinate for first vertex of triangle:";
cin>>x1>>y1;
cout<<"enter for second vertex:";
cin>>x2>>y2;
cout<<"enter for third :";
cin>>x3>>y3;
float object[3][2]={ {x1,y1},
{x2,y2},
{x3,y3}
};
draw(object);
cout<<"n enter rotation angle:";
cin>>theta;
20
theta=theta*(3.14/180);
a1=cos(theta);
a2=-sin(theta);
a3=sin(theta);
float rotate[2][2]={
{a1,a2},
{a3,a1},
};
rotation(object,rotate);
getch();
closegraph();
}
void draw(float m[][2])
{
line(m[0][0],m[0][1],m[1][0],m[1][1]);
line(m[1][0],m[1][1],m[2][0],m[2][1]);
line(m[2][0],m[2][1],m[0][0],m[0][1]);
}
void rotation(float object[][2],float rotate[][2])
{ float c[3][2];
for(int i=0;i<=2;i++)
{
for(int j=0;j<=1;j++)
{
c[i][j]=0;
for(int k=0;k<=1;k++)
{
c[i][j]=c[i][j]+object[i][k]*rotate[k][j];
21
}
}
}
draw(c);
}
10) Write a program for window to viewport
transformation
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>
void draw(int ,int ,int ,int,int,int,int,int);
void translation2(int ,int ,int ,int,int,int,int,int,int,int);
void transaction(int,int,int,int,int,int,int,int);
void scaling(int,int,int ,int ,int,int,int,int,int,int,int,int);
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"c:turboc3BGI");
int x1,y1,x2,y2,x3,y3,x4,y4;
printf("enter the coordinates of window");
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
draw(x1,y1,x2,y2,x3,y3,x4,y4);
transaction(x1,y1,x2,y2,x3,y3,x4,y4);
getch();
22
}
void draw(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
line(x4,y4,x1,y1);
}
void transaction(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
int object[][3]={
{x1,y1,1},
{x2,y2,1},
{x3,y3,1},
{x4,y4,1}
} ;
int tx,ty,sx,sy;
printf("enter the transaction factor");
scanf("%d%d",&tx,&ty);
int trans[][3]={
{1,0,0},
{0,1,0},
{-tx,-ty,1}
};
int c[4][3],i,j,k;
for(i=0;i<=3;i++)
{
for(j=0;j<=2;j++)
23
{
c[i][j]=0;
for(k=0;k<=2;k++)
{
c[i][j]=c[i][j]+object[i][k]*trans[k][j];
}
}
}
draw(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1],c[3][0],c[3][1]);
scaling(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1],c[3][0],c[3][1],x1,y1,x4,y2);
}
void scaling(int x5,int y5,int x6,int y6,int x7,int y7,int x8,int y8,int x1,int y1,int x4,int y2)
{
int newobj[][3]={
{x5,y5,1},
{x6,y6,1},
{x7,y7,1},
{x8,y8,1}
};
int sx,sy,x9,y9,x10,y10,x11,y11,x12,y12;
printf("enter the coordinates of view port");
scanf("%d%d%d%d%d%d%d%d",&x9,&y9,&x10,&y10,&x11,&y11,&x12,&y12);
draw(x9,y9,x10,y10,x11,y11,x12,y12);
sx=((y10-y9)/(y2-y1));
sy=((x12-x9)/(x4-x1));
int scale[][3]={
{sx,0,0},
24
{0,sy,0},
{0,0,1}
};
int r[4][3],i,j,k;
for(i=0;i<=3;i++)
{
for(j=0;j<=2;j++)
{ r[i][j]=0;
for(k=0;k<=2;k++)
{
r[i][j]=r[i][j]+newobj[i][k]*scale[k][j];
}
}
}
draw(r[0][0],r[0][1],r[1][0],r[1][1],r[2][0],r[2][1],r[3][0],r[3][1]);
translation2(r[0][0],r[0][1],r[1][0],r[1][1],r[2][0],r[2][1],r[3][0],r[3][1],x9,y9);
}
void translation2(int x5,int y5,int x6,int y6,int x7,int y7,int x8,int y8,int x9,int y9)
{
int object[][3]={
{x5,y5,1},
{x6,y6,1},
{x7,y7,1},
{x8,y8,1}
} ;
int tx,ty,sx,sy;
tx=(x9);
ty=(y9);
25
int trans[][3]={ {1,0,0},
{0,1,0},
{tx,ty,1}
};
int c[4][3],i,j,k;
for(i=0;i<=3;i++)
{
for(j=0;j<=2;j++)
{ c[i][j]=0;
for(k=0;k<=2;k++)
{
c[i][j]=c[i][j]+object[i][k]*trans[k][j];
}
}
}
draw(c[0][0],c[0][1],c[1][0],c[1][1],c[2][0],c[2][1],c[3][0],c[3][1]);
}
26

More Related Content

DOCX
Computer graphics programs in c++
DOCX
Wap in c to draw a line using DDA algorithm
DOCX
Computer Graphics Lab File C Programs
DOCX
Cg my own programs
DOCX
Computer graphics lab assignment
DOCX
Graphics point clipping c program
PDF
Computer graphics lab manual
DOC
SE Computer, Programming Laboratory(210251) University of Pune
Computer graphics programs in c++
Wap in c to draw a line using DDA algorithm
Computer Graphics Lab File C Programs
Cg my own programs
Computer graphics lab assignment
Graphics point clipping c program
Computer graphics lab manual
SE Computer, Programming Laboratory(210251) University of Pune

What's hot (19)

PDF
Basics of Computer graphics lab
DOC
COMPUTER GRAPHICS LAB MANUAL
PDF
Computer graphics lab report with code in cpp
DOCX
DAA Lab File C Programs
PDF
Computer Graphics Lab
DOCX
Computer graphics File for Engineers
DOCX
Advance java
DOCX
Os lab file c programs
DOC
Computer graphics
PDF
Cn os-lp lab manual k.roshan
DOCX
Numerical Method Assignment
DOCX
Solutionsfor co2 C Programs for data structures
DOCX
PDF
Matlab assignment
DOCX
Caropro
PDF
design and analysis of algorithm Lab files
Basics of Computer graphics lab
COMPUTER GRAPHICS LAB MANUAL
Computer graphics lab report with code in cpp
DAA Lab File C Programs
Computer Graphics Lab
Computer graphics File for Engineers
Advance java
Os lab file c programs
Computer graphics
Cn os-lp lab manual k.roshan
Numerical Method Assignment
Solutionsfor co2 C Programs for data structures
Matlab assignment
Caropro
design and analysis of algorithm Lab files
Ad

Similar to computer graphics practicals (20)

DOC
'C' language notes (a.p)
DOC
Computer graphics
PDF
Revision1schema C programming
DOCX
Graphic Design Lab File.docx
DOC
prg5,6,.doc computer science and engineering
PPT
Struct examples
PDF
C Programming lab
PDF
Pattern printing-in-c(Jaydip Kikani)
DOCX
Graphics practical lab manual
PDF
Assignment on Numerical Method C Code
PDF
C Programming Example
PDF
C programs Set 2
DOC
C basics
DOCX
C graphics programs file
PDF
Numerical analysis
DOCX
Computer science project work on C++
PDF
Data Structure in C Programming Language
PDF
pattern-printing-in-c.pdf
'C' language notes (a.p)
Computer graphics
Revision1schema C programming
Graphic Design Lab File.docx
prg5,6,.doc computer science and engineering
Struct examples
C Programming lab
Pattern printing-in-c(Jaydip Kikani)
Graphics practical lab manual
Assignment on Numerical Method C Code
C Programming Example
C programs Set 2
C basics
C graphics programs file
Numerical analysis
Computer science project work on C++
Data Structure in C Programming Language
pattern-printing-in-c.pdf
Ad

Recently uploaded (20)

PPTX
Artificial Intelligence
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Well-logging-methods_new................
DOCX
573137875-Attendance-Management-System-original
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Sustainable Sites - Green Building Construction
Artificial Intelligence
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Fundamentals of Mechanical Engineering.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Foundation to blockchain - A guide to Blockchain Tech
Well-logging-methods_new................
573137875-Attendance-Management-System-original
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Construction Project Organization Group 2.pptx
Safety Seminar civil to be ensured for safe working.
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Categorization of Factors Affecting Classification Algorithms Selection
CYBER-CRIMES AND SECURITY A guide to understanding
Sustainable Sites - Green Building Construction

computer graphics practicals