CG File
CG File
1
1. Write a program to implement Bresenham’s line drawing algorithm.
Program:
#include<iostream>
#include<graphics.h>
#include<math.h>
int dx = abs(xa-xb);
int dy = abs(ya-yb);
int p = 2*dy-dx;
int x,y,xEnd;
if(xa>xb)
x = xb;
y = yb;
xEnd = xa;
2
}
else
x = xa;
y = ya;
xEnd = xb;
while(x<xEnd)
x++;
if(p<0)
p += two_Dy;
else
y++;
p += two_DyDx;
return 0;
3
int main()
x_mid = X/2;
y_mid = Y/2;
cin>>x1;
cin>>x2;
cin>>y1;
cin>>y2;
//displays a text string in the viewport at the given distance (x,y) using the
current justification settings and the current font, direction and size
setcolor(GREEN);
getch();
//deallocates all the memory allocated by the graphics system, then restores
the screen to the mode it was in before you called initgraph.
closegraph();
return 0;
5
Output:
6
2. Write a program to implement mid-point circle drawing algorithm.
7
Program:
#include<iostream>
#include<graphics.h>
#include<math.h>
int x = 0;
int y = radius;
int p = 1 - radius;
while (x < y)
x++;
if (p < 0)
p += 2 * x + 1;
else
y--;
p += 2 * (x - y) + 1;
// Driver Code
9
int main()
x_mid = X / 2;
y_mid = Y / 2;
int r;
cin >> r;
cout << "\n Circle using Mid-point Circle Algorithm:" << endl;
setcolor(GREEN);
10
outtextxy(x_mid - 60, y_mid - 230, "Y - axis");
getch();
closegraph();
return 0;
Output:
11
12
3. Write a program to clip a line using Cohen and Sutherland line
clipping algorithm.
Program:
#include <iostream>
#include<graphics.h>
#include<math.h>
13
// Function to compute region code for a point(x, y).
int code = 0;
if (y > y_max)
code |= TOP;
code |= BOTTOM;
if (x > x_max)
code |= RIGHT;
code |= LEFT;
return code;
14
// Initialize line as outside the clipping window.
do
if (!(code1 | code2))
accept = true;
done = true;
break;
break;
else
15
/* Failed both tests, so calculate the line segment to clip:
*/
double x, y;
int code_out;
y = y_max;
y = y_min;
16
else if (code_out & RIGHT)
x = x_max;
x = x_min;
if (code_out == code1)
x1 = x;
y1 = y;
else
x2 = x;
17
y2 = y;
} while(done == false);
if (accept)
cout << "\n Line accepted from (" << x1 << ", " << y1 << ") to (" <<
x2 << ", " << y2 << ")" << endl;
setcolor(YELLOW);
else
// Driver code
int main()
18
float X = getmaxx(), Y = getmaxy();
float x_mid = X / 2;
float y_mid = Y / 2;
setcolor(GREEN);
setcolor(RED);
setcolor(BLUE);
setcolor(WHITE);
setcolor(BLUE);
setcolor(WHITE);
19
outtextxy(135, 110, "LINE 2");
setcolor(BLUE);
setcolor(WHITE);
setcolor(BLUE);
setcolor(WHITE);
setcolor(BLUE);
getch();
closegraph();
return 0;
20
Output:
21
4. Write a program to clip a polygon using Sutherland Hodgeman
algorithm.
Program:
#include<iostream>
#include<graphics.h>
struct vertex
float x;
float y;
};
22
int in_out(float x, float y, int x1, int y1, int x2, int y2)
if (p<0)
return 1; //for in
void intersection_line(float &x, float &y, float x1, float y1, float x2, float y2, int
xa, int ya, int xb, int yb)
if (x2==x1)
x = x1;
y = ya - m2*(xa-x1);
else if (xb==xa)
x = xa;
23
y = y1 + m1*(xa-x1);
else
vertex temp[40];
int j=-1;
24
j++;
j++;
j++;
temp[j].x = sp[(i+1)%n_sp].x;
temp[j].y = sp[(i+1)%n_sp].y;
j++;
temp[j].x = sp[(i+1)%n_sp].x;
temp[j].y = sp[(i+1)%n_sp].y;
else
25
}
sp[i].x = temp[i].x;
sp[i].y = temp[i].y;
n_sp = j+1;
int main()
cin>>n_cw;
cin>>cw[i].x>>cw[i].y;
draw_poly(cw, n_cw);
cin>>n_sp;
26
cout<<"Enter vertices (x,y) clockwise"<<endl;
cin>>sp[i].x>>sp[i].y;
draw_poly(sp, n_sp);
char ch;
cin>>ch;
cleardevice();
draw_poly(cw, n_cw);
draw_poly(sp, n_sp);
getch();
closegraph();
return 0;
27
Output:
28
5. Write a program to apply various 2D transformations on a 2D object
(use homogenous Coordinates).
i.Translation
Program:
#include<graphics.h>
#include<iostream>
#include<stdlib.h>
int main()
system("cls");
int gd = DETECT,gm;
29
cin >> ty;
getch();
return 0;
Output:
30
ii.Scaling
Program:
#include<iostream>
#include<conio.h>
#include<graphics.h>
int main()
system("cls");
31
initgraph(&gd, &gm, "c://turboc3//bgi");
getch();
return 0;
Output:
32
iii.Rotation
Program:
#include<iostream>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int main()
system("cls");
33
int gd = DETECT, gm;
float angle;
x1 = x1 * cos(angle) - y1 * sin(angle);
x2 = x2 * cos(angle) - y2 * sin(angle);
x3 = x3 * cos(angle) - y3 * sin(angle);
getch();
return 0;
34
Output:
iv.Reflection
Program:
#include<iostream>
#include<conio.h>
#include<graphics.h>
int main()
35
int gd = DETECT, gm;
int m = getmaxx();
int n = getmaxy();
setcolor(6);
setcolor(5);
setcolor(4);
setcolor(3);
Y2 = y2 + (d*2);
Y1 = y1 + (c*2);
setcolor(9);
X1 = x1 + (a*2);
X2 = x2 + (b*2);
getch();
closegraph();
return 0;
Output:
37
v.Shearing
#include<iostream>
#include<graphics.h>
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
38
line(x3,y3,x1,y1);
void shear(int &x1,int &y1,int &x2,int &y2,int &x3,int &y3,int c,int d){
x1=x1+c*y1;
x2=x2+c*y2;
x3=x3+c*y3;
y1=y1+d*x1;
y2=y2+d*x2;
y3=y3+d*x3;
int main(){
int x1,y1,x2,y2,x3,y3,c,d;
initgraph(&gd,&gm,(char*)"");
cin>>x1>>y1;
cin>>x2>>y2;
cin>>x3>>y3;
39
drawTriangle(x1,y1,x2,y2,x3,y3);
cin>>c>>d;
drawTriangle(x1,y1,x2,y2,x3,y3);
shear(x1,y1,x2,y2,x3,y3,c,d);
getch();
closegraph();
return 0;
Output:
40
41
6. Write a program to apply various 3D transformations on a 3D object
and then apply parallel and perspective projection on it.
Program:
#include<iostream>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#define pi 3.14285714
class projections
double vertices[8][4];
double t_matrix[4][4];
double result[8][4];
public:
projections(){};
void get_vertices();
void display_cube();
void multiplication();
void copyback();
void orthographic();
42
void axonometric(double angle_x,double angle_y);
};
void projections::get_vertices()
for(int i=0;i<8;i++)
cout<<"\nx : ";
cin>>vertices[i][0];
result[i][0]=vertices[i][0];
cout<<"y : ";
cin>>vertices[i][1];
result[i][1]=vertices[i][1];
cout<<"z : ";
cin>>vertices[i][2];
result[i][2]=vertices[i][2];
vertices[i][3]=result[i][3]=1;
}
43
}
void projections::display_cube()
int i=0;
for(i=0;i<3;i++)
line(result[i][0],result[i][1],result[i+1][0],result[i+1][1]);
line(result[i][0],result[i][1],result[0][0],result[0][1]);
for(i=4;i<7;i++)
line(result[i][0],result[i][1],result[i+1][0],result[i+1][1]);
line(result[i][0],result[i][1],result[4][0],result[4][1]);
for(i=0;i<4;i++)
line(result[i][0],result[i][1],result[i+4][0],result[i+4][1]);
void projections::copyback()
int i=0,j=0;
for(i=0;i<8;i++)
for(j=0;j<4;j++)
result[i][j]=vertices[i][j];
void projections::multiplication()
double r[8][4];
44
int i=0,j=0,k=0;
for(i=0;i<8;i++)
for(j=0;j<4;j++)
r[i][j]=0;
for(k=0;k<4;k++)
r[i][j]+=result[i][k]*t_matrix[k][j];
for(i=0;i<8;i++)
for(j=0;j<4;j++)
result[i][j]=r[i][j];
void projections::orthographic()
cleardevice();
clearviewport();
cout<<"\tORTHOGRAPHIC PROJECTION";
copyback();
t_matrix[0][0]=1;
t_matrix[0][1]=0;
t_matrix[0][2]=0;
45
t_matrix[0][3]=0;
t_matrix[1][0]=0;
t_matrix[1][1]=1;
t_matrix[1][2]=0;
t_matrix[1][3]=0;
t_matrix[2][0]=0;
t_matrix[2][1]=0;
t_matrix[2][2]=0;
t_matrix[2][3]=0;
t_matrix[3][0]=0;
t_matrix[3][1]=0;
t_matrix[3][2]=0;
t_matrix[3][3]=1;
multiplication();
setcolor(YELLOW);
display_cube();
delay(20);
getch();
angle_x=((pi/180)*angle_x);
angle_y=((pi/180)*angle_y);
46
cleardevice();
clearviewport();
cout<<"\tAXONOMETRIC PROJECTION";
copyback();
t_matrix[0][0]=cos(angle_y);
t_matrix[0][1]=(sin(angle_y))*(sin(angle_x));
t_matrix[0][2]=0;
t_matrix[0][3]=0;
t_matrix[1][0]=0;
t_matrix[1][1]=cos(angle_x);
t_matrix[1][2]=0;
t_matrix[1][3]=0;
t_matrix[2][0]=sin(angle_y);
t_matrix[2][1]=(-1)*(cos(angle_y)*sin(angle_x));
t_matrix[2][2]=0;
t_matrix[2][3]=0;
t_matrix[3][0]=0;
t_matrix[3][1]=0;
t_matrix[3][2]=0;
t_matrix[3][3]=1;
multiplication();
setcolor(YELLOW);
display_cube();
47
delay(20);
getch();
angle=((pi/180)*angle);
cleardevice();
clearviewport();
cout<<"\tCAVALIER PROJECTION";
copyback();
t_matrix[0][0]=1;
t_matrix[0][1]=0;
t_matrix[0][2]=0;
t_matrix[0][3]=0;
t_matrix[1][0]=0;
t_matrix[1][1]=1;
t_matrix[1][2]=0;
t_matrix[1][3]=0;
t_matrix[2][0]=(-1)*1*cos(angle);
t_matrix[2][1]=(-1)*1*sin(angle);
t_matrix[2][2]=0;
t_matrix[2][3]=0;
t_matrix[3][0]=0;
48
t_matrix[3][1]=0;
t_matrix[3][2]=0;
t_matrix[3][3]=1;
multiplication();
setcolor(YELLOW);
display_cube();
delay(20);
getch();
angle=((pi/180)*angle);
cleardevice();
clearviewport();
cout<<"\tCABINET PROJECTION";
copyback();
t_matrix[0][0]=1;
t_matrix[0][1]=0;
t_matrix[0][2]=0;
t_matrix[0][3]=0;
t_matrix[1][0]=0;
t_matrix[1][1]=1;
t_matrix[1][2]=0;
49
t_matrix[1][3]=0;
t_matrix[2][0]=(-1)*0.5*cos(angle);
t_matrix[2][1]=(-1)*0.5*sin(angle);
t_matrix[2][2]=0;
t_matrix[2][3]=0;
t_matrix[3][0]=0;
t_matrix[3][1]=0;
t_matrix[3][2]=0;
t_matrix[3][3]=1;
multiplication();
setcolor(YELLOW);
display_cube();
delay(20);
getch();
void projections::single_point(double r)
double l=10,m=10,n=10;
cleardevice();
clearviewport();
r=(-1/r);
copyback();
50
t_matrix[0][0]=1;
t_matrix[0][1]=0;
t_matrix[0][2]=0;
t_matrix[0][3]=0;
t_matrix[1][0]=0;
t_matrix[1][1]=1;
t_matrix[1][2]=0;
t_matrix[1][3]=0;
t_matrix[2][0]=0;
t_matrix[2][1]=0;
t_matrix[2][2]=0;
t_matrix[2][3]=r;
t_matrix[3][0]=l;
t_matrix[3][1]=m;
t_matrix[3][2]=0;
t_matrix[3][3]=r*n+1;
multiplication();
setcolor(YELLOW);
display_cube();
delay(20);
getch();
void projections::two_point(double r)
51
{
double angle=45;
angle=(pi/180)*angle;
cleardevice();
clearviewport();
copyback();
r=(-1/r);
t_matrix[0][0]=cos(angle);
t_matrix[0][1]=0;
t_matrix[0][2]=(-1*sin(angle));
t_matrix[0][3]=sin(angle)/r;
t_matrix[1][0]=0;
t_matrix[1][1]=1;
t_matrix[1][2]=0;
t_matrix[1][3]=0;
t_matrix[2][0]=sin(angle);
t_matrix[2][1]=0;
t_matrix[2][2]=cos(angle);
t_matrix[2][3]=(-1*cos(angle))/r;
t_matrix[3][0]=0;
t_matrix[3][1]=0;
t_matrix[3][2]=0;
52
t_matrix[3][3]=1;
multiplication();
setcolor(YELLOW);
display_cube();
delay(20);
getch();
void projections::three_point(double r)
double angle_y=45,angle_x=45;
angle_y=(pi/180)*angle_y;
angle_x=(pi/180)*angle_x;
cleardevice();
clearviewport();
copyback();
r=(-1/r);
t_matrix[0][0]=cos(angle_y);
t_matrix[0][1]=sin(angle_y)*sin(angle_x);
t_matrix[0][2]=0;
t_matrix[0][3]=(sin(angle_y)*cos(angle_x))/r;
t_matrix[1][0]=0;
t_matrix[1][1]=cos(angle_x);
53
t_matrix[1][2]=0;
t_matrix[1][3]=(-1*sin(angle_x))/r;
t_matrix[2][0]=(sin(angle_y));
t_matrix[2][1]=(-1*cos(angle_y)*sin(angle_x));
t_matrix[2][2]=0;
t_matrix[2][3]=(-1*cos(angle_y)*cos(angle_x))/r;
t_matrix[3][0]=0;
t_matrix[3][1]=0;
t_matrix[3][2]=0;
t_matrix[3][3]=1;
multiplication();
setcolor(YELLOW);
display_cube();
delay(20);
getch();
int main()
system("cls");
int gd=DETECT,gm,choice;
projections t1;
char ch1,ch2,axis,axis1,axis2;
double angle_x,angle_y,angle,ratio,ratio1,ratio2,ratio3;
54
do
cout<<"\n\n\t........PROJECTIONS OF 3D OBJECTS........\n";
t1.get_vertices();
do
initgraph(&gd,&gm,(char*)"");
cout<<"\n\n...........MENU...........";
cout<<"\n1.Orthographic.";
cout<<"\n2.Axonometric.";
cout<<"\n5.Single-Point presepective.";
cout<<"\n6.Two-Point presepective.";
cout<<"\n7.Three-Point presepective.";
cout<<"\n............................";
cin>>choice;
switch(choice)
case 1:t1.orthographic();
break;
55
case 2:cout<<"\n\nFOR AXONOMETRIC PROJECTION";
cout<<"\nx-axis : ";
cin>>angle_x;
cout<<"y-axis : ";
cin>>angle_y;
t1.axonometric(angle_x,angle_y);
break;
cin>>angle;
t1.cavalier(angle);
break;
cin>>angle;
t1.cabinet(angle);
break;
cout<<"\nAssuming that the VP lies on the z-axis, Enter the prespective ratio: ";
cin>>ratio;
t1.single_point(ratio);
break;
56
case 6:cout<<"\n\nFOR TWO POINT PRESPECTIVE PROJECTION";
cout<<"\nAssuming that the VP lies on the z-axis, Enter the prespective ratio: ";
cin>>ratio;
t1.two_point(ratio);
break;
cout<<"\nAssuming that the VP lies on the z-axis, Enter the prespective ratio: ";
cin>>ratio;
t1.three_point(ratio);
break;
default:cout<<"\n\n\t!!!INVALID CHOICE!!!";
getch();
closegraph();
cin>>ch2;
}while(ch2=='y' || ch2=='Y');
cin>>ch1;
}while(ch1=='y' || ch1=='Y');
getch();
return 0;
}
57
Output:
58
59
60
61
62
63
7. Write a program to draw Hermite /Bezier curve.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
#include <conio.h>
#include <iostream>
int dx = xb - xa;
int dy = yb - ya;
int steps, k;
steps = abs(dx);
else
64
steps = abs(dy);
x += x_inc;
y += y_inc;
return 0;
int i;
double t;
65
// Original curve
// New curve
int main()
int i;
66
cout << "\n Enter the X and Y coordinates of the four control points:" << endl;
cin>>x[i]>>y[i];
bezier(x, y);
getch();
closegraph();
return 0;
Output:
67
68
8. DDA line algorithm.
Program:
#include<iostream>
#include<math.h>
int dx = xb-xa;
int dy = yb-ya;
int steps, k;
if(abs(dx)>abs(dy))
69
steps=abs(dx);
else
steps=abs(dy);
x_inc = dx/float(steps);
y_inc = dy/float(steps);
x += x_inc;
y += y_inc;
return 0;
int main()
70
//getmaxx() and getmaxy() returns the maximum (screen-relative) x and y
value for the current graphics driver and mode
x_mid = X/2;
y_mid = Y/2;
cin>>x1;
cin>>x2;
cin>>y1;
cin>>y2;
71
/*displays a text string in the viewport at the given distance (x,y)
using the current justification settings and the current font, direction and size
*/
setcolor(GREEN);
getch();
then restores the screen to the mode it was in before you called initgraph.*/
closegraph();
return 0;
Output:
72
73
74
9. Mid point line algorithm.
Program:
#include<iostream>
#include<graphics.h>
#include<stdlib.h>
int dx,dy;
dx=x2-x1;
dy=y2-y1;
if(dy<=dx){
int d = dy - (dx/2);
int x = x1,y=y1;
cout<<"("<<x<<','<<y<<")"<<'\n';
while(x<x2){
putpixel(x,y,6);
x++;
if(d<0)
d=d+dy;
else{
75
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';
while(y<y2){
putpixel(x,y,6);
y++;
if(d<0){
d=d+dx;
else{
d += dx-dy;
x++;
cout<<"("<<x<<','<<y<<")"<<'\n';
76
}
int main(){
system("cls");
int x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
midPoint(x1,y1,x2,y2);
getch();
return 0;
77
Output:
78