You are on page 1of 32

Lingaya’s Vidyapeeth, Faridabad

(Deemed to be University under Section 3 of UGC Act, 1956)

Computer GraphicsLab
CA-1254A

LAB MANUAL
BCA

Nachauli, Old Faridabad-Jasana Road,


Faridabad 121002, Phone: (129) 2201008, 2201009;
WebSite: www.lingayasuniversity.edu.in

Name :……Adarsh singh………………………………………


Roll No :……19BCA01……………….. Branch: …BCA………
Group :……………………….. Session: 2020.. – 2021…

DEPTT. OF COMPUTER SCIENCE & ENGG.

PROGRAM=1
1: write a program for 2D line as raster graphics display using Bresenhem line
drawing algorithm
#include<stdio.h>

#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)

int dx, dy, p, x, y;

dx=x1-x0;

dy=y1-y0;

x=x0;

y=y0;

p=2*dy-dx;

while(x<x1)

if(p>=0)

putpixel(x,y,7);

y=y+1;

p=p+2*dy-2*dx;

else

putpixel(x,y,7);

p=p+2*dy;}

x=x+1;

int main()

int gdriver=DETECT, gmode, error, x0, y0, x1, y1;

initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

printf("Enter co-ordinates of first point: ");

scanf("%d%d", &x0, &y0);


printf("Enter co-ordinates of second point: ");

scanf("%d%d", &x1, &y1);

drawline(x0, y0, x1, y1);

return 0;

OUTPUT:
Program=2
2: write a program for 2D line drawing as raster graphics display
using DDA line drawing algorithm
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

intgd = DETECT ,gm, i;

float x, y,dx,dy,steps;

int x0, x1, y0, y1;

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

setbkcolor(WHITE);

x0 = 100 , y0 = 200, x1 = 500, y1 = 300;

dx = (float)(x1 - x0);

dy = (float)(y1 - y0);

if(dx>=dy)

steps = dx;

else

steps = dy;

dx = dx/steps;

dy = dy/steps;

x = x0;

y = y0;

i = 1;

while(i<= steps)

putpixel(x, y, RED);
x += dx;

y += dy;

i=i+1;

getch();

closegraph();

Output:
Program=3
3: Write a program for circle drawing as raster graphics display
using mid point circle drawing algorithm
#include<stdio.h>`

#include<conio.h>

#include<graphics.h>

Void main()

Int gd=DETECT,gm;

Int d,r,x,y,xc,yc;

Clrscr();

Initgraph(&gd.&gm,”C:\\TC\\BGI”);

Printf(“Enter coordinate of circle”);

Scanf(“%d%d”,&xc,&xy);

Printf(“Enter of radius”);

Scanf(“%d”,&r);

d=1-r;

x=0;

y=r;

while(x<=y)

Putpixel(xc+x,yc+y,5);

Putpixel(xc-x,yc-y,5);

Putpixel(xc+x,yc-y,5);

Putpixel(xc+x,yc+y,5);

Putpixel(xc-x,yc-y,5);

Putpixel(xc+x,yc-y,5);

Putpixel(xc-x,yc+y,5);

Putpixel(xc+y,yc-x,5);

Putpixel(xc-y,yc-x,5);
Putpixel(xc+y,yc-x,5);

Putpixel(xc-y,yc+x,5);

If(d<=0)

d=d+2*x+1;

Else

d=d+2*(x-y)+5;

y=y-1;

X=x+1;

getch();

}
OUTPUT:
Program=4
4: Write a program for circle drawing as raster graphics display
using mid point using Bresenhems circle drawing algorithm
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

Void main()

Int gd=DETECT,gm;

Int d,r,x,y,xc,yc;

Clrscr();

Initgraph(&gd.&gm,”C:\\TC\\BGI”);

Printf(“Enter the radius”);

Scanf(“%d”,&r);

Printf(“Enter the center of circle”);

Scanf(“%d%d”,&xc,&yc);

d=3-2*r;

x=0;

y=r;

while(x<=y)

Putpixel(xc+x,yc+y,5);

Putpixel(xc-y,yc-x,5);

Putpixel(xc+y,yc-x,5);

