Computer Graphics Lab Report
Computer Graphics Lab Report
Problem
Problems Pages
No.
Write a program to implement Bresenham's line drawing
01 1–3
algorithm.
Write a program to implement DDA (Digital Differential
02 4–6
Analyzer) line drawing algorithm.
Write a program to implement Bresenham's circle drawing
03 7 – 10
algorithm.
Write a program to implement Midpoint line drawing
04 11 – 12
algorithm.
Write a program to implement Polygon (Rectangle) filling
05 13 – 14
algorithm.
Write a program to implement Midpoint ellipse drawing
06 15 – 17
algorithm.
Write a program to implement 2D transformations (Translation,
07 18 – 21
Rotation).
Write a program to implement 2D transformations (Scaling.
08 22 – 25
Rotation).
Write a program to implement composite (Translation,
09 26 – 29
Rotation) transformation.
Write a program to implement Cohen-Sutherland line clipping
10 30 – 33
algorithm.
Write a program to implement Sutherland-Flodgmen polygon
11 34 – 41
line clipping algorithm.
Problem 1: Write a program to implement Bresenham's line drawing algorithm
Solution: This algorithm is used for scan converting a line. It was developed by
Bresenham. It is an efficient method because it involves only integer addition,
subtractions, and multiplication operations. These operations can be performed very
rapidly so lines can be generated quickly.
In this method, next pixel selected is that one who has the least distance from true line.
Algorithm:
Step1: Start
Step2: Declare x1, y1, x2, y2.
Step3: Calculate dx = x2 - x1
dy = y2 - y1
Step 4: Calculate slope, m = dy / dx.
Step5: For m < 1: Calculate initial decision variable: P = 2dy - dx.
Step 6: while (x1 <= x2)
if(P < 0):
xk = xk + 1
P = P + 2dy
yk = yk
else :
xk = xk + 1
P = P + 2dy - 2dx
yk = yk + 1
Step 7: Plot ( xk , yk )
Step 8: End
1 | Page
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int x,y,x1,y1,x2,y2,dx,dy,p,i;
int gd=DETECT,gm;
initgraph(&gd,&gm,(char*)"");
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
x=x1;
y=y1;
i=0;
while(i<=dx)
{
putpixel(x,y,YELLOW);
if(p<0)
{
x=x+1;
p=p+2*dy;
}
else
{
x=x+1;
y=y+1;
p=p+2*dy-2*dx;
}
i++;
delay(100);
}
2 | Page
getch();
closegraph();
return 0;
}
Output:
3 | Page
Problem 2: Write a program to implement DDA(Digital Differential Analyzer) Line
Drawing Algorithm
Source Code:
#include <graphics.h>
#include <stdio.h>
#include <math.h>
int main( )
{
float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i,gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,1);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
5 | Page
Output:
6 | Page
Problem 3: Write a program to implement Bresenham's circle drawing algorithm.
Algorithm:
Step1: Start Algorithm
Step2: Declare p, q, x, y, r, d variables
p, q are coordinates of the center of the circle
r is the radius of the circle
Step3: Enter the value of r
Step4: Calculate d = 3 - 2r
Step5: Initialize x=0
&nbsy= r
Step6: Check if the whole circle is scan converted
If x > = y
Stop
Step7: Plot eight points by using concepts of eight-way symmetry. The center is at (p,
q). Current active pixel is (x, y).
putpixel (x+p, y+q)
putpixel (y+p, x+q)
putpixel (-y+p, x+q)
putpixel (-x+p, y+q)
putpixel (-x+p, -y+q)
putpixel (-y+p, -x+q)
putpixel (y+p, -x+q)
putpixel (x+p, -y-q)
Step8: Find location of next pixels to be scanned
If d < 0
then d = d + 4x + 6
increment x = x + 1
If d ≥ 0
7 | Page
then d = d + 4 (x - y) + 10
increment x = x + 1
decrement y = y - 1
Step9: Go to step 6
Step10: Stop Algorithm
Source Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)-(4*y)+10;
y=y-1;
}
8 | Page
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
}
}
int main(void)
{
/* request auto detection */
int xc,yc,r,gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
getch();
closegraph();
return 0;
}
9 | Page
Output:
10 | Page
Problem 4: Write a program to implement Midpoint line drawing algorithm.
Source Code:
#include<bits/stdc++.h>
using namespace std;
if(dy<=dx){
int d = dy - (dx/2);
int x = X1, y = Y1;
cout << x << "," << y << "\n";
while (x < X2)
{
x++;
if (d < 0)
d = d + dy;
else
{
d += (dy - dx);
y++;
}
cout << x << "," << y << "\n";
}
}
else if(dx<dy)
{
int d = dx - (dy/2);
int x = X1, y = Y1;
cout << x << "," << y << "\n";
int main()
{
int X1 = 2, Y1 = 2, X2 = 8, Y2 = 5;
midPoint(X1, Y1, X2, Y2);
return 0;
}
Output:
2,2
3,3
4,3
5,4
6,4
7,5
8,5
12 | Page
Problem 5: Write a program to implement Polygon (Rectangle) filling algorithm.
Source Code:
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int gdriver, gmode;
poly[2] = 120;
poly[3] = 140; /* 2nd vertex */
poly[4] = 240;
poly[5] = 260; /* 3rd vertex */
poly[6] = 120;
poly[7] = 320; /* 4th vertex */
poly[8] = poly[0];
poly[9] = poly[1];
setfillstyle(SOLID_FILL, RED);
closegraph();
return 0;
Output:
14 | Page
Problem 6: Write a program to implement Midpoint ellipse drawing algorithm.
Source Code:
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
class bresen
{
float x,y,a, b,r,p,h,k,p1,p2;
public:
void get ();
void cal ();
};
void main ()
{
bresen b;
b.get ();
b.cal ();
getch ();
}
void bresen :: get ()
{
cout<<"\n ENTER CENTER OF ELLIPSE";
cout<<"\n ENTER (h, k) ";
cin>>h>>k;
cout<<"\n ENTER LENGTH OF MAJOR AND MINOR AXIS";
cin>>a>>b;
}
void bresen ::cal ()
{
/* request auto detection */
int gdriver = DETECT,gmode, errorcode;
int midx, midy, i;
/* initialize graphics and local variables */
initgraph (&gdriver, &gmode, " ");
/* read result of initialization */
errorcode = graphresult ();
if (errorcode ! = grOK) /*an error occurred */
{
printf("Graphics error: %s \n", grapherrormsg
(errorcode);
15 | Page
printf ("Press any key to halt:");
getch ();
exit (1); /* terminate with an error code */
}
x=0;
y=b;
// REGION 1
p1 =(b * b)-(a * a * b) + (a * a)/4);
{
putpixel (x+h, y+k, RED);
putpixel (-x+h, -y+k, RED);
putpixel (x+h, -y+k, RED);
putpixel (-x+h, y+k, RED);
if (p1 < 0)
p1 += ((2 *b * b) *(x+1))-((2 * a *
a)*(y-1)) + (b * b);
else
{
p1+= ((2 *b * b) *(x+1))-((2 * a *
a)*(y-1))-(b * b);
y--;
}
x++;
}
//REGION 2
p2 =((b * b)* (x + 0.5))+((a * a)*(y-1) * (y-1))-(a
* a *b * b);
while (y>=0)
{
If (p2>0)
p2=p2-((2 * a * a)* (y-1))+(a *a);
else
{
p2=p2-((2 * a * a)* (y-1))+((2 * b *
b)*(x+1))+(a * a);
x++;
}
y--;
putpixel (x+h, y+k, RED);
putpixel (-x+h, -y+k, RED);
putpixel (x+h, -y+k, RED);
putpixel (-x+h, y+k, RED);
}
getch();
}
16 | Page
Output:
ENTER CENTER OF ELLIPSE: 319 239
ENTER LENGTH OF MAJOR AND MONOR AXIS: 50 40
ENTER STEP SIZE: .50
17 | Page
Problem 7: Write a program to implement 2D transformations (Translation, Rotation)
Solution:
Translation:
Source Code:
#include<stdio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm;
int l[2][2],v[2]={10,15},i=0,j;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the initial and final coordinates of a
line ");
while(i<2)
{
printf("x%d and y%d = ",i,i);
j=0;
scanf("%d",&l[i][j]);
scanf("%d",&l[i][j+1]);
i++;
}
// Line before translation
line(l[0][0],l[0][1],l[1][0],l[1][1]);
setcolor(BLUE);
// Line after translation
line(l[0][0]+v[0],l[0][1]+v[1],l[1][0]+v[0],l[1][1]+v[1]
); // Adding Translation vector in it to change the
position
getch();
closegraph();
}
18 | Page
Output:
Enter the initial and final coordinates of a line x0 and y0 = 200 300
x1 and y1 = 350 400
19 | Page
Rotation:
Source Code:
#include <math.h>
#include <iostream>
#include <conio.h>
#include <graphics.h>
using namespace std;
int main()
{
int gd=DETECT,gm,x1,x2,y1,y2,x4,y4;
initgraph(&gd,&gm,"C:\\Tc\\BGI");
float angle=0,ang;
cout<<"Enter the first coordinate of a line:";
cin>>x1>>y1;
cout<<"Enter the second coordinate of a line:";
cin>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"Enter the angle:";
cin>>ang;
angle=(ang*3.14)/180;
setcolor(RED);
x4=x2-(((x2-x1)*cos(angle))-((y2-y1)+sin(angle)));
y4=y2-(((x2-x1)*sin(angle))+((y2-y1)*cos(angle)));
line(x2,y2,x4,y4);
getch();
closegraph();
}
20 | Page
Output:
Enter the first coordinate of a line:55 88
Enter the second coordinate of a line:158 255
Enter the angle: 45
21 | Page
Problem 8: Write a program to implement 2D transformations (Scaling. Rotation).
Solution:
Scaling:
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main(){
int x,y,x1,y1,x2,y2;
int scl_fctr_x,scl_fctr_y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Please enter first coordinate of Triangle= ");
scanf("%d %d",&x,&y);
printf("\nPlease enter second coordinate of Triangle=
");
scanf("%d %d",&x1,&y1);
printf("\nPlease enter third coordinate of Triangle= ");
scanf("%d %d",&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\nNow Enter Scaling factor x and y= ");
scanf("%d %d",&scl_fctr_x,&scl_fctr_y);
x = x* scl_fctr_x;
x1 = x1* scl_fctr_x;
x2 = x2* scl_fctr_x;
y = y* scl_fctr_y;
y1 = y1* scl_fctr_y;
y2= y2 * scl_fctr_y ;
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}
22 | Page
Output:
Please enter first coordinate of Triangle= 50 100
Please enter second coordinate of Triangle= 50 200
Please enter third coordinate of Triangle= 100 100
Now Enter Scaling factor x and y= 2 2
23 | Page
Rotation:
Source Code:
#include <math.h>
#include <iostream>
#include <conio.h>
#include <graphics.h>
using namespace std;
int main()
{
int gd=DETECT,gm,x1,x2,y1,y2,x4,y4;
initgraph(&gd,&gm,"C:\\Tc\\BGI");
float angle=0,ang;
cout<<"Enter the first coordinate of a line:";
cin>>x1>>y1;
cout<<"Enter the second coordinate of a line:";
cin>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"Enter the angle:";
cin>>ang;
angle=(ang*3.14)/180;
setcolor(RED);
x4=x2-(((x2-x1)*cos(angle))-((y2-y1)+sin(angle)));
y4=y2-(((x2-x1)*sin(angle))+((y2-y1)*cos(angle)));
line(x2,y2,x4,y4);
getch();
closegraph();
}
24 | Page
Output:
Enter the first coordinate of a line:55 88
Enter the second coordinate of a line:158 255
Enter the angle: 45
25 | Page
Problem 9: Write a program to implement composite (Translation, Rotation)
transformation.
Solution:
Translation:
Source Code:
#include <conio.h>
#include <graphics.h>
#include <iostream>
#include <stdio.h>
int gd = DETECT, gm;
int n, xs[100], ys[100], i, ty, tx;
void draw();
void translate();
using namespace std;
int main()
{
cout << "Enter no. of sides in polygon: ";
cin >> n;
cout << "Enter coordinates x, y for each vertex: ";
for (i = 0; i < n; i++) {
cin >> xs[i] >> ys[i];
}
cout << "Enter distances for translation (in x and y
"
"directions): ";
cin >> tx >> ty;
initgraph(&gd, &gm, (char*)"");
cleardevice();
setcolor(RED);
draw();
translate();
setcolor(YELLOW);
draw();
getch();
closegraph();
return 0;
}
void draw()
{
for (i = 0; i < n; i++) {
line(xs[i], ys[i], xs[(i + 1) % n],
ys[(i + 1) % n]);
}
}
26 | Page
void translate()
{
for (i = 0; i < n; i++) {
xs[i] += tx;
ys[i] += ty;
}
}
Output:
Enter no. of sides in polygon: 3
Enter coordinates x, y for each vertex:
50 100
50 200
100 100
Enter distances for translation (in x and y directions): 150 150
27 | Page
Rotation:
Source Code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
int gd=0,gm,x1,y1,x2,y2,x3,y3;
double s,c, angle;
initgraph(&gd, &gm, (char*)"");
setcolor(RED);
cout<<"Enter coordinates of triangle: ";
cin>>x1>>y1;
cin>>x2>>y2;
cin>>x3>>y3;
setbkcolor(WHITE);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
cout<<"Enter rotation angle: ";
cin>>angle;
c = cos(angle *M_PI/180);
s = sin(angle *M_PI/180);
x1 = floor(x1 * c + y1 * s);
y1 = floor(-x1 * s + y1 * c);
x2 = floor(x2 * c + y2 * s);
28 | Page
y2 = floor(-x2 * s + y2 * c);
x3 = floor(x3 * c + y3 * s);
y3 = floor(-x3 * s + y3 * c);
setcolor(GREEN);
line(x1, y1 ,x2, y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
getch();
closegraph();
return 0;
}
Output:
Enter coordinates of triangle: 50 100
50 200
100 100
Enter rotation angle: 45
29 | Page
Problem 10: Write a program to implement Cohen-Sutherland line clipping algorithm.
Source Code:
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
using namespace std;
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
int 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);
30 | Page
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);
}
Output:
Enter x1 and y1: 100 100
Enter x2 and y2: 200 200
33 | Page
Problem 11: Write a program to implement Cohen-Sutherland line clipping algorithm.
Source Code:
#include <iostream>
#include <conio.h>
#include <graphics.h>
using namespace std;
int xl,yl,xh,yh,poly[100],n;
void left_clip(){
int temp[100],i,j=0,count=0,x1,y1,x2,y2;
for(i=0;i<2*n;i+=2){
x1 = poly[i];
y1 = poly[i+1];
x2 = poly[i+2];
y2 = poly[i+3];
if(x1<xl && x2<xl){
//both points outside. Do not store any vertices
}else if(x1>xl && x2>xl){
//both points inside. Store second vertex
temp[j] = x2;
temp[j+1] = y2;
j+=2;
count++;
}else if(x1<xl && x2>xl){
//outside to inside. Store intersection n second vertex
int x=xl;
int y= y1 + (xl-x1)*(float)(y2-y1)/(x2-x1);
temp[j]=x;
temp[j+1]=y;
34 | Page
temp[j+2]=x2;
temp[j+3]=y2;
j+=4;
count+=2;
}else{
//inside to outside. Store intersection only
int x=xl;
int y= y1 + (xl-x1)*(float)(y2-y1)/(x2-x1);
temp[j] = x;
temp[j+1] = y;
j+=2;
count++;
}
}
n=count;
//store 1st vertex as last
temp[j]=temp[0];
temp[j+1]=temp[1];
for(i=0;i<2*(n+1);i++)
poly[i]=temp[i];
}
void right_clip(){
int temp[100],i,j=0,count=0,x1,y1,x2,y2;
for(i=0;i<2*n;i+=2){
x1 = poly[i];
y1 = poly[i+1];
x2 = poly[i+2];
y2 = poly[i+3];
if(x1>xh && x2>xh){
35 | Page
//both points outside. Do not store any vertices
}else if(x1<xh && x2<xh){
//both points inside. Store second vertex
temp[j] = x2;
temp[j+1] = y2;
j+=2;
count++;
}else if(x1>xh && x2<xh){
//outside to inside. Store intersection n second vertex
int x=xh;
int y= y1 + (xh-x1)*(float)(y2-y1)/(x2-x1);
temp[j]=x;
temp[j+1]=y;
temp[j+2]=x2;
temp[j+3]=y2;
j+=4;
count+=2;
}else{
//inside to outside. Store intersection only
int x=xh;
int y= y1 + (xh-x1)*(float)(y2-y1)/(x2-x1);
temp[j] = x;
temp[j+1] = y;
j+=2;
count++;
}
}
n=count;
//store 1st vertex as last
36 | Page
temp[j]=temp[0];
temp[j+1]=temp[1];
for(i=0;i<2*(n+1);i++)
poly[i]=temp[i];
}
void bottom_clip(){
int temp[100],i,j=0,count=0,x1,y1,x2,y2;
for(i=0;i<2*n;i+=2){
x1 = poly[i];
y1 = poly[i+1];
x2 = poly[i+2];
y2 = poly[i+3];
if(y1>yl && y2>yl){
//both points outside. Do not store any vertices
}else if(y1<yl && y2<yl){
//both points inside. Store second vertex
temp[j] = x2;
temp[j+1] = y2;
j+=2;
count++;
}else if(y1>yl && y2<yl){
//outside to inside. Store intersection n second vertex
int x= x1 + (yl-y1)/((float)(y2-y1)/(x2-x1));
int y= yl;
temp[j]=x;
temp[j+1]=y;
temp[j+2]=x2;
temp[j+3]=y2;
37 | Page
j+=4;
count+=2;
}else{
//inside to outside. Store intersection only
int x= x1 + (yl-y1)/((float)(y2-y1)/(x2-x1));
int y= yl;
temp[j] = x;
temp[j+1] = y;
j+=2;
count++;
}
}
n=count;
//store 1st vertex as last
temp[j]=temp[0];
temp[j+1]=temp[1];
for(i=0;i<2*(n+1);i++)
poly[i]=temp[i];
}
void top_clip(){
int temp[100],i,j=0,count=0,x1,y1,x2,y2;
for(i=0;i<2*n;i+=2){
x1 = poly[i];
y1 = poly[i+1];
x2 = poly[i+2];
y2 = poly[i+3];
if(y1<yh && y2<yh){
//both points outside. Do not store any vertices
38 | Page
}else if(y1>yh && y2>yh){
//both points inside. Store second vertex
temp[j] = x2;
temp[j+1] = y2;
j+=2;
count++;
}else if(y1<yh && y2>yh){
//outside to inside. Store intersection n second vertex
int x= x1 + (yh-y1)/((float)(y2-y1)/(x2-x1));
int y= yh;
temp[j]=x;
temp[j+1]=y;
temp[j+2]=x2;
temp[j+3]=y2;
j+=4;
count+=2;
}else{
//inside to outside. Store intersection only
int x= x1 + (yh-y1)/((float)(y2-y1)/(x2-x1));
int y= yh;
temp[j] = x;
temp[j+1] = y;
j+=2;
count++;
}
}
n=count;
//store 1st vertex as last
temp[j]=temp[0];
39 | Page
temp[j+1]=temp[1];
for(i=0;i<2*(n+1);i++)
poly[i]=temp[i];
}
int main(){
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\TC\BGI");
int i;
setcolor(BLUE);
cout<<"Enter bottom left and top right co-ordinates of
window: ";
cin>>xl>>yl>>xh>>yh;
rectangle(xl,yl,xh,yh);
cout<<"Enter the no. of vertices: ";
cin>>n;
for(i=0;i<2*n;i+=2){
cout<<"Enter co-ordinates of vertex "<<(i/2+1)<<": ";
cin>>poly[i]>>poly[i+1];
}
//store 1st vertex as last
poly[2*n] = poly[0];
poly[2*n+1] = poly[1];
drawpoly(n+1,poly);
getch();
left_clip();
right_clip();
bottom_clip();
top_clip();
40 | Page
cout<<"After clipping:";
setcolor(WHITE);
drawpoly(n+1,poly);
getch();
closegraph();
}
Output:
Enter bottom left and top right co-ordinates of window: 150
000
1111
0000
Enter the no. of vertices: 3
Enter co-ordinates of vertex 1: 50 100
Enter co-ordinates of vertex 2: 50 200
Enter co-ordinates of vertex 3: 100 100
41 | Page