Computer Graphics (2) (1)
Computer Graphics (2) (1)
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.
(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)
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
// 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();
}
#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 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>
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 drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,400);
line(450,400,150,400);
line(150,400,150,100);
}
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>
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);
}
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
}
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;
// calculating Sx and Sy
sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);
Output:
DEPARTMENT OF COMPUTER SCIENCE &
ENGINEERING
The point on viewport: (35, 60)