0% found this document useful (0 votes)
111 views

CGraphics1 PDF

The document contains a computer graphics practical file submitted by Yatinder Singh Rawat. It includes 8 programs: 1) Line scan conversion using DDA algorithm 2) Line scan using Bresenham's algorithm 3) Circle scan using Bresenham's algorithm 4) Ellipse scan using Bresenham's algorithm 5) Circle scan using midpoint algorithm 6) Scaling transformation 7) Translation transformation 8) Cohen-Sutherland line clipping algorithm Each program contains the source code and output for the corresponding computer graphics concept.

Uploaded by

Narender Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

CGraphics1 PDF

The document contains a computer graphics practical file submitted by Yatinder Singh Rawat. It includes 8 programs: 1) Line scan conversion using DDA algorithm 2) Line scan using Bresenham's algorithm 3) Circle scan using Bresenham's algorithm 4) Ellipse scan using Bresenham's algorithm 5) Circle scan using midpoint algorithm 6) Scaling transformation 7) Translation transformation 8) Cohen-Sutherland line clipping algorithm Each program contains the source code and output for the corresponding computer graphics concept.

Uploaded by

Narender Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

COMPUTER GRAPHICS

PRACTICAL FILE

SUBMITTED TO: SUBMITTED BY:


MR. DEEPAK SHARMA YATINDER SINGH RAWAT
(07211402020)

1|Page YATINDER SINGH RAWAT


07211402020
1. Write a Program to Scan convert line using DDA algorithm

2. Write a Program to Scan convert line using Bresenham’s


algorithm.

3. Write a Program to Scan convert circle using Bresenham’s


algorithm.

4. Write a Program to Scan convert ellipse using Bresenham’s


algorithm.

5. Write a Program to Scan convert circle using Mid-point


algorithm.

6. Write a Program to implement Scaling Transformation.

7. Write a Program to implement Translation Transformation.

8. Write a Program to implement Cohen Sutherland line


clipping algorithm.

2|Page YATINDER SINGH RAWAT


07211402020
Q1.Write a Program to Scan convert line using DDA algorithm.
Source Code:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int main( )
{
float x,y,dx,dy,step;
int x1,x2,y1,y2;
int gd=DETECT,gm,i;

initgraph(&gd,&gm, (char*)"");
outtextxy(280,50, "***DDA Line***");
setbkcolor(WHITE);
cout<<"Enter the value of x1 and y1 : ";
cin>>x1>>y1;
cout<<"Enter the value of x2 and y2: ";
cin>>x2>>y2;
dx=(float)(x2-x1);
dy=(float)(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;

dx=dx/step;
dy=dy/step;

3|Page YATINDER SINGH RAWAT


07211402020
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,WHITE);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
return 0; }

Output:

4|Page YATINDER SINGH RAWAT


07211402020
Q2.Write a Program to Scan convert line using Bresenham’s
algorithm.
Source Code:
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
int main(){
int gd=DETECT,gm;
int x1,y1,x2,y2,dx,dy,p;
initgraph(&gd,&gm,(char*)" "); //for graph
outtextxy(200,100,"Bresnhem's line Algorithm");
cout<<"Enter the value of x1 and y1:"<<endl;
cin>>x1>>y1;
cout<<"Enter the value of x2 and y2:"<<endl;
cin>>x2>>y2;
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
putpixel(x1,y1,9);//plot first coordinates
for(int i=0;i<dx;i++){
if (p<0){
x1++;
p=p+2*dy;
}
else{
x1++;
y1++;
p=p+2*dy-2*dx;

5|Page YATINDER SINGH RAWAT


07211402020
}
putpixel(x1,y1,9);
delay(50);
}
getch();
closegraph();
return 0;
}

Output:

6|Page YATINDER SINGH RAWAT


