Computer Graphics Practical
Computer Graphics Practical
Submitted by,
Sudhashnu Prusty
College Roll No: 10906
BSc (Hons) Computer Science0
Keshav Mahavidyalaya
1. Write a program to implement Bresenham’s line drawing algorithm.
#include<bits/stdc++.h>
#include<graphics.h>
int dx,dy,P,x,y;
int xmid=getmaxx()/2;
int ymid=getmaxy()/2;
dx=x2-x1; dy=y2-y1;
x=x1; y=y1;
P=2*dy-dx;
while(x<=x2)
if(P>=0)
putpixel(x,y,YELLOW);
y=y+1;
P=P+2*dy-2*dx;
else
putpixel(x,y,YELLOW);
P=P+2*dy;}
x=x+1;
1
int main()
initgraph(&gdriver,&gmode,"C:\\Dev-Cpp\\lib");
setbkcolor(BLACK); cleardevice();
int x1,x2,y1,y2;
cin>>x1;
cin>>y1;
cin>>x2;
cin>>y2;
cleardevice();
bresline(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
getch();
closegraph();
return 0;
2
2. Write a program to implement mid-point circle drawing algorithm.
#include<iostream>
#include<graphics.h>
#include<math.h>
3
{
int x = 0;
int y = radius;
int p = 1 - radius;
while (x <= y)
if (p < 0)
p += (2*x)+1;
else
p +=(2*(x-y))+1;
y--;
x++ ;
4
}
int main()
int x , y;
float r;
cin>>x;
cin>>y;
cin>>r;
xmid = getmaxx()/2;
getch();
closegraph();
return 0;
5
3. Write a program to clip a line using Cohen and Sutherland line clipping
algorithm.
#include <iostream>
#include<graphics.h>
#include<math.h>
6
// Defining x_max, y_max and x_min, y_min for clipping rectangle.
int code = 0;
if (y > y_max)
code |= TOP;
code |= BOTTOM;
if (x > x_max)
code |= RIGHT;
code |= LEFT;
return code;
double y2)
7
do
if (!(code1 | code2))
accept = true;
done = true;
break;
break;
else
*/
double x, y;
int code_out;
y = y_max;
8
}
y = y_min;
x = x_max;
x = x_min;
if (code_out == code1){
x1 = x;
y1 = y;
else
x2 = x;
y2 = y;
9
code2 = ComputeOutCode(x2, y2);
while(done == false);
if (accept)
cout << "Line accepted from (" << x1 << ", " << y1 << ") to (" << x2
setcolor(RED);
else
// Driver code
int main(){
float x_mid = X / 2;
float y_mid = Y / 2;
setcolor(WHITE);
setcolor(YELLOW);
10
line(x_max, y_max, x_min, y_max);
setcolor(GREEN);
setcolor(GREEN);
setcolor(GREEN);
getch();
closegraph();
return 0;
11
4. Write a program to clip a polygon using Sutherland Hodgeman algorithm.
#include<iostream>
#include<conio.h>
#include<graphics.h>
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
12
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
if(y2-y1)
m=(x2-x1)/(y2-y1);
13
else
m=100000;
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;}
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
14
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=xmax;arr[k+1]=y1+m*(xmax-x1);
k+=2;
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
arr[k]=x2;
arr[k+1]=y2;
15
k+=2;
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
int main()
int gd=DETECT,gm,n,poly[20];
initgraph(&gd,&gm,(char*)"");
float xi,yi,xf,yf,polyy[20];
cin>>xmin>>ymin;
cout<<"xmax,ymax :";
cin>>xmax>>ymax;
cin>>n;
int i;
16
for(i=0;i < 2*n;i++)
cin>>polyy[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\t\tUNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
17
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\tCLIPPED POLYGON";
getch();
closegraph();
18
5. Write a program to fill a polygon using Scan line fill algorithm.
#include<iostream>
#include<graphics.h>
#include<math.h>
int x;
19
int y;
}dcPt;
int yUpper;
}Edge;
dcPt vertex[5] = {{200, 500}, {300, 250}, {270, 230}, {320, 200}, {360, 290}};
p = q->next;
while (p != NULL)
p = NULL;
else
q = p;
p = p->next;
edge->next = q->next;
q->next = edge;
int j;
20
if ((k + 1) > (cnt - 1))
j = 0;
else
j = k + 1;while(pts[k].y == pts[j].y)
j = 0;
else
j++;
return (pts[j].y);
void makeEdgeRec(dcPt lower, dcPt upper, int yComp, Edge *edge, Edge*edges[])
edge->xIntersect = lower.x;
edge->yUpper = upper.y - 1;
else
edge->yUpper = upper.y;
insertEdge(edges[lower.y], edge);
Edge *edge;
21
v2 = pts[i];
yPrev = v1.y;
v1 = v2;
p = edges[scan]->next;while (p)
q = p->next;
insertEdge(active, p);
p = q;
int i;
p1 = active->next;
while (p1)
22
{
p2 = p1->next;
p1 = p2->next;
free(p);
while (p)
p = p->next;
deleteAfter(q);
else
q = p;
p = p->next;
23
}
active->next = NULL;
while (p)
q = p->next;
insertEdge(active, p);
p = q;
int i, scan;
edges[i]->next = NULL;
active->next = NULL;
if (active->next)
fillScan(scan, active);
updateActiveList(scan, active) ;
24
resortActiveList(active);
free(edges[WINDOW_HEIGHT]);
free(active);
int main()
float x_mid = X / 2;
float y_mid = Y / 2;
cleardevice();
scanFill(5, vertex);
getch();closegraph();
return 0;
25
6. Write a program to apply various 2D transformations on a 2D object (use
homogenous Coordinates).
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<conio.h>
#include<math.h>
int mat[3][3];
int dx , dy , st;
dx = x2 - x1;
dy = y2 - y1;
xmid = getmaxx()/2;
ymid = getmaxy()/2;
st = abs(dx);
else{
st = abs(dy);
}xinc = dx / st;
yinc = dy / st;
x = x1;
y = y1;
x += xinc;
26
y += yinc;
}}
void rotate(){
xmid = getmaxx()/2;
ymid = getmaxy()/2;
int c[3][2] ,l , m, i , j , k;
int a[3][2]={{200,200},{200,100},{100,200}};
int t[2][2]={{0,1},{-1,0}};
c[i][j]=0;
dda_line(a[0][0],a[0][1],a[1][0],a[1][1],YELLOW);
dda_line(a[1][0],a[1][1],a[2][0],a[2][1],YELLOW);dda_line(a[2][0],a[2][1],a[0][0],a[0][1],YELLOW);
for ( i=0;i<3;i++){
for ( j=0;j<2;j++){
for ( k=0;k<2;k++){
c[i][j]=c[i][j]+(a[i][k]*t[k][j]);
dda_line(c[0][0],c[0][1],c[1][0],c[1][1],GREEN);
dda_line(c[1][0],c[1][1],c[2][0],c[2][1],GREEN);
dda_line(c[2][0],c[2][1],c[0][0],c[0][1],GREEN);
27
}
void reflection(){
xmid = getmaxx()/2;
ymid = getmaxy()/2;
int c[3][2] ,l , m, i , j , k;
int a[3][2]={{200,200},{200,100},{100,200}};
int t[2][2]={{0,-1},{-1,0}};
c[i][j]=0;
} }dda_line(a[0][0],a[0][1],a[1][0],a[1][1],YELLOW);
dda_line(a[1][0],a[1][1],a[2][0],a[2][1],YELLOW);
dda_line(a[2][0],a[2][1],a[0][0],a[0][1],YELLOW);
for ( i=0;i<3;i++){
for ( j=0;j<2;j++){
for ( k=0;k<2;k++){
c[i][j]=c[i][j]+(a[i][k]*t[k][j]);
}}}
dda_line(c[0][0],c[0][1],c[1][0],c[1][1],GREEN);
dda_line(c[1][0],c[1][1],c[2][0],c[2][1],GREEN);
dda_line(c[2][0],c[2][1],c[0][0],c[0][1],GREEN);
void scaling(){
xmid = getmaxx()/2;
ymid = getmaxy()/2;
28
line(xmid , 0 , xmid , getmaxy());
int c[3][2] ,l , m, i , j , k;
int a[3][2]={{20,20},{20,10},{10,20}};
int t[2][2]={{5,0},{0,5}};
c[i][j]=0;} }
dda_line(a[0][0],a[0][1],a[1][0],a[1][1],YELLOW);
dda_line(a[1][0],a[1][1],a[2][0],a[2][1],YELLOW);
dda_line(a[2][0],a[2][1],a[0][0],a[0][1],YELLOW);
for ( i=0;i<3;i++){
for ( j=0;j<2;j++){
for ( k=0;k<2;k++){
c[i][j]=c[i][j]+(a[i][k]*t[k][j]);
}}}
dda_line(c[0][0],c[0][1],c[1][0],c[1][1],GREEN);
dda_line(c[1][0],c[1][1],c[2][0],c[2][1],GREEN);
dda_line(c[2][0],c[2][1],c[0][0],c[0][1],GREEN);
int i , j ,k;
int c[3][3];
c[i][j]=0;
}}
for ( i=0;i<3;i++){
29
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}}}
mat[i][j]=c[i][j];
}}}
void reflection_arbitrary(){
xmid = getmaxx()/2;
ymid = getmaxy()/2;
int a[3][3]={{200,200,1},{200,100,1},{100,200,1}};
int t[3][3]={{1,0,0},{0,1,0},{0,0,1}};
int r[3][3]={{-1,0,0},{0,-1,0},{0,0,1}};
int ref[3][3]={{1,0,0},{0,-1,0},{0,0,1}};
int rinv[3][3]={{-1,0,0},{0,-1,0},{0,0,1}};
int tinv[3][3]={{1,0,0},{0,1,0},{0,1,1}};
dda_line(a[0][0],a[0][1],a[1][0],a[1][1],YELLOW);
dda_line(a[1][0],a[1][1],a[2][0],a[2][1],YELLOW);
dda_line(a[2][0],a[2][1],a[0][0],a[0][1],YELLOW);multi(t,r);
multi(mat,ref);
multi(mat,rinv);
multi(mat,tinv);
multi(a,mat);
dda_line(mat[0][0],mat[0][1],mat[1][0],mat[1][1],GREEN);
dda_line(mat[1][0],mat[1][1],mat[2][0],mat[2][1],GREEN);
dda_line(mat[2][0],mat[2][1],mat[0][0],mat[0][1],GREEN);
30
void rotation_arbitrary(){
xmid = getmaxx()/2;
ymid = getmaxy()/2;
int c[3][3] , i , j , k;
int l[1][3]={{200,200,1}};
int a[3][3]={{200,200,1},{200,100,1},{100,200,1}};
int t[3][3]={{1,0,0},{0,1,0},{-133,-133,1}};
int r[3][3]={{-1,0,0},{0,-1,0},{0,0,1}};
int tinv[3][3]={{1,0,0},{0,1,0},{133,133,1}};
dda_line(a[0][0],a[0][1],a[1][0],a[1][1],YELLOW);
dda_line(a[1][0],a[1][1],a[2][0],a[2][1],YELLOW);
dda_line(a[2][0],a[2][1],a[0][0],a[0][1],YELLOW);
multi(t,r);
c[i][j]=0;
}}
for ( i=0;i<3;i++){
for ( j=0;j<3;j++){
for ( k=0;k<3;k++){
c[i][j]=c[i][j]+(a[i][k]*mat[k][j]);
}}}
dda_line(c[0][0],c[0][1],c[1][0],c[1][1],GREEN);
dda_line(c[1][0],c[1][1],c[2][0],c[2][1],GREEN);
dda_line(c[2][0],c[2][1],c[0][0],c[0][1],GREEN);
31
int main()
int n , m;
cin>>n;
switch(n){
case 1 : rotate();
break;
case 2 : reflection();
break;
case 3 : scaling();
break;
case 4 : reflection_arbitrary();
break;
case 5 : rotation_arbitrary();
break;
getch();
32
33
7. Write a program to apply various 3D transformations on a 3D object and
then apply parallel and perspective projection on it.
#include <iostream>
#include <direct.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <graphics.h>
#include <process.h>
34
double y1;
int i;
clearviewport();
getch();
closegraph();
double a, b, c;
int i;
clearviewport();
35
edge[i][0] = edge[i][0] * a;
edge[i][1] = edge[i][1] * b;
edge[i][2] = edge[i][2] * c;
draw_cube(edge);
closegraph();
int a, b, c;
int i;
clearviewport();
edge[i][0] += a;
edge[i][0] += b;
edge[i][0] += c;
draw_cube(edge);
closegraph();
int ch;
36
int i;
switch (ch)
case 1:
edge[i][0] = edge[i][0];
temp = edge[i][1];
temp1 = edge[i][2];
draw_cube(edge);
break;
case 2:
37
for (i = 0; i < 20; i++)
edge[i][1] = edge[i][1];
temp = edge[i][0];
temp1 = edge[i][2];
draw_cube(edge);
break;
case 3:
edge[i][2] = edge[i][2];
temp = edge[i][0];
temp1 = edge[i][1];
draw_cube(edge);
break;
38
{
int ch;
int i;
switch (ch)
case 1:
edge[i][0] = edge[i][0];
edge[i][1] = -edge[i][1];
edge[i][2] = -edge[i][2];
draw_cube(edge);
break;
case 2:
edge[i][1] = edge[i][1];
edge[i][0] = -edge[i][0];
edge[i][2] = -edge[i][2];
draw_cube(edge);
39
break;
case 3:
edge[i][2] = edge[i][2];
edge[i][0] = -edge[i][0];
edge[i][1] = -edge[i][1];
draw_cube(edge);
break;
int ch;
int i;
double p, q, r;
switch (ch)
case 1:
40
cin >> p;
draw_cube(edge);
break;
case 2:
cin >> q;
draw_cube(edge);
break;
case 3:
cin >> r;
41
edge[i][1] = edge[i][1] / (edge[i][2] * r + 1);
draw_cube(edge);
break;
closegraph();
int main()
int choice;
double edge[20][3] = {
100, 0, 0,
100, 100, 0,
0, 100, 0,
0, 100, 100,
0, 0, 100,
0, 0, 0,
100, 0, 0,
100, 0, 100,
100, 100, 0,
0, 100, 100,
0, 100, 0,
42
0, 0, 0,
0, 0, 100,
100, 0, 100};
while (1)
switch (choice)
case 1:
draw_cube(edge);
break;
case 2:
scale(edge);
break;
case 3:
rotate(edge);
break;
case 4:
43
reflect(edge);
break;
case 5:
translate(edge);
break;
case 6:
perspect(edge);
break;
case 7:
exit(0);
default:
getch();
break;
closegraph();
return 0;
44
45
46
47
8. Write a program to draw Hermite /Bezier curve.
Hermite Curve
#include <conio.h>
#include <graphics.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct point
int x, y;
};
float x, y, t;
(pow(t, 3) - 2 * pow(t, 2) + t) * r1 +
(pow(t, 3) - 2 * pow(t, 2) + 1) * r1 +
48
(pow(t, 3) - pow(t, 2)) * r4;
putpixel(x, y, WHITE);
int main()
getch();
closegraph();
49
}
Bezier Curve
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
int main()
int x[4],y[4],i;
50
double put_x,put_y,t;
int gr=DETECT,gm;
initgraph(&gr,&gm,NULL);
for(i=0;i<4;i++)
scanf("%d%d",&x[i],&y[i]);
getch();
closegraph();
return 0;
51
52