Putpixel(xc-y,yc+x,5);

Putpixel(xc+x,yc-y,5);

Putpixel(xc-x,yc+y,5);

If(d<=0)

d=d+4*x+6;

Else
{

d=d+4*(x-y)+10;

y=y-1;

X=x+1;

getch();

Output:
PROGRAM=5
5:Write a program for transformation
(a) 2D Translation Transformation
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

int graphdriver=DETECT,graphmode,errorcode;

int i;

int x2,y2,x1,y1,x,y;

printf("Enter the 2 line end points:");

printf("x1,y1,x2,y2");

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");

line(x1,y1,x2,y2);

printf("Enter translation co-ordinates ");

printf("x,y");

scanf("%d%d",&x,&y);

x1=x1+x;

y1=y1+y;

x2=x2+x;

y2=y2+y;

printf("Line after translation");

line(x1,y1,x2,y2);

getch();

closegraph();

}
OUTPUT:
(b) 2D RotationTransformation
#include<graphics.h>

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

int graphdriver=DETECT,graphmode,errorcode;

int i;

int x2,y2,x1,y1,x,y,xn,yn;

double r11,r12,r21,r22,th;

clrscr();

printf("Enter the 2 line end points:");

printf("x1,y1,x2,y2");

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");

line(x1,y1,x2,y2);

printf("\n\n\n[ Enter the angle");

scanf("%lf",&th);

r11=cos((th*3.1428)/180);

r12=sin((th*3.1428)/180);

r21=(-sin((th*3.1428)/180));

r22=cos((th*3.1428)/180);

//printf("%lf %lf %lf %lf",r11,r12,r21,r22);

xn=((x2*r11)-(y2*r12));

yn=((x2*r12)+(y2*r11));

line(x1,y1,xn,yn);

getch();

closegraph();

}
OUTPUT:
(c ) 2D scalling Transformation
#include<graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include<math.h>

void main()

int graphdriver=DETECT,graphmode,errorcode;

int i;

int x2,y2,x1,y1,x,y;

printf("Enter the 2 line end points:");

printf("x1,y1,x2,y2");

scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");

line(x1,y1,x2,y2);

printf("Enter scaling co-ordinates ");

printf("x,y");

scanf("%d%d",&x,&y);

x1=(x1*x);

y1=(y1*y);

x2=(x2*x);

y2=(y2*y);

printf("Line after scaling");

line(x1,y1,x2,y2);

getch();

closegraph();

}
OUTPUT:
PROGRAM=6
6:Write a program for line clipping
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
 
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
 
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
 
void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;

cout<<"\nEnter x1 and y1\n";


cin>>p1.x>>p1.y;
cout<<"\nEnter x2 and y2\n";
cin>>p2.x>>p2.y;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);

drawline(p1,p2);
delay(500);
cleardevice();

delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);

switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}

delay(5000);
closegraph();
}
 
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
 
void drawline(PT p1,PT p2)
{
line(p1.x,p1.y,p2.x,p2.y);
}
 
PT setcode(PT p)
{
PT ptemp;

if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';

if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';

if(p.x>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';

if(p.x<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';

ptemp.x=p.x;
ptemp.y=p.y;

return(ptemp);
}
 
int visibility(PT p1,PT p2)
{
int i,flag=0;

for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}

if(flag==0)
return(0);

for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}
if(flag==0)
return(1);

return(2);
}
 
PT resetendpt(PT p1,PT p2)
{
PT temp;
int x,y,i;
float m,k;

if(p1.code[3]=='1')
x=150;

if(p1.code[2]=='1')
x=450;

if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;

for(i=0;i<4;i++)
temp.code[i]=p1.code[i];

if(temp.y<=350 && temp.y>=100)


return (temp);
}

if(p1.code[0]=='1')
y=100;

if(p1.code[1]=='1')
y=350;

if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;

for(i=0;i<4;i++)
temp.code[i]=p1.code[i];

return(temp);
}
else
return(p1);
}
OUTPUT:
PROGRAM=7:
7:write a program for polygon clipping
#include<stdio.h>

#include<graphics.h>

#include<conio.h>

#include<stdlib.h>

int main()

int gd,gm,n,*x,i,k=0;

int wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,wy4=340;

int w[]={220,140,420,140,420,340,220,340,220,140};//

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

printf("Window:-");

setcolor(RED);

drawpoly(5,w);

printf("Enter the no. of vertices of polygon: ");

scanf("%d",&n);

x = malloc(n*2+1);

printf("Enter the coordinates of points:\n");

k=0;

for(i=0;i<n*2;i+=2)

printf("(x%d,y%d): ",k,k);

scanf("%d,%d",&x[i],&x[i+1]);

k++;

x[n*2]=x[0];.

x[n*2+1]=x[1];

setcolor(WHITE);
drawpoly(n+1,x);

printf("\nPress a button to clip a polygon..");

getch();

setcolor(RED);

drawpoly(5,w);

setfillstyle(SOLID_FILL,BLACK);

floodfill(2,2,RED);

gotoxy(1,1);

printf("\nThis is the clipped polygon..");

getch();

cleardevice();

closegraph();

return 0;

}
PROGRAM=8
8:write a program for 3D translation
#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

#include<graphics.h>

int x1,x2,y1,y2,mx,my,depth;

void draw();

void trans();

void main()

int gd=DETECT,gm,c;

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

printf("\n\t\t3D Translation\n\n");

printf("\nEnter 1st top value(x1,y1):");

scanf("%d%d",&x1,&y1);

printf("Enter right bottom value(x2,y2):");

scanf("%d%d",&x2,&y2);

depth=(x2-x1)/4;

mx=(x1+x2)/2;

my=(y1+y2)/2;

draw();

getch();

cleardevice();

trans();

getch();

void draw()

bar3d(x1,y1,x2,y2,depth,1);

}
void trans()

int a1,a2,b1,b2,dep,x,y;

printf("\n Enter the Translation Distances:");

scanf("%d%d",&x,&y);

a1=x1+x;

a2=x2+x;

b1=y1+y;

b2=y2+y;

dep=(a2-a1)/4;

bar3d(a1,b1,a2,b2,dep,1);

setcolor(5);

draw();

}
OUTPUT:
PROGRAM=9
9:write a program for rotation for 3D object
#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

int x1,x2,y1,y2,mx,my,depth;

void draw();

void rotate();

void main()

int gd=DETECT,gm,c;

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

printf("\n3D Transformation Rotating\n\n");

printf("\nEnter 1st top value(x1,y1):");

scanf("%d%d",&x1,&y1);

printf("Enter right bottom value(x2,y2):");

scanf("%d%d",&x2,&y2);

depth=(x2-x1)/4;

mx=(x1+x2)/2;

my=(y1+y2)/2;

draw(); getch();

cleardevice();

rotate();

getch();

void draw()

bar3d(x1,y1,x2,y2,depth,1);

void rotate()

{
float t;

int a1,b1,a2,b2,dep;

printf("Enter the angle to rotate=");

scanf("%f",&t);

t=t*(3.14/180);

a1=mx+(x1-mx)*cos(t)-(y1-my)*sin(t);

a2=mx+(x2-mx)*cos(t)-(y2-my)*sin(t);

b1=my+(x1-mx)*sin(t)-(y1-my)*cos(t);

b2=my+(x2-mx)*sin(t)-(y2-my)*cos(t);

if(a2>a1)

dep=(a2-a1)/4;

else

dep=(a1-a2)/4;57

bar3d(a1,b1,a2,b2,dep,1); setcolor(5);

}
OUTPUT:
PROGRAM=10
9:write a program to create screen saver using graphics
commands
#include<stdio.h>

#include<conio.h>

#include"graphics.h"

#include"stdlib.h"

void main()

intgd=DETECT,gm,i=0,x,xx,y,yy,r;

//Initializes the graphics system

initgraph(&gd,&gm,"c:\\tc\\bgi");

x=getmaxx();

y=getmaxy();

while(!kbhit())

i++;

// setfillstyle(random(i),random(30));

circle(xx=random(x),yy=random(y),random(30));

setfillstyle(random(i),random(30));

floodfill(xx,yy,getmaxcolor());

delay(200);

getch();

}
OUTPUT:

You might also like