07211402020
Q3.Write a Program to Scan convert circle using Bresenham’s
algorithm.
Source Code:
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
int main(){
int xc,yc,x,y,r,d;
int gd=DETECT,gm;
cout<<"Enter the value of radius: ";
cin>>r;
x=0;
y=r;
d=3-2*r;
initgraph(&gd,&gm,(char *)" ");
outtextxy(150,100,"Bresenham's Circle Drawing Algorithm");
cout<<"Enter the center point of circle: ";
cin>>xc>>yc;
do{
putpixel(xc+x,yc+y,7);
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc+y,8);
putpixel(xc-x,yc-y,8);
putpixel(xc+y,yc+x,4);
putpixel(xc+y,yc-x,4);
putpixel(xc-y,yc+x,3);

7|Page YATINDER SINGH RAWAT


07211402020
putpixel(xc-y,yc-x,3);
if(d<0){
x=x+1;
y=y;
d=d+4*x+6; }
else{
x=x+1;
y=y-1;
d=d+4*x-4*y+10;
} }while(x<y);
getch();
closegraph();
return 0;}

Output:

8|Page YATINDER SINGH RAWAT


07211402020
Q4.Write a Program to Scan convert ellipse using
Bresenham’s algorithm.
Source Code:
#include<graphics.h>
#include<iostream>
using namespace std;
void ellipse(double xc,double yc,double a,double b)
{
double p=b*b-a*a*b+a*a/4;
double x=0, y=b;
while(2.0*b*b*x <= 2.0*a*a*y)
{
if(p < 0)
{
x++;
p = p+2*b*b*x+b*b;
}
else
{
x++;
y--;
p = p+2*b*b*x-2*a*a*y-b*b;
}
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);

9|Page YATINDER SINGH RAWAT


07211402020
delay(100); }
p=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
while(y > 0)
{
if(p <= 0)
{
x++;
y--;
p = p+2*b*b*x-2*a*a*y+a*a;
}
else
{
y--;
p = p-2*a*a*y+a*a;
}
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);
delay(100);
}
}
int main()
{
int gd = DETECT, gm;
double xc,yc,x,y, a,b;
initgraph(&gd, &gm, (char*)"");
outtextxy(150,100,"Bresenham's Ellipse Drawing Algorithm");

10 | P a g e YATINDER SINGH RAWAT


07211402020
cout<<"Enter coordinates of centre: ";
cin>>xc>>yc;
cout<<"Enter length of major and minor axix a,b: ";
cin>>a>>b;
ellipse(xc, yc, a, b);
getch();
closegraph();
}

Output:

11 | P a g e YATINDER SINGH RAWAT


07211402020
Q5.Write a Program to Scan convert circle using Mid-point
algorithm.
Source Code:
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
int main(){
int xc,yc,x,y,r,d;
int gd=DETECT,gm;
cout<<"Enter the value of radius: ";
cin>>r;
x=0;
y=r;
d=1-r;
initgraph(&gd,&gm,(char *)" ");
outtextxy(180,100,"Mid Point Circle Drawing Algorithm");
cout<<"Enter the center point of circle: ";
cin>>xc>>yc;
do{
putpixel(xc+x,yc+y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc+y,7);
putpixel(xc-x,yc-y,7);
putpixel(xc+y,yc+x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc+x,7);

12 | P a g e YATINDER SINGH RAWAT


07211402020
putpixel(xc-y,yc-x,7);
if(d<0){
x=x+1;
y=y;
d=d+2*x+3;}
else{
x=x+1;
y=y-1;
d=d+2*x-2*y+5; }
}
while(x<y);
getch();
closegraph();
return 0;}

Output:

13 | P a g e YATINDER SINGH RAWAT


07211402020
Q6.Write a Program to implement Scaling Transformation.
Source Code:
#include<iostream>
#include<conio.h>
#include<graphics.h>
using namespace std;
void beforescaling(int,int,int,int,int,int);
void afterscaling(int,int,int,int,int,int,int,int);

void beforescaling(int x1,int y1,int x2,int y2,int x3,int y3)


{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
}

void afterscaling(int x1,int y1,int x2,int y2,int x3,int y3,int Sx,int Sy)
{
line(x1*Sx,y1*Sy,x2*Sx,y2*Sy);
line(x2*Sx,y2*Sy,x3*Sx,y3*Sy);
line(x1*Sx,y1*Sy,x3*Sx,y3*Sy);
}

int main()
{
int x1=100,x2=120,y1=100,y2=200,x3=150,y3=200,Sx=2,Sy=2,gd=DETECT,gm;

14 | P a g e YATINDER SINGH RAWAT


07211402020
initgraph(&gd,&gm,(char*)"");
outtextxy(300,40,"*****SCALING*****");

beforescaling(x1,x2,y1,y2,x3,y3);
afterscaling(x1,y1,x2,y2,x3,y3,Sx,Sy);

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

Output:

15 | P a g e YATINDER SINGH RAWAT


07211402020
Q7.Write a Program to implement Translation
Transformation.
Source Code:
#include<iostream>
#include<conio.h>
#include<graphics.h>
using namespace std;
void beforetranslate(int,int,int,int,int,int);
void aftertranslate(int,int,int,int,int,int,int,int);

void beforetranslate(int x1,int y1,int x2,int y2,int x3,int y3)


{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

}
void aftertranslate(int x1,int y1,int x2,int y2,int x3,int y3,int Sx,int Sy)
{
line(x1+Sx,y1+Sy,x2+Sx,y2+Sy);
line(x2+Sx,y2+Sy,x3+Sx,y3+Sy);
line(x1+Sx,y1+Sy,x3+Sx,y3+Sy);
}

int main()
{
int x1=100,x2=120,y1=100,y2=200,x3=150,y3=200,Sx=100,Sy=70,gd=DETECT,gm;

initgraph(&gd,&gm ,(char*)"");

16 | P a g e YATINDER SINGH RAWAT


07211402020
outtextxy(300,20,"*****TRANSLATION*****");
beforetranslate(x1,x2,y1,y2,x3,y3);
aftertranslate(x1,x2,y1,y2,x3,y3,Sx,Sy);
getch();
closegraph();
return 0;
}

Output:

17 | P a g e YATINDER SINGH RAWAT


07211402020
Q8.Write a Program to implement Cohen Sutherland line
clipping algorithm.
Source Code:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
int a[4],b[4];
float m,xnew,ynew;
float xl=100,yl=100,xh=300,yh=300,xa=10,ya=200,xb=250,yb=150;
int gd = DETECT,gm;
initgraph(&gd,&gm,(char*)"");
setcolor(5);
line(xa,ya,xb,yb);
setcolor(12);
rectangle(xl,yl,xh,yh);
m = (yb-ya)/(xb-xa);

if(xa < xl)


a[3] = 1;
else a[3] = 0;

if(xa>xh)
a[2] = 1;
else a[2] = 0;

18 | P a g e YATINDER SINGH RAWAT


07211402020
if(ya < yl)
a[1] = 1;
else a[1] = 0;

if (ya > yh)


a[0] = 1;
else a[0] = 0;

if(xb < xl)


b[3] = 1;
else b[3] = 0;

if(xb>xh)
b[2] = 1;
else b[2] = 0;

if(yb < yl)


b[1] = 1;
else b[1] = 0;

if (yb > yh)


b[0] = 1;
else b[0] = 0;

printf("press a key to continue");


getch();
if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && b[0] == 0 && b[1] == 0 && b[2]
== 0 && b[3] == 0 )

19 | P a g e YATINDER SINGH RAWAT


07211402020
{
printf("no clipping");
line(xa,ya,xb,yb);
}
else if(a[0]&&b[0] || a[1]&&b[1] || a[2]&&b[2] || a[3]&&b[3])
{
cleardevice();
printf("line discarded");
rectangle(xl,yl,xh,yh);
}
else
{
if(a[3] == 1 && b[3]==0)
{
ynew = (m * (xl-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[2] == 1 && b[2] == 0)
{
ynew = (m * (xh-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
setcolor(0);
line(xa,ya,xb,yb);

20 | P a g e YATINDER SINGH RAWAT


07211402020
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[1] == 1 && b[1] == 0)
{
xnew = xa + (yl-ya)/m;
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xnew,yh,xb,yb);
}

else if(a[0] == 1 && b[0] == 0)


{
xnew = xa + (yh-ya)/m;
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xnew,yh,xb,yb);
}
}
getch();
closegraph();
return 0;
}

21 | P a g e YATINDER SINGH RAWAT


07211402020
Output:

Before Clipping:

After Clipping:

22 | P a g e YATINDER SINGH RAWAT


07211402020

You might also like