0% found this document useful (0 votes)
2 views37 pages

Computer Graphics (2) (1)

The document provides a lab manual for Computer Graphics, detailing the implementation of various algorithms including DDA, Bresenham's, Midpoint Circle, and 2D transformations. It compares the performance of DDA and Bresenham's algorithms, highlighting that Bresenham's is more efficient due to its use of integer arithmetic. Additionally, it discusses the Sutherland-Hodgeman algorithm for polygon clipping and the importance of transformation concatenation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views37 pages

Computer Graphics (2) (1)

The document provides a lab manual for Computer Graphics, detailing the implementation of various algorithms including DDA, Bresenham's, Midpoint Circle, and 2D transformations. It compares the performance of DDA and Bresenham's algorithms, highlighting that Bresenham's is more efficient due to its use of integer arithmetic. Additionally, it discusses the Sutherland-Hodgeman algorithm for polygon clipping and the importance of transformation concatenation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

DEPARTMENT OF COMPUTER SCIENCE &

ENGINEERING

6 BTech (CSE)
Lab Manual
COMPUTER GRAPHICS
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING

Implement the DDA algorithms: (i) Symmetrical & (ii) Simple for line drawing and write
your comment on relative performance of two algorithms based on experimental data.
Answer
(i) Symmetrical DDA:
The Digital Differential Analyzer (DDA) generates lines from their differential equations. The
equation of a straight line is

The DDA works on the principle that we simultaneously increment x and y by small steps
proportional to the first derivatives of x and y. In this case of a straight line, the first derivatives
are constant and are proportional to ∆x and ∆y . Therefore, we could generate a line by
incrementing x and y by ϵ ∆x and ϵ ∆y, where ϵ is some small quantity. There are two ways to
generate points
1. By rounding to the nearest integer after each incremental step, after rounding we display dots at
the resultant x and y.
2. An alternative to rounding the use of arithmetic overflow: x and y are kept in registers that have
two parts, integer and fractional. The incrementing values, which are both less than unity, are
repeatedly added to the fractional parts and whenever the results overflow, the corresponding
integer part is incremented. The integer parts of the x and y registers are used in plotting the line.
In the case of the symmetrical DDA, we choose ε=2-n,where 2n-1≤max (|∆x|,|∆y|)<2π
A line drawn with the symmetrical DDA is shown in fig:
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING

1. #include<graphics.h>
2. #include<conio.h>
3. #include<stdio.h>
4. void
main() 5. {
6. int gd = DETECT ,gm, i;
7. float x,
y,dx,dy,steps; 8. int
x0,x1,y0,y1;
9. initgraph(&gd, &gm, "C:\\TC\\BGI");
10. setbkcolor(WHITE);
11. x0 = 100 , y0 = 200, x1 = 500, y1 = 300;
12. dx = (float)(x1 - x0);
13. dy = (float)(y1 - y0);
14. if(dx>=dy)
15. {
16. steps = dx;
17. }
18. else
19. {
20. steps = dy;
21. }
22. dx = dx/steps;
23. dy = dy/steps;
24. x = x0;
25. y = y0;
26. i = 1;
27. while(i<= steps)
28. {
29. putpixel(x, y, RED);
30. x += dx;
31. y += dy;
32. i=i+1;
33. }
34. getch();
35. closegraph();
36. }
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING

Output:
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING

Practical No. 2
Implement Bresenham's algorithms for line drawing and compare it with DDA to write your
comments on its relative performance based on experimental data.

// C++ program for Bresenham’s Line Generation


// Assumptions :
// 1) Line is drawn from left to right.
// 2) x1 < x2 and y1 < y2
// 3) Slope of the line is between 0 and 1.
// We draw a line from lower left to upper right.
#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;
}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
Output :

(3,2)
(4,3)
(5,3)
(6,3)
(7,3)
(8,4)
(9,4)
(10,4)
(11,4)
(12,5)
(13,5)
(14,5)
(15,5)

Key Differences Between DDA and Bresenham line drawing algorithm


1. Bresenham’s algorithm is more efficient and accurate than DDA algorithm.
2. The DDA algorithm involves floating point values while in the Bresenham algorithm only
integer values are included. This is the major reason that made the computations in DDA
more difficult than the bresenham algorithm.
3. DDA uses multiplication and division operations. As against, bresenham involves addition
and subtraction causing less consumption of time. Therefore, DDA is slower than
bresenham.
4. The values in DDA never rounded off. In contrast, bresenham rounds off the value to the
closest integer value.
5. The Bresenham algorithm is optimized. Conversely, DDA is not and less
expensive.

Conclusion
The Bresenhem line drawing algorithm is more efficient and better in all aspects than the DDA
algorithm which is not that efficient.
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
Practical No. 3
Implement the midpoint circle generating algorithm and compare it with DDA

// C program for implementing


// Mid-Point Circle Drawing Algorithm
#include<stdio.h>

// Implementing Mid-Point Circle Drawing Algorithm


void midPointCircleDraw(int x_centre, int y_centre, int r)
{
int x = r, y = 0;

// Printing the initial point on the axes


// after translation
printf("(%d, %d) ", x + x_centre, y + y_centre);

// When radius is zero only a single


// point will be printed
if (r > 0)
{
printf("(%d, %d) ", x + x_centre, -y + y_centre);
printf("(%d, %d) ", y + x_centre, x + y_centre);
printf("(%d, %d)\n", -y + x_centre, x + y_centre);
}

// Initialising the value of P


int P = 1 - r;
while (x > y)
{
y++;

// Mid-point is inside or on the


perimeter if (P <= 0)
P = P + 2*y + 1;

// Mid-point is outside the perimeter


else
{
x--;
P = P + 2*y - 2*x + 1;
}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
// All the perimeter points have already been printed
if (x < y)
break;

// Printing the generated point and its reflection


// in the other octants after translation
printf("(%d, %d) ", x + x_centre, y + y_centre);
printf("(%d, %d) ", -x + x_centre, y + y_centre);
printf("(%d, %d) ", x + x_centre, -y + y_centre);
printf("(%d, %d)\n", -x + x_centre, -y + y_centre);

// If the generated point is on the line x = y then


// the perimeter points have already been printed
if (x != y)
{
printf("(%d, %d) ", y + x_centre, x + y_centre);
printf("(%d, %d) ", -y + x_centre, x + y_centre);
printf("(%d, %d) ", y + x_centre, -x + y_centre);
printf("(%d, %d)\n", -y + x_centre, -x + y_centre);
}
}
}

// Driver code
int main()
{
// To draw a circle of radius 3 centred at (0, 0)
midPointCircleDraw(0, 0, 3);
return 0;
}

Output:
(3, 0) (3, 0) (0, 3) (0, 3)
(3, 1) (-3, 1) (3, -1) (-3, -1)
(1, 3) (-1, 3) (1, -3) (-1, -3)
(2, 2) (-2, 2) (2, -2) (-2, -2)

By the Mid Point Circle algorithm we can only draw a circle and circle related figures whereas by
DDA we can draw lines and symmetry.
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
Practical No. 4
Implement the algorithms for 2D transformations (translation, rotation, scaling) and
illustrate the importance of concatenation of transformations.

2D Translation:
#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 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();
}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
2D Rotation:

#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,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();
}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
2D Scaling:

#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();
}

The importance of concatenation of transformation is that we can achieve Composite


transformation of matrices to obtain a combined transformation matrix.
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
Practical No. 5
Implement the Sutherland Hodgeman algorithm for clipping a polygon.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float
y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1 >= xmin && x2 >= xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1 < xmin && x2 >= xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1 >= xmin && x2 < xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}

void clipt(float x1,float y1,float x2,float y2)


{
if(y2-y1)
m=(x2-x1)/(y2-y1);
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
else
m=100000;
if(y1 <= ymax && y2 <= ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 > ymax && y2 <= ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1 <= ymax && y2 > ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}

void clipr(float x1,float y1,float x2,float y2)


{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1 <= xmax && x2 <= xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1 > xmax && x2 <= xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
if(x1 <= xmax && x2 > xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}

void clipb(float x1,float y1,float x2,float y2)


{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1 >= ymin && y2 >= ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1 < ymin && y2 >= ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1 >= ymin && y2 < ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}

void main()
{
int gdriver=DETECT,gmode,n,poly[20];
float xi,yi,xf,yf,polyy[20];
clrscr();
cout<<"Coordinates of rectangular clip window :\nxmin,ymin :";
cin>>xmin>>ymin;
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
cout<<"xmax,ymax
cin>>xmax>>ymax;
cout<<"\n\nPolygon to be clipped :\nNumber of sides :";
cin>>n;
cout<<"Enter the coordinates
:"; for(int i=0;i < 2*n;i++)
cin>>polyy[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i < 2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin); cout<<"\t\
tUNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i < 2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i < k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
k=0;
for(i=0;i < 2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i < k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\tCLIPPED POLYGON";
getch();
closegraph();
}

Sample Output:
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
Practical No. 6
Implement the Weiler and Atherton algorithm for clipping a polygon.

If the objects are approximated with straight-line boundary sections, we use boundary sections, we
use a polygon-clipping method, so we will use the Weiler and Atherton algorithm for clipping a
polygon.

#include<conio.h>
#include <graphics.h>
#include<dos.h>
void weiler_polygon_clipping();
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
settextstyle(SMALL_FONT,HORIZ_DIR,4);
clrscr();
outtextxy(50,80,"Weiler-Atherton Polygon Clipping");
cleardevice();
setbkcolor(6);
weiler_polygon_clipping();
cleardevice();
setbkcolor(2);
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 5);
outtextxy(150,150,"THANK YOU");
getch();
closegraph();
}

void weiler_polygon_clipping()
{
rectangle(70,240,180,360);
delay(1100);
line(30,310,110,270);
delay(1100);
line(110,270,100,295);
delay(1100);
line(100,295,50,330);
delay(1100);
line(50,330,110,340);
delay(1100);
line(110,340,30,350);
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
delay(1100);
line(30,310,30,350);
delay(1100);
outtextxy(20,310,"v1");
delay(1100);
outtextxy(110,270,"v2");
delay(1100);
outtextxy(105,295,"v3");
delay(1100);
outtextxy(45,330,"v4");
delay(1100);
outtextxy(115,340,"v5");
delay(1100);
outtextxy(20,350,"v6");
delay(1100);
outtextxy(65,285,"v1'");
delay(1100);
outtextxy(65,305,"v3'");
delay(1100);
outtextxy(75,325,"v4'");
delay(1100);
outtextxy(50,350,"v5'");
outtextxy(50,469,"Hit any key to continue...");
getch();
cleardevice();
rectangle(70,240,180,360);
setcolor(11);
line(70,290,110,270);
line(110,270,100,295);
line(100,295,70,320);
line(70,290,70,320);
delay(2000);
line(70,330,110,340);
line(70,330,110,340);
line(110,340,70,350);
line(70,330,70,350);
setcolor(15);
outtextxy(50,469,"Hit any key to continue...");
getch();
}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
Practical No. 7
Implement the midpoint subdivision algorithm for clipping a line.

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
#include <stdlib.h>

typedef struct coordinates


{
int x;
int y;
char code[4];
}pt;

pt p1,p2,ptemp;

void drawwindow();
void drawline(pt p1,pt p2, int c1);
pt setcode(pt p);
int visibility(pt p1,pt p2);
pt resetendpt(pt p1,pt p2);
void midsub(pt p1,pt p2);

void main()
{
int gd=DETECT,gm,v;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,”c:\\tc\\bgi”);
cleardevice();
printf(“Enter the endpoint 1\t”);
scanf(“%d %d”,&p1.x,&p1.y);
printf(“\nEnter the endpoint 2\t”);
scanf(“%d %d”,&p2.x,&p2.y);
cleardevice();
drawwindow();
getch();
drawline(p1,p2,15);
getch();
cleardevice();
drawwindow();
midsub(p1,p2);
getch();
closegraph();
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
}

void midsub(pt p1,pt p2)


{
pt mid;
int v;
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(100);
switch(v)
{
case 0:
{
drawline(p1,p2,15);
break;
}
case 1:
{
break;
}
case 2:
{
mid.x=p1.x+(p2.x-p1.x)/2;
mid.y=p1.y+(p2.y-p1.y)/2;
midsub(p1,mid);
mid.x=mid.x+1;
mid.y=mid.y+1;
midsub(mid,p2);
break;
}
}
}

void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,400);
line(450,400,150,400);
line(150,400,150,100);
}

void drawline(pt p1,pt p2,int c1)


{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
pt setcode(pt p)
{
pt ptemp;
if(p.y>400)
ptemp.code[0]=’1′;
else
ptemp.code[0]=’0′;
if(p.y<100) 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);
}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING

After Clipping;
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
Practical No. 8
Implementation of text compression.

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char a[50],b[50],dl;
int i,j=0,flag=0,countindex=0,index=-1,count=0;
FILE *ip,*op;
clrscr();
printf("Enter the text for input\t");
scanf("%s",a);
ip=fopen("source","w");
fprintf(ip,"%s",a);
printf("\n input file length=\t");
printf("%d",ftell(ip));
fclose(ip);
printf("\n");
ip=fopen("compress","w");
for(i=0;i{
j=i+1;
if(a[j]==a[i])
count=count+1;
else
if(count>0)
{
b[++index]=a[i];
fprintf(ip,"%c",b[index]);
b[++index]='#';
fprintf(ip,"%c",b[index]);
b[++index]=count+1;
fprintf(ip,"%c",b[index]);
count=0;
}
else
{
b[++index]=a[i];
fprintf(ip,"%c",b[index]);
}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
countindex=index;
}
fclose(ip);
op=fopen("compress","r+");
printf("\n Compressed output\n");
for(i=0;i<size;i++)
{
fscanf(op,"%c",b[i]);
if(b[i]=='#')
{
flag=1;
printf("%c",b[i]);
continue;
}
else if(flag==1)
{
printf("%d",b[i]);
flag=0;
}
else
printf("%c",b[i]);
}
printf("\n Compressed file length\t");
printf("%d",ftell(op));
fclose(op);
flag=0;
op=fopen("comperss","r+");
ip=fopen("decompress","w");
printf("\n decompressed output\n");
for(i=0;i<=countindex;i++)
{
fscanf(op,"%c",b[i]);
if(b[i]=='#')
{
flag=1;
continue;
}
else if(flag==1)
{
for(j=0;j{ fprintf(i
p,"%c",dl);
printf("%c",dl);
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
}
flag=0;
}
else if(b[i]!='\0')
{
fprintf(ip,"%c",b[i]);
printf("%c",b[i]);
dl=b[i];
}
}
printf("\n Decompressed length");
printf("%d",ftell(ip));
fclose(ip);
fclose(op);
getch();
return;
}

Output:
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
Practical No. 9
Implement using the DDA for circle generation. Show that the true DDA will generate spiral
rather than a circle.

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>

//function prototype for DDA circle function


void dda_circle(int xc,int yc,int r);

int main()
{
int xc,yc,r,gd,gm;
clrscr();
//taking circle parameters from the user
cout<<"\nEnter the Center point (xc,yc) :";
cin>>xc>>yc;
cout<<"\nEnter the Radius
:"; cin>>r;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(10);

//drawing the coordinate


axes line(0,240,640,240);
line(320,0,320,480);
setcolor(5);

//converting the screen coordinates to more user friendly coordinates


// with respect to the centre of the
screen. xc = 320 + xc;
yc = 240 - yc;

//function call for DDA circle.


dda_circle(xc,yc,r);
getch();
return 0;

}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
/*
Function Definition for DDA circle

Function: dda_circle() Draws the circle with given parameters using DDA algorithm.
Input: Takes three input
1) x coordinate for centre
2) y coordinate for centre
3) radius of the circle

Return : This function does not return anything.


*/

void dda_circle(int xc,int yc,int r)


{
float xc1,xc2,yc1,yc2,eps,sx,sy;
int val,i;
xc1=r;
yc1=0;
sx=xc1;
sy=yc1;
i=0;
do
{
val=pow(2,i);
i++;
}
while(val<r);
eps = 1/pow(2,i-1);
do
{
xc2 = xc1 +
yc1*eps; yc2 = yc1 -
eps*xc2;
putpixel(xc+xc2,yc-yc2,3);
xc1=xc2;
yc1=yc2;
}
while((yc1-sy)<eps || (sx-xc1)>eps);

}
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING

Output
Image Of Getting Coordinates

Final Output

Since, DDA is made for Line Generations and it has disadvantage of poor End Point accuracy so
while generating Circle and we have it’s closer look it generates a spiral.
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING

Practical No. 10
Implement the algorithms for 2D windowing and viewing transformations

// C program to implement
// Window to ViewPort Transformation
#include <stdio.h>
// Function for window to viewport transformation
void WindowtoViewport(int x_w, int y_w, int x_wmax,int y_wmax, int x_wmin, int y_wmin, int
x_vmax, int y_vmax, int x_vmin, int y_vmin)
{
// point on viewport
int x_v, y_v;

// scaling factors for x coordinate and y


coordinate float sx, sy;

// calculating Sx and Sy
sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);

// calculating the point on viewport


x_v = x_vmin + (float)((x_w - x_wmin) * sx);
y_v = y_vmin + (float)((y_w - y_wmin) * sy);

printf("The point on viewport: (%d, %d )\n ", x_v, y_v);


}
// Driver Code
void main()
{
// boundary values for window
int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
// boundary values for viewport
int x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 40;
// point on window
int x_w = 30, y_w = 80;
WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
}

Output:
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
The point on viewport: (35, 60)

You might also like