Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

Computer Graphics (IT0501)

INDEX

S.No Name of Experiment Sign Page


No.
PART A: Algorithm based practicals

1 Digital Differential Analyzer Algorithm


2 Bresenham’s Line Drawing Algorithm
3 Midpoint Circle Generation Algorithm
4 2- Dimensional Transformation
1) Translation 2) Scalling 3) Rotation
5 W.A.P in C to implement 2-Dimensional Transformation Such
as Sher & Reflection
6 W.A.P in C to implement 3-Dimensional Transformation Such as
translation,rotation & scalling.

PART B: Animation based practicals

1 Draw a picture frame using computer graphics programming


in C

2 Draw a moving Wheel using computer graphics programming in C

3 Draw an Analog Clock using computer graphics programming in C

4 Draw a moving car using computer graphics programming in C

IU2141220086 Avi Patel 1


Computer Graphics (IT0501)

Practical: - 1(A)

Aim: - IMPLEMENT DDA LINE DRAWING ALGORITHMS USING C


GRAPHICS.
Program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void main()
{
int gd= DETECT ,gm , xmax, ymax
; int x1 , y1, x2, y2, i = 0 ,dx ,dy;
float xincr , yincr , x , y
,step; printf("Enter x1:");
scanf("%d",&x1);
printf("Enter x2:");
scanf("%d",&x2);
printf("Enter Y1:");
scanf("%d",&y1);
printf("Enter Y2:");
scanf("%d",&y2);
initgraph(&gd,&gm , "C:\\TURBOC3\\BGI");
x = x1;
y =y1;
dx = x2 -x1;
dy = y2 -y1;
if(dx>dy)
step = dx;
else
step = dy;
xincr = dx /
step; yincr = dy
/ step;
putpixel(x, y, WHITE);
for( i = 0; i <= step; i++){
x = x+ xincr
;y=y+
yincr;
putpixel(ceil(x), ceil(y), WHITE);
outtextxy(300,380,"IU2141220086");
outtextxy(300,400,"Avi Patel");
delay(10);
IU2141220086 Avi Patel 2
Computer Graphics (IT0501)
}
getch();
}
OUTPUT:

Rushi Pate l
Avi Patel

IU2141220086
IU2141220108

IU2141220086 Avi Patel 3


Computer Graphics (IT0501)

Practical: - 1(B)
Aim: - TO STUDY BASICS OF COMPUTER GRAPHICS BY CREATING
A PICTURE FRAME IN C/C++.
Program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm,xmax,ymax;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
xmax=getmaxx();
ymax=getmaxy();
printf("%d %d",xmax,ymax);
setfillstyle(SOLID_FILL,2);
line(0,ymax/2,xmax,ymax/2);
line(0,ymax/2,xmax/8,0);
line(xmax/8,0,xmax/4,ymax/2);
line(xmax/4,ymax/2,xmax/3,0);
line(xmax/3,0,xmax/2,ymax/2);
floodfill(xmax/8,(ymax/2)-10,getmaxcolor());
setfillstyle(SOLID_FILL,6);
circle(xmax/2,100,40);
floodfill(xmax/2,100,getmaxcolor());
getch();
}

OUTPUT:

IU2141220086 Avi Patel 4


Computer Graphics (IT0501)

IU2141220086 Avi Patel 5


Computer Graphics (IT0501)
Practical: - 2(A)
Aim: - IMPLEMENT BRESENHAM'S LINE DRAWING ALGORITHM.

Program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void main(){
int gd= DETECT ,gm ;
int x1 , y1, x2, y2, i = 0 ,dx
,dy,x,y; int s1,s2,swap=0 ,temp,e;
printf("Enter x1:");
scanf("%d",&x1);
printf("Enter x2:");
scanf("%d",&x2);
printf("Enter Y1:");
scanf("%d",&y1);
printf("Enter Y2:");
scanf("%d",&y2);
initgraph(&gd,&gm , "C:\\TURBOC3\\BGI");
x = x1;
y =y1;
dx =abs (x2 -
x1); dy =abs (y2
-y1);

if(x2>x1)
{ s1=
1;

IU2141220086 Avi Patel 6


Computer Graphics (IT0501)
}
else if(x2==x1)
{ s1=
0;
}
else
{
s1=-1;
}
if(y2>y1)
s2=1;
else if
(y2==y1) s2=0;
else
s2=-1;
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
swap=1;
}
else
swap=0;
e=(2*dy)-dx;
for(i=0;i<=dx;i++)
{
putpixel(x,y,15);
while(e>=0)
{
if(swap==1)
x=x+s1;
else
y=y+s2;
e=e-(2*dx);
}
if(swap==1)
y=y+s2;
else
x=x+s1;
e=e+(2*dy);
}
putpixel(ceil(x), ceil(y), WHITE);
outtextxy(300,380,"IU2141220086");
outtextxy(300,400,"Avi Patel");
delay(10);

IU2141220086 Avi Patel 7


Computer Graphics (IT0501)
getch();
}

OUTPUT: -

Rushi Patel
IU2141220108

Avi Patel IU2141220086

IU2141220086 Avi Patel 8


Computer Graphics (IT0501)

Practical: - 2(B)
Aim: - IMPLEMENT A ROTATING WHEEL USING C GRAPHICS.

Program: -
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode,
th=0; float rad1, rad2,rad3, rad4;
int x, y,i=0;
int r = 100;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "C:\\TURBOC3\\
BGI");

/* read result of initialization


*/ x = 100;
y = getmaxy() / 2;
setcolor(getmaxcolor());
rad1=th*3.14/180;

/* draw the circle */


// for(int i=0; i<600; i++)
while(!kbhit()

IU2141220086 Avi Patel 9


Computer Graphics (IT0501)
{ cleardevice();
circle(x+i, y , (r+30));
circle(x+i, y,r);
line( x+i, y,(x+i)
+r*sin(rad1),y+r*cos(rad1)); line( x+i, y,
(x+i)-r*sin(rad1),y-r*cos(rad1));
rad2=(th+45)*3.14/180;
line( x+i, y,(x+i)+r*sin(rad2),y+r*cos(rad2));
line( x+i, y,(x+i)-r*sin(rad2),y-r*cos(rad2));
rad3=(th+90)*3.14/180;
line( x+i, y,(x+i)
+r*sin(rad3),y+r*cos(rad3)); line( x+i, y,
(x+i)-r*sin(rad3),y-r*cos(rad3));
rad4=(th+135)*3.14/180;
line( x+i, y,(x+i)+r*sin(rad4),y+r*cos(rad4));
line( x+i, y,(x+i)-r*sin(rad4),y-r*cos(rad4));
th=th+15;
i++;
delay(10);
}
/* clean up
*/ getch();
closegraph();
return 0;
}

OUTPUT:

IU2141220086 Avi Patel 10


Computer Graphics (IT0501)

Practical: - 3(A)
Aim: - Implement mid point circle algorithm.

Program: -
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
int main()
{
int gd=DETECT,gm;
int xc=300,yc=200,x=0,y=0,r=70;
float p;
/*printf("Enter center and radius");
scanf("%d%d%d",&xc,&yc,&r); */
initgraph(&gd,&gm,"..\\BGI\\");
x=0;
y=r;
p=(5/4)-r;
while(x<=y)
{
if(p<0)
{
x=x+1;
p=p+(2*x)+1;
}
else
{
x=x+1;

IU2141220086 Avi Patel 11


Computer Graphics (IT0501)
y=y-1;
p=p+2*(x-y)+1;
}
putpixel(xc+x,yc+y,15);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,6);
putpixel(xc-y,yc-x,1);
putpixel(xc+x,yc-y,7);
putpixel(xc+y,yc-x,3);
putpixel(xc-x,yc+y,4);
putpixel(xc-y,yc+x,2);
delay(50);
}
outtextxy(300,380,"IU2141220086");
outtextxy(300,400,"Avi Patel");
getch();
return 0;
}

OUTPUT:

IU2141220108
Rushi Patel
IU2141220086
Avi Patel

IU2141220086 Avi Patel 12


Computer Graphics (IT0501)

Practical: - 3(B)
Aim: - Draw an Analog Clock using computer graphics programming in C.

Program: -
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
int main(void)
{
float rad1,rad2,rad3,rad4,rad5,rad6,rad7;
int t1=0;
int th=0;
int gdriver = DETECT,
gmode; int midx, midy;
int radius = 100;
initgraph(&gdriver, &gmode,
"C://TURBOC3//BGI"); while(!kbhit())
{
cleardevice();
midx = getmaxx()/2;
midy = getmaxy()/2;
setfillstyle(SOLID_FILL,9);
circle(midx,midy,radius);
floodfill(midx,midy+85,getmaxcolor());
setfillstyle(SOLID_FILL,1);
circle(midx,midy,90);
floodfill(midx,midy+95,getmaxcolor());
setfillstyle(SOLID_FILL,8);

IU2141220086 Avi Patel 13


Computer Graphics (IT0501)
circle(midx,midy,5);
floodfill(midx,midy+3,getmaxcolor());
rad1=th*3.14/180;
rad2=(th+30)*3.14/180;
rad3=(th+60)*3.14/180;
rad4=(th+90)*3.14/180;
rad5=(th+120)*3.14/180;
rad6=(th+150)*3.14/180;
outtextxy((midx+80*sin(rad1)),(midy+80*cos(rad1)),"6");
outtextxy((midx-80*sin(rad1)),(midy-80*cos(rad1)),"12");
outtextxy((midx+80*sin(rad2)),(midy+80*cos(rad2)),"5");
outtextxy((midx-80*sin(rad2)),(midy-80*cos(rad2)),"11");
outtextxy((midx+80*sin(rad3)),(midy+80*cos(rad3)),"4");
outtextxy((midx-80*sin(rad3)),(midy-80*cos(rad3)),"10");
outtextxy((midx+80*sin(rad4)),(midy+80*cos(rad4)),"3");
outtextxy((midx-80*sin(rad4)),(midy-80*cos(rad4)),"9");
outtextxy((midx+80*sin(rad5)),(midy+80*cos(rad5)),"2");
outtextxy((midx-80*sin(rad5)),(midy-80*cos(rad5)),"8");
outtextxy((midx+80*sin(rad6)),(midy+80*cos(rad6)),"1");
outtextxy((midx-80*sin(rad6)),(midy-80*cos(rad6)),"7");
line(midx,midy,midx-70*sin(rad1),midy-70*cos(rad1));
line(midx,midy,midx+40*sin(rad4),midy+40*cos(rad4));
rad7=t1*3.14/180;
line(midx,midy,midx+60*sin(rad7),midy+60*cos(rad7));
t1=t1-1;
outtextxy(300,380,"IU2141220086");
outtextxy(300,400,"Avi Patel");
delay(100);
}
getch();
closegraph();
return 0;
}

OUTPUT:

IU2141220086 Avi Patel 14


Computer Graphics (IT0501)

IU2141220108 Rushi Patel


IU2141220086 Avi Patel

IU2141220086 Avi Patel 15


Computer Graphics (IT0501)
Practical: - 4(A)
Aim: - IMPLEMENT FOLLOWING 2D TRANSFORMATION.

Program: -
#include <stdio.h>
#include <math.h>
#include <graphics.h>
#include <conio.h>

typedef struct {
int x;
int y;
} Point;

void drawSquare(Point vertices[4])


{ for (int i = 0; i < 4; i++) {
line(vertices[i].x, vertices[i].y, vertices[(i + 1) % 4].x, vertices[(i + 1) % 4].y);
}
}

void translate(Point vertices[4], int dx, int dy)


{ for (int i = 0; i < 4; i++) {
vertices[i].x +=
dx; vertices[i].y
+= dy;
}
}

void scale(Point vertices[4], float sx, float sy)


{ for (int i = 0; i < 4; i++) {
vertices[i].x = (int)(vertices[i].x *
sx); vertices[i].y = (int)(vertices[i].y
* sy);
}
}

void rotate(Point vertices[4], float angle)


{ float radians = angle * M_PI / 180.0;
int cx = (vertices[0].x + vertices[2].x) / 2; // Center of the
square int cy = (vertices[0].y + vertices[2].y) / 2;

for (int i = 0; i < 4; i++)


{ int x = vertices[i].x -
cx; int y = vertices[i].y -
cy;
vertices[i].x = cx + (int)(x * cos(radians) - y * sin(radians));
IU2141220086 Avi Patel 16
Computer Graphics (IT0501)
vertices[i].y = cy + (int)(x * sin(radians) + y *
cos(radians));
}
}

IU2141220086 Avi Patel 17


Computer Graphics (IT0501)

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm,"C:\\turboc3\\bgi");

Point vertices[] = {
{100, 100},
{200, 100},
{200, 200},
{100, 200}
};

drawSquare(vertices);

int choice;
printf("Select transformation:\n");
printf("1. Translation\n");
printf("2. Scaling\n");

printf("3. Rotation\n");
printf("Enter your choice:
"); scanf("%d", &choice);

switch (choice) {
case 1: {
int dx, dy;
printf("Enter translation values (dx dy):
"); scanf("%d %d", &dx, &dy);
translate(vertices, dx, dy);
break;
}
case 2: {
float sx, sy;
printf("Enter scaling factors (sx sy):
"); scanf("%f %f", &sx, &sy);
scale(vertices, sx, sy);
break;
}
case 3: {
float angle;
printf("Enter rotation angle (degrees):
"); scanf("%f", &angle);
rotate(vertices, angle);
break;
}
default:
printf("Invalid choice\n");
IU2141220086 Avi Patel 18
Computer Graphics (IT0501)
closegraph();
return 1;
}

drawSquare(vertices);

outtextxy(400,380,"IU2141220086");
outtextxy(400,400,"Avi Patel");

getch();
closegraph();
return 0;
}

OUTPUT:

IU2141220108
IU2141220086

IU2141220086 Avi Patel 19


Computer Graphics (IT0501)

Practical: - 4(B)
Aim: - Create inverse animation keeping car static and moving the road.

Program: -
#include <stdio.h>
#include <graphics.h>
#include <dos.h>

int main()
{
int gd = DETECT, gm; int i, maxx, midy;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
maxx = getmaxx();
midy = getmaxy()/2;

for (i=0; i < maxx-150; i=i+5)


{ cleardevice();

setcolor(WHITE);
line(0, midy + 37, maxx, midy +
37); setcolor(YELLOW);
setfillstyle(SOLID_FILL, RED);
line(i, midy + 23, i, midy);
line(i, midy, 40 + i, midy - 20);
line(40 + i, midy - 20, 80 + i, midy - 20);
line(80 + i, midy - 20, 100 + i, midy);
line(100 + i, midy, 120 + i, midy);
line(120 + i, midy, 120 + i, midy + 23);
line(0 + i, midy + 23, 18 + i, midy + 23);
arc(30 + i, midy + 23, 0, 180, 12);
line(42 + i, midy + 23, 78 + i, midy +
23); arc(90 + i, midy + 23, 0, 180, 12);
line(102 + i, midy + 23, 120 + i, midy +
23); line(28 + i, midy, 43 + i, midy - 15);
line(43 + i, midy - 15, 57 + i, midy -
15); line(57 + i, midy - 15, 57 + i,
midy); line(57 + i, midy, 28 + i, midy);
line(62 + i, midy - 15, 77 + i, midy -
15); line(77 + i, midy - 15, 92 + i,
midy); line(92 + i, midy, 62 + i, midy);
line(62 + i, midy, 62 + i, midy - 15);
floodfill(5 + i, midy + 22, YELLOW);
setcolor(BLUE);
setfillstyle(SOLID_FILL, DARKGRAY);
IU2141220086 Avi Patel 20
Computer Graphics (IT0501)
circle(30 + i, midy + 25, 9);
circle(90 + i, midy + 25, 9);
floodfill(30 + i, midy + 25, BLUE);
floodfill(90 + i, midy + 25, BLUE);
delay(100);
outtextxy(300,380,"IU2141220086");
outtextxy(300,400,"Avi Patel");
}
closegraph();
return 0;
}

OUTPUT: -

IU2141220108
Rushi Patel

IU2141220086
Avi Patel

IU2141220086 Avi Patel 21


Computer Graphics (IT0501)

Practical: - 5(A)
Aim: - W.A.P. To write a Program in C to implement the basic 2-Dimensional
transformations such as Shear and Reflection.

Program: -
#include <conio.h>
#include <graphics.h>
#include <stdio.h>
void main()
{
int gm, gd = DETECT, ax, x1 = 100;
int x2 = 100, x3 = 200, y1 = 100;
int y2 = 200, y3 = 100;

initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");


cleardevice();
line(getmaxx() / 2, 0, getmaxx() / 2, getmaxy());
line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);
printf("Before Reflection Object"" in 2nd Quadrant");
setcolor(14);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
printf("\nAfter Reflection");
setcolor(4);
line(getmaxx() - x1, getmaxy() - y1, getmaxx() - x2, getmaxy() - y2);
line(getmaxx() - x2, getmaxy() - y2, getmaxx() - x3, getmaxy() - y3);
line(getmaxx() - x3, getmaxy() - y3, getmaxx() - x1, getmaxy() - y1);
setcolor(3);
line(getmaxx() - x1, y1, getmaxx() - x2, y2);
line(getmaxx() - x2, y2, getmaxx() - x3, y3);
line(getmaxx() - x3, y3, getmaxx() - x1, y1);
setcolor(2);
line(x1, getmaxy() - y1, x2, getmaxy() - y2);
line(x2, getmaxy() - y2, x3, getmaxy() - y3);
line(x3, getmaxy() - y3, x1, getmaxy() - y1);
getch();
closegraph();
}

IU2141220086 Avi Patel 22


Computer Graphics (IT0501)

OUTPUT: -

IU2141220086 Avi Patel 23


Computer Graphics (IT0501)

Practical: - 6(A)

Aim: - W.A.P. To write a Program in C to implement the basic 3-Dimensional


transformations such as translation, rotation, scaling.

Program: -
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

int maxx,maxy,midx,midy;
void axis()
{
getch(); cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}

void main()
{
int gd = DETECT, gm;
int x,y,z,ang,x1,x2,y1,y2;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setfillstyle(3,25);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;

outtextxy(100,100,"ORIGINAL OBJECT");
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
axis();

outtextxy(100,20,"TRANSLATION");
printf("\n\n Enter the Translation vector:
"); scanf("%d%d",&x,&y);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5); bar3d(midx+
(x*100),midy-(y+20),midx+(x+60),midy-(y+90),20,5); axis();

outtextxy(100,20,"SCALING");
printf("\n Enter the Scaling Factor: ");
scanf("%d%d%d",&x,&y,&z);

IU2141220086 Avi Patel 24


Computer Graphics (IT0501)
bar3d(midx+100,midy-20,midx+60,midy-90,20,5); bar3d(midx+
(x*100),midy-(y*20),midx+(x*60),midy-(y*90),20*z,5); axis();

outtextxy(100,20,"ROTATION");
printf("\n Enter the Rotation angle:
"); scanf("%d",&ang);

x1=100*cos(ang*3.14/180)-20*sin(ang*3.14/180);
y1=100*sin(ang*3.14/180)+20*sin(ang*3.14/180);
x2=60*cos(ang*3.14/180)-90*sin(ang*3.14/180);
y2=60*sin(ang*3.14/180)+90*sin(ang*3.14/180);
axis();

printf("\n After rotation about z-axis\n");


bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,20,5);
axis();

printf("\n After rotation about x-axis\n");


bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+100,midy-x1,midx+60,midy-x2,20,5);
axis();

printf("\n After rotation about y-axis\n");


bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-20,midx+x2,midy-90,20,5);
axis();
closegraph();
}
OUTPUT: -

IU2141220086 Avi Patel 25


Computer Graphics (IT0501)

IU2141220086 Avi Patel 26

You might also like