NAME-SHIVAM MISHRA
CLASS ROLL NO.-IIT/ITESM/LTE/2020/501
BTE ROLL NO.- 2012112002
TEACHER- Divya Mam
S/N TOPICS DATE
Introduction of Computer Graphics. 02-08-21
1
To draw a bar & circle using computer graphics in c. 09-08-21
2
To draw a house using computer graphics in c. 09-08-21
3
Write a C program for DDA Line Drawing Algorithm. 16-08-21
4
Write a C program for Bresenham Line Drawing Algorithm. 23-08-21
5
Write a C program for Bresenham Circle Drawing Algorithm 30-08-21
6
Write C program of 2D Transformation translation 04-09-21
7
Rotation of a triangle by C graphics in 2D transformations. 11-09-21
8
2D Scaling program in C. 18-09-21
9
Write a program of a moving car in C language. 25-09-21
10
PRACTICAL – 01
1.1 (AIM)- Introduction to computer graphics
1.2 - Definition of computer graphics
1.3 - Why computer graphics are used
1.4 - Types of computer graphics
1.5 - Application of computer graphics
1.1 Aim: - Introduction to computer
graphics
Computer Graphics involves technology to access. The
Process transforms and presents information in a visual
form. The role of computer graphics insensible. In today
life, computer graphics has now become a common
element in user interfaces, T.V. commercial motion
pictures.
In computer graphics, two or three-dimensional pictures
can be created that are used for research. Many hardware
devices algorithm has been developing for improving the
speed of picture generation with the passes of time. It
includes the creation storage of models and image of
objects. These models for various fields like engineering,
mathematical and so on.
1.2: -Definition of computer graphics
It is the use of computers to create and manipulate
pictures on a display device. It comprises of software
techniques to create, store, modify, represents pictures.
1.3: - Why computer graphics are
used
Suppose a clothes manufacturing company want to show
the sale of their clothes for five years. For this vast amount
of information is to store. So a lot of time and memory will
be needed. This method will be tough to understand by a
common man. In this situation graphics is a better
alternative. Graphics tools are charts and graphs. Using
graphs, data can be represented in pictorial form. A
picture can be understood easily just with a single look.
Interactive computer graphics work using the concept of
two-way communication between computer users. The
computer will receive signals from the input device, and
the picture is modified accordingly. Picture will be changed
quickly when we apply command.
1.4: - Types of computer graphics
• Raster Graphics: In raster graphics pixels are used for
an image to be drawn. It is also known as a bitmap
image in which a sequence of image is into smaller
pixels. Basically a bitmap indicates a large number of
pixels together.
• Vector Graphics: In vector graphics, mathematical
formulae are used to draw different types of shapes,
lines, objects and so on.
1.5: - Application of computer
graphics
1. Education and Training: Computer-generated model
of the physical, financial and economic system is often
used as educational aids. Model of physical systems,
physiological system, population trends or equipment can
help trainees to understand the operation of the system.
For some training applications, particular systems are
designed. For example, Flight Simulator.
Flight Simulator: It helps in giving training to the pilots of
airplanes. These pilots spend much of their training not in
a real aircraft but on the ground at the controls of a Flight
Simulator.
3. Use in Biology: Molecular biologist can display a
picture of molecules and gain insight into their structure
with the help of computer graphics.
4. Computer-Generated Maps: Town planners and
transportation engineers can use computer-generated
maps which display data useful to them in their planning
work.
5. Architect: Architect can explore an alternative solution
to design problems at an interactive graphics terminal. In
this way, they can test many more solutions that would not
be possible without the computer.
PRACTICAL – 02
AIM:- To draw a bar & circle using computer graphics in c.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd= DETECT ,gm;
initgraph(&gd,&gm,(char*)"");
bar(100,100,200,400);
circle(400,250,50);
getch();
closegraph;
return 0;
}
OUTPUT:-
PRACTICAL – 03
AIM:- To draw a house using computer graphics in c.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd= DETECT,gm;
initgraph(&gd,&gm,(char*)"");
line (150 ,50,200 ,100);
line (100, 100 ,150 ,50);
line(150,50,450,50);
line(450,50,500,100);
line(100,100,500,100);
rectangle(100,100,200,200);
rectangle(200 ,100 ,500 ,200);
line(500,200,500,100);
rectangle(130,130,170,200);
rectangle(250,130,450,180);
setfillstyle(2,3);
floodfill(131,131,WHITE);
floodfill(201,101,WHITE);
setfillstyle(11,7);
floodfill(101,101,WHITE);
floodfill(150,52,WHITE);
floodfill(163,55,WHITE);
floodfill(251,151,WHITE);
getch();
closegraph();
}
OUTPUT:-
PRACTICAL – 04
AIM:- Write a C program for DDA Line Drawing Algorithm.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,xn,yn,dx,dy,i,step;
initgraph(&gd,&gm,(char*)"");
printf("enter starting coordinates: ");
scanf("%d%d",&x1,&y1);
printf("enter the end coordinates: ");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if (abs(dx)>abs(dy))
{
step=abs(dx);
}
else{
step=abs(dy);
}
xn=dx/step;
yn=dy/step;
for (i=1;i<=step;i++){
putpixel(x1,y1,LIGHTBLUE);
delay(100);
x1=x1+xn;
y1=y1+yn;
}
getch();
closegraph();
}
OUTPUT:-
PRACTICAL – 05
AIM:- Write a C program for Bresenham Line Drawing Algorithm.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int x,y,x1,y1,x2,y2,dx,dy,p,i;
int gd=DETECT,gm;
printf("enter first point: ");
scanf("%d%d",&x1,&y1);
printf("enter the last point: ");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,(char*)"");
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
x=x1;
y=y1;
i=0;
while(i<=dx)
{
putpixel(x,y,WHITE);
if(p<0)
{
x=x+1;
p=p*dy;
}
else
{
x=x+1;
y=y+1;
p=p+2*dy-2*dx;
}
i++;
}
getch();
closegraph();
}
OUTPUT:-
PRACTICAL – 06
AIM:- Write a C program for Bresenham Circle Drawing Algorithm.
CODE:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd= DETECT, gm;
int x,y,xc,yc,d,r;
printf("enter the center of the circle: ");
scanf("%d%d",&xc,&yc);
printf("enter the radius of the circle: ");
scanf("%d",&r);
initgraph(&gd,&gm,(char*)"");
d=3-2*r;
x=0;
y=r;
while (x<=y)
{
putpixel(x+xc,y+yc,GREEN);
putpixel(x+xc,-y+yc,GREEN);
putpixel(-x+xc,-y+yc,GREEN);
putpixel(-x+xc,y+yc,GREEN);
putpixel(y+xc,x+yc,GREEN);
putpixel(y+xc,-x+yc,GREEN);
putpixel(-y+xc,-x+yc,GREEN);
putpixel(-y+xc,x+yc,GREEN);
if(d<0)
{
x=x+1;
d=d+4*x+6;
}
else
{
x=x+1;
y=y-1;
d=d+4*(x-y);
}
}
getch();
closegraph();
return 0;
}
OUTPUT:-
PRACTICAL – 07
AIM:- Write C program of 2D Transformation translation
CODE:-
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
// global variable, function defined
int gd = DETECT, gm = DETECT;
void draw(int x[], int y[], int n);
void translate(int *x, int *y, int tx, int ty, int *n);
int main()
{
system("CLS");
int n, xs[100], ys[100], ty, tx;
printf("(0 <= n > 100)\n");
printf("Enter number of sides of polygon (n): ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter coordinate( x%d, y%d ): ", i, i);
scanf("%d%d", &xs[i], &ys[i]);
}
printf("Enter distance for translation respact of x and y directions (tx,
ty): ");
scanf("%d%d", &tx, &ty);
// make graph
initgraph(&gd, &gm, (char *)"");
// Drawing original polygon
setcolor(RED);
draw(xs, ys, n);
// dowing translating polygon
translate(xs, ys, tx, ty, &n);
setcolor(BLUE);
draw(xs, ys, n);
// print others
printf("Original Polygon Color is: RED\nTranslating Polygon Color is:
BLUE\n");
_getch();
closegraph();
return 0;
}
// function definations
void draw(int x[], int y[], int n)
{
for (int i = 0; i < n; i++)
{
line(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n]);
}
}
void translate(int *x, int *y, int tx, int ty, int *n)
{
for (int i = 0; i < *n; i++)
{
x[i] += tx;
y[i] += ty;
}
}
OUTPUT:-
PRACTICAL – 08
AIM:- Rotation of a triangle by C graphics in 2D transformations.
CODE:-
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int main(){
int gd= DETECT , gm;
int x1,x2,x3,y1,y2,y3;
double s,c,a;
initgraph(&gd,&gm,(char*)"");
printf("Rotation of triangle\n ");
printf("enter coordinate of a: ");
scanf("%d%d" ,&x1,&y1);
printf("enter coordinate of b: ");
scanf("%d%d" ,&x2,&y2);
printf("enter coordinate of c: ");
scanf("%d%d" ,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("enter the angel of triangle which want to rotate");
scanf("%if" , &a);
c= cos(a*3.14/180);
s= sin(a*3.14/180);
x1= floor(x1*c+y1*s);
y1= floor (-x1*c+y1*c);
x2= floor(x2*c+y2*s);
y2= floor (-x2*c+y2*c);
x3= floor(x3*c+y3*s);
y3= floor (-x3*c+y3*c);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
closegraph();
}
OUTPUT:-
PRACTICAL – 09
AIM:- 2D Scaling program in C.
CODE:-
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int gd= DETECT , gm;
int n,x[100],y[100],i;
float sfx,sfy;
void draw();
void scale();
int main()
{
printf("enter the numbers of sides : ");
scanf("%d",&n);
printf("enter the co-ordinates : x,y for each point");
for(i=0;i<n;i++)
scanf("%d%d",&x[i],&y[i]);
printf("enter the scale factor:(sfx ,sfy) ");
scanf("%f%f" ,&sfx ,&sfy);
initgraph(&gd,&gm,(char*)"");
cleardevice();
setcolor(CYAN);
draw();// original
scale();// scaling
setcolor(YELLOW);
draw();//after scaling
getch();
}
void draw()
{
for(i=0;i<n;i++)
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
}
void scale()
{
for(i=0;i<n;i++)
{
x[i]= x[0]+(int)((float)(x[i]-x[0])*sfx);
y[i]= y[0]+(int)((float)(y[i]-y[0])*sfy);
}
OUTPUT:-
PRACTICAL – 10
AIM:- Write a program of a moving car in C language.
CODE:-
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <time.h>
void floodfill(int x, int y, int oldcolor, int newcolor)
{
if (getpixel(x, y) == oldcolor)
{
putpixel(x, y, newcolor);
floodfill(x + 1, y, oldcolor, newcolor);
floodfill(x, y + 1, oldcolor, newcolor);
floodfill(x - 1, y, oldcolor, newcolor);
floodfill(x, y - 1, oldcolor, newcolor);
}
}
void CarBody(int i, int color, int stylecolor)
{
/***CAR BODY ******/
setcolor(color);
setfillstyle(SOLID_FILL, stylecolor);
line(50 + i, 370, 90 + i, 370);
arc(110 + i, 370, 0, 180, 20); // half circle
line(130 + i, 370, 220 + i, 370);
arc(240 + i, 370, 0, 180, 20); // half circle
line(260 + i, 370, 300 + i, 370);
line(300 + i, 370, 300 + i, 350);
line(300 + i, 350, 240 + i, 330);
line(240 + i, 330, 200 + i, 300);
line(200 + i, 300, 110 + i, 300);
line(110 + i, 300, 80 + i, 330);
line(80 + i, 330, 50 + i, 340);
line(50 + i, 340, 50 + i, 370);
/***CAR Windows***/
line(165 + i, 305, 165 + i, 330);
line(165 + i, 330, 230 + i, 330);
line(230 + i, 330, 195 + i, 305);
line(195 + i, 305, 165 + i, 305);
line(160 + i, 305, 160 + i, 330);
line(160 + i, 330, 95 + i, 330);
line(95 + i, 330, 120 + i, 305);
line(120 + i, 305, 160 + i, 305);
}
void CarWheel(int i)
{
/**Wheels**/
setcolor(BLUE);
setfillstyle(SOLID_FILL, DARKGRAY);
circle(110 + i, 370, 17);
floodfill(110 + i, 370, BLUE);
circle(240 + i, 370, 17);
floodfill(240 + i, 370, BLUE);
delay(10);
cleardevice();
setcolor(WHITE);
}
void Road(int x1, int y1, int x2, int y2)
{
line(x1, y1, x2, y2); //ROAD
}
int main()
{
int gd = DETECT, gm;
int array[] = {GREEN, YELLOW, RED, BLUE};
int colorinput;
while (1)
{
printf("Please Choose a color for car:-\n");
printf("1. GREEN\n2. YELLOW\n3. RED\n4. BLUE\n");
printf("Enter Now number: (required) = ");
scanf("%d", &colorinput);
if (colorinput <= 0 || colorinput > 4)
{
printf("Wrong Input........");
Sleep(1000);
system("cls");
}
else
{
printf("See now...");
break;
}
}
initgraph(&gd, &gm, (char *)"");
for (int i = 0; i < 500; i++)
{
CarBody(i, YELLOW, array[colorinput - 1]);
floodfill(160 + i, 350, YELLOW);
CarWheel(i);
Road(0, 390, 639, 390);
}
getch();
return 0;
} OUTPUT:-