Experiment No.-3 Aim: To Draw A Line, Triangle and Circle Using Functions of Graphics.h Header File. Description

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 51

Department of Computer Science and Engineering

Chandigarh University

Experiment No.- 3

Aim : To draw a line, triangle and circle using functions of graphics.h header

File.

Description: C graphics.h header file contain functions that can be used to draw different
Shapes and display text and many more functions. We can make projects, games

Etc . and we can also draw circles amd many geometrical figures.

Program : #include<graphics.h>

#include<iostream.h>

#include<conio.h>

void main()

{ int gd=DETECT,gn;

initgraph(&gd,&gn,"");

line(50,50,50,150);

line(150,50,200,150);

line(200,150,100,150);

line(100,150,150,50);

circle(300,100,50);

getch(); }

Output:

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Experiment 4

Aim : To display simple shapes-Hut, Star and Car using graphics primitives.

Description:

Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.

ARC function is used to draw an arc with center (x,y) and stangle specifies starting angle,
endangle specifies the end angle and last parameter specifies the radius of the arc. arc function
can also be used to draw a circle but for that starting angle and end angle should be 0 and 360
respectively.

Source code for HUT :

#include<graphics.h>

#include<iostream.h>

#include<conio.h>

void main()

int a[]={100,100,400,100,450,200,50,200,100,100};

int gd=DETECT,gn;

initgraph(&gd,&gn,"");

drawpoly(5,a);

outtextxy(250,150,"HUT");
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

line(100,100,150,200);

rectangle(50,200,150,400);

rectangle(150,200,450,400);

rectangle(65,400,135,250);

rectangle(250,250,390,350);

line(320,250,320,350);

line(250,300,390,300);

getch();

OUTPUT :

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Source code for STAR :


#include<graphics.h>

#include<iostream.h>

#include<conio.h>

void main()

int gd=DETECT,gm;
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

initgraph(&gd,&gm,"");

int
a[]={200,200,250,250,300,250,250,300,300,350,250,350,200,400,150,350,100,350,150,300,100,
250,150,250,200,200};

drawpoly(13,a);

outtextxy(185,300,"STAR");

getch();

OUTPUT :

Source Code for CAR :


#include<graphics.h>

#include<iostream.h>

#include<conio.h>

void main()

{ int gd=DETECT,gm;

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

initgraph(&gd,&gm,"");

int a[]={100,200,150,150,250,150,300,200,350,200,350,280,50,280,50,200,99,200,300,200};

drawpoly(10,a);

line(200,150,200,200);

outtextxy(190,240,"CAR");

arc(100,280,180,360,25);

arc(300,280,180,360,25);

getch(); }

OUTPUT :

Experiment-1(a)

Aim: To draw a line using Simple DDA Algorithm for positive line slope.

Description:
DDA algorithm is an incremental scan conversion method. The characterstics of DDA algorithm
is to take unit steps along one coordinate and compute corresponding values along the other
coordinate.It is the simplest algorithm and it does not require special skills for implementation.

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Source Code:
#include<iostream.h>

#include<conio.h>

#include<dos.h>

#include<math.h>

#include<graphics.h>

#define round(x) ((int)(x+0.5))

#define round(y) ((int)(y+0.5))

void dda(int xa,int ya,int xb,int yb)

{ int dx=xb-xa,dy=yb-ya,l;

float xinc,yinc,x=xa,y=ya;

if(abs(dx)>abs(dy))

{ l=abs(dx); }

else

{ l=abs(dy); }

xinc=dx/float(l);

yinc=dy/float(l);

putpixel(round(x),round(y),15);

for(int k=0;k<l;k++)

{ x=x+xinc;

y=y+yinc;

putpixel(round(x),round(y),15);

delay(100);

}
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

} void main()

{ int gd=DETECT,gn,p1,p2,p3,p4;

initgraph(&gd,&gn,"");

cout<<"Enter the point 1"<<endl;

cin>>p1>>p2;

cout<<"Enter the point 2"<<endl;

cin>>p3>>p4;

dda(p1,p2,p3,p4);

getch();

closegraph(); }

Output:
O

Experiment-1(b)

Aim: To draw a line using Symmetrical DDA Algorithm for positive line
slope.

Source Code :
#include<iostream.h>

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

#include<conio.h>

#include<dos.h>

#include<math.h>

#include<graphics.h>

#define round(x) ((int)(x+0.5))

#define round(y) ((int)(y+0.5))

void dda(int xa,int ya,int xb,int yb)

{ int dx=xb-xa,dy=yb-ya,l;

float xinc,yinc,x=xa,y=ya;

if(abs(dx)>abs(dy))

{ l=abs(dx); }

else

{ l=abs(dy); }

float n=log10(l)/log10(2);

xinc=dx/pow(2,n);

yinc=dy/pow(2,n);

putpixel(round(x),round(y),15);

for(int k=0;k<l;k++)

x=x+xinc;

y=y+yinc;

putpixel(round(x),round(y),15);

delay(100); }}

void main()

{ int gd=DETECT,gn,p1,p2,p3,p4;
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

initgraph(&gd,&gn,"");

cout<<"Enter the point 1"<<endl;

cin>>p1>>p2;

cout<<"Enter the point 2"<<endl;

cin>>p3>>p4;

dda(p1,p2,p3,p4);

getch();

closegraph(); }

Output :

Experiment-1(c)

Aim: To draw a line using Bresenhams Algorithm for positive line slope.

Description :

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Bresenhams Algorithm is another incremental scan conversion algorithm.The big advantage of


this algorithm is that , it uses only integer calculations.

Source Code :
#include<iostream.h>

#include<conio.h>

#include<dos.h>

#include<math.h>

#include<graphics.h>

int sign(int x)

{ if(x<0)

return(-1);

if(x>0)

return(1);

else

return(0); }

void bres(int xa,int ya,int xb,int yb)

{ int dx=abs(xb-xa),dy=abs(yb-ya),l,flag=0;

int x=xa,y=ya,sx,sy;

sx=sign(xb-xa);

sy=sign(yb-ya);

if(dy>dx)

{ int t=dx;

dx=dy;

dy=t;

l=dx;

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

flag=1; }

else

{ l=dx;

flag=0; }

int p=(2*dy)-dx;

int twodx=(2*dx);

int twody=(2*dy);

putpixel(x,y,15);

delay(100);

for(int i=0;i<l;i++)

{ while(p>=0)

{ if(flag==1)

{ x=x+sx; }

else

{ y=y+sy;}

p=p-twodx;

if(flag==1)

{ y=y+sy; }

else

{ x=x+sx; }

p=p+twody;

putpixel(x,y,15);

delay(100);

} }
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

void main()

{ int gd=DETECT,gn,p1,p2,p3,p4;

initgraph(&gd,&gn,"");

cout<<"Enter the point 1"<<endl;

cin>>p1>>p2;

cout<<"Enter the point 2"<<endl;

cin>>p3>>p4;

bres(p1,p2,p3,p4);

getch();

closegraph(); }

Output:

Experiment-2(a)

Aim: To draw a line using Simple DDA Algorithm for negative line slope.

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Source Code:
#include<iostream.h>

#include<conio.h>

#include<dos.h>

#include<math.h>

#include<graphics.h>

#define round(x) ((int)(x+0.5))

#define round(y) ((int)(y+0.5))

void dda(int xa,int ya,int xb,int yb)

{ int dx=xb-xa,dy=yb-ya,l;

float xinc,yinc,x=xa,y=ya;

if(abs(dx)>abs(dy))

{ l=abs(dx); }

else

{ l=abs(dy); }

xinc=dx/float(l);

yinc=dy/float(l);

putpixel(round(x),round(y),15);

for(int k=0;k<l;k++)

{ x=x+xinc;

y=y+yinc;

putpixel(round(x),round(y),15);

delay(100);

}
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

} void main()

{ int gd=DETECT,gn,p1,p2,p3,p4;

initgraph(&gd,&gn,"");

cout<<"Enter the point 1"<<endl;

cin>>p1>>p2;

cout<<"Enter the point 2"<<endl;

cin>>p3>>p4;

dda(p1,p2,p3,p4);

getch();

closegraph(); }

Output:
O

Experiment-2(b)

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Aim: To draw a line using Symmetrical DDA Algorithm for negative line
slope.

Source Code :
#include<iostream.h>

#include<conio.h>

#include<dos.h>

#include<math.h>

#include<graphics.h>

#define round(x) ((int)(x+0.5))

#define round(y) ((int)(y+0.5))

void dda(int xa,int ya,int xb,int yb)

{ int dx=xb-xa,dy=yb-ya,l;

float xinc,yinc,x=xa,y=ya;

if(abs(dx)>abs(dy))

{ l=abs(dx); }

else

{ l=abs(dy); }

float n=log10(l)/log10(2);

xinc=dx/pow(2,n);

yinc=dy/pow(2,n);

putpixel(round(x),round(y),15);

for(int k=0;k<l;k++)

x=x+xinc;

y=y+yinc;
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

putpixel(round(x),round(y),15);

delay(100); }}

void main()

{ int gd=DETECT,gn,p1,p2,p3,p4;

initgraph(&gd,&gn,"");

cout<<"Enter the point 1"<<endl;

cin>>p1>>p2;

cout<<"Enter the point 2"<<endl;

cin>>p3>>p4;

dda(p1,p2,p3,p4);

getch();

closegraph(); }

Output :

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Experiment-2(c)

Aim: To draw a line using Bresenhams Algorithm for negative line slope.

Source Code :
#include<iostream.h>

#include<conio.h>

#include<dos.h>

#include<math.h>

#include<graphics.h>

int sign(int x)

{ if(x<0)

return(-1);

if(x>0)

return(1);

else

return(0); }

void bres(int xa,int ya,int xb,int yb)

{ int dx=abs(xb-xa),dy=abs(yb-ya),l,flag=0;

int x=xa,y=ya,sx,sy;

sx=sign(xb-xa);

sy=sign(yb-ya);

if(dy>dx)

{ int t=dx;

dx=dy;

dy=t;

l=dx;
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

flag=1; }

else

{ l=dx;

flag=0; }

int p=(2*dy)-dx;

int twodx=(2*dx);

int twody=(2*dy);

putpixel(x,y,15);

delay(100);

for(int i=0;i<l;i++)

{ while(p>=0)

{ if(flag==1)

{ x=x+sx; }

else

{ y=y+sy;}

p=p-twodx;

if(flag==1)

{ y=y+sy; }

else

{ x=x+sx; }

p=p+twody;

putpixel(x,y,15);

delay(100);

} }
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

void main()

{ int gd=DETECT,gn,p1,p2,p3,p4;

initgraph(&gd,&gn,"");

cout<<"Enter the point 1"<<endl;

cin>>p1>>p2;

cout<<"Enter the point 2"<<endl;

cin>>p3>>p4;

bres(p1,p2,p3,p4);

getch();

closegraph(); }

Output:

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

EXPERIMENT-8.

Aim: - To draw a circle with a given center and radius using mid-point circle
algorithm.

Description: -
This algorithm draws all eight octants simultaneously, starting from each cardinal direction (0,
90, 180, 270) and extends both ways to reach the nearest multiple of 45 (45, 135, 225,
315). You can determine where to stop because when y = x, you have reached 45. The reason
for using these angles is shown in the above picture: As you increase y, you do not skip nor
repeat any y value until you get to 45. So during the while loop, y increments by 1 each
iteration, and x decrements by 1 on occasion, never exceeding 1 in one iteration. This changes at
45 because that is the point where the tangent is rise=run. Whereas rise>run before and rise<run
after.

Source Code: -
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void circleplotpoint(int,int,int,int);
void circlemidpoint(int xc,int yc,int r)
{ int x=0,y=r;
int p=(1-r);
circleplotpoint(xc,yc,x,y);
while(x<y)
{ x++;
if(p<0)
{ p=p+2*x+1; }
else
{ y--;
p=p+2*(x-y)+1; }
circleplotpoint(xc,yc,x,y);
delay(100);} }
void circleplotpoint(int xc,int yc,int x,int y)
{ putpixel(xc+x, yc+y, 15);
putpixel(xc-x, yc+y, 14);
putpixel(xc+x, yc-y, 13);

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

putpixel(xc-x, yc-y, 12);


putpixel(xc-y, yc+x, 11);
putpixel(xc+y, yc+x, 10);
putpixel(xc+y, yc-x, 9);
putpixel(xc-y, yc-x, 8); }
void main()
{ int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm," ");
int xc,yc,r;
cout<<"\n Enter the values of coordinates and radius : \n";
cin>>xc;
cin>>yc;
cin>>r;
circlemidpoint(xc,yc,r);
getch();
}

Output:-

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

EXPERIMENT-9.

Aim: - To draw a circle with a given center and radius using circle generator
algorithm.

Description:-
This algorithm draws all eight octants simultaneously, starting from each cardinal direction (0,
90, 180, 270) and extends both ways to reach the nearest multiple of 45 (45, 135, 225,
315). You can determine where to stop because when y = x, you have reached 45. The reason
for using these angles is shown in the above picture: As you increase y, you do not skip nor
repeat any y value until you get to 45. So during the while loop, y increments by 1 each
iteration, and x decrements by 1 on occasion, never exceeding 1 in one iteration. This changes at
45 because that is the point where the tangent is rise=run. Whereas rise>run before and rise<run
after.

Source Code:-

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void circleplot(int,int,int,int);
void circlepoint(int xc,int yc,float r)
{ double x,y;
for(x=0;x<=y;x++)
{ double temp=((r*r)-(x*x));
y=sqrt(temp);
circleplot(xc,yc,x,y);
delay(50); }
}
void main()
{
intgd=DETECT,gm,x,y,r;
clrscr();
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
cout<<"Enter center point(Xc,Yc):\t";
cin>>x>>y;
cout<<"Enter Radius\t";
cin>>r;
circlepoint(x,y,r);
getch();
closegraph();

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

void circleplot(int xc, int yc, int x, int y)


{
putpixel(xc+x,yc+y,1);
putpixel(xc+x,yc-y,2);
putpixel(xc-x,yc+y,3);
putpixel(xc-x,yc-y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc+y,yc+x,6);
putpixel(xc-y,yc+x,7);
putpixel(xc-y,yc-x,8);
}

Output:-

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Experiment-5(a)

Aim: To perform translation transformation on a given triangle.

Description:
Transformation means changing some graphics into something else by applying rules. There are
various types of transformations such as translation, scaling up or down, rotation, shearing, etc.
When a transformation takes place on a 2D plane, it is called 2D transformation. Transformations
play an important role in computer graphics to reposition the graphics on the screen and change
their size or orientation. A translation moves an object to a different position on the screen. You
can translate a point in 2D by adding translation coordinate (tx, ty) to the original coordinate (X,
Y) to get the new coordinate (X, Y).

The pair (tx, ty) is called the translation vector or shift vector.

Source Code :
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int x1,y1,x2,y2,x3,y3,tx,ty;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");

cout<<"enter coordinates of first point :"<<endl;


cin>>x1>>y1;

cout<<"enter coordinates of second point :"<<endl;


cin>>x2>>y2;

cout<<"enter coordinates of third point :"<<endl;


cin>>x3>>y3;

int p[]={x1,y1,x2,y2,x3,y3,x1,y1};

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

drawpoly(4,p);
outtextxy(x1+50,y1+50,"original image");

cout<<"enter the translation value tx and ty :"<<endl;


cin>>tx>>ty;

int q[]={x1+tx,y1+ty,x2+tx,y2+ty,x3+tx,y3+ty,x1+tx,y1+ty};
setcolor(4);
drawpoly(4,q);
outtextxy(x1+tx+50,y1+ty+50,"translated image");

getch();
closegraph();
}

Output:

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Experiment-5(b)

Aim : To perform scaling transformation on a given triangle.

Description:

Transformation means changing some graphics into something else by applying rules. There are
various types of transformations such as translation, scaling up or down, rotation, shearing, etc.
When a transformation takes place on a 2D plane, it is called 2D
transformation.Transformations play an important role in computer graphics to reposition the
graphics on the screen and change their size or orientation. To change the size of an object,
scaling transformation is used. In the scaling process, you either expand or compress the
dimensions of the object. Scaling can be achieved by multiplying the original coordinates of the
object with the scaling factor to get the desired result. Let us assume that the original
coordinates are (X, Y), the scaling factors are (S X, SY), and the produced coordinates are (X,
Y). This can be mathematically represented as shown below

X' = X . SX and Y' = Y . SY

Source Code:

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int x1,y1,x2,y2,x3,y3,sx,sy;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");

cout<<"enter coordinates of first point :"<<endl;


cin>>x1>>y1;

cout<<"enter coordinates of second point :"<<endl;


cin>>x2>>y2;

cout<<"enter coordinates of third point :"<<endl;


14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

cin>>x3>>y3;

int p[]={x1,y1,x2,y2,x3,y3,x1,y1};
drawpoly(4,p);

cout<<"enter the translation value sx and sy :"<<endl;


cin>>sx>>sy;

int q[]={x1*sx,y1*sy,x2*sx,y2*sy,x3*sx,y3*sy,x1*sx,y1*sy};
setcolor(4);
drawpoly(4,q);

getch();
closegraph();
}

Output :

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Experiment-5(c)

Aim: To perform rotation transformation on a given triangle.

Description :

Transformation means changing some graphics into something else by applying rules. There are
various types of transformations such as translation, scaling up or down, rotation, shearing, etc.
When a transformation takes place on a 2D plane, it is called 2D
transformation.Transformations play an important role in computer graphics to reposition the
graphics on the screen and change their size or orientation.

In rotation, we rotate the object at particular angle (theta) from its origin. Using standard
trigonometric the original coordinate of point P(X, Y) can be represented as X=rcos.. ,
Y=rsin. Same way we can represent the point P (X, Y) as

x=rcos(+)=rcoscosrsinsin

y=rsin(+)=rcossin+rsincos

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

R=[cos() sin()

sin() cos()]

Source Code:
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main()
{
clrscr();
int x1,y1,x2,y2,x3,y3,tx,ty;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"enter coordinates of first point :"<<endl;
cin>>x1>>y1;
cout<<"enter coordinates of second point :"<<endl;
cin>>x2>>y2;
cout<<"enter coordinates of third point :"<<endl;
cin>>x3>>y3;
int p[]={x1,y1,x2,y2,x3,y3,x1,y1};
drawpoly(4,p);
outtextxy(x2+50,(y2+y3)/2,"original image");
int ang,tx1,ty1,tx2,ty2,tx3,ty3;
cout<<"enter angle to rotate:";
cin>>ang;
float rad=ang*(3.142/180);
tx1=x1*cos(rad)-y1*sin(rad);
ty1=y1*cos(rad)+x1*sin(rad);
tx2=x2*cos(rad)-y2*sin(rad);
ty2=y2*cos(rad)+x2*sin(rad);
tx3=x3*cos(rad)-y3*sin(rad);
ty3=y3*cos(rad)+x3*sin(rad);
int q[]={tx1,ty1,tx2,ty2,tx3,tx3,tx1,ty1};
setcolor(4);
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

drawpoly(4,q);
outtextxy((tx2+50),((ty2+ty3)/2),"scaled image");
getch();
closegraph(); }

Output :

Experiment-10

Aim: To display 4-bitregion code for end points of line and check whether the
line is completely on screen or off the screen.

Source Code:

#include<graphics.h>

#include<iostream.h>

#include<conio.h>

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

#define xmin 125

#define xmax 200

#define ymin 350

#define ymax 425

enum{LEFT=1,RIGHT=2,BELOW=4,ABOVE=8};

unsigned int encode(int x,int y)

{ unsigned int c=0;

if(x<xmin)

c=c|LEFT;

if(x>xmax)

c=c|RIGHT;

if(y<ymin)

c=c|BELOW;

if(y>ymax)

c=c|ABOVE;

return(c);

void check(unsigned int c1,unsigned int c2)

{ if(!(c1|c2))

cout<<"inside & completely visible";

else

if(c1&c2)

cout<<"outside &complete off screen";

else
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

cout<<"line is a clipping candidate"; }

void bin(unsigned int c)

{ int i;

for(i=8;i>0;i=i/2)

(c&i)?(cout<<"1"):cout<<"0"; }

void main()

{ int gd=DETECT,gm;

initgraph(&gd,&gm,"");

int x1,y1,x2,y2;

cout<<"enter the value x1: \n";

cin>>x1;

cout<<"enter the value of y1: \n";

cin>>y1;

cout<<"enter the value of x2: \n";

cin>>x2;

cout<<"enter the value of y2: \n";

cin>>y2;

line(xmin,0,xmin,getmaxy());

line(xmax,0,xmax,getmaxy());

line(0,ymin,getmaxx(),ymin);

line(0,ymax,getmaxx(),ymax);

line(x1,y1,x2,y2);

setcolor(4);

rectangle(xmin,ymin,xmax,ymax);

unsigned int code1, code2;


14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

code1=encode(x1,y1);

code2=encode(x2,y2);

cout<<"region code of point 1: \n";

bin(code1);

cout<<"region code of point 2: \n";

bin(code2);

check(code1,code2);

getch(); closegraph(); }

Experiment-6
Aim : To rotate a given triangle clockwise and anticlockwise about a given point.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd = DETECT, gmode;
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

initgraph(&gd, &gmode, "C:\\turboc3\\bgi");


double x1, x2, x3, y1, y2, y3, rot;
cout<<"Enter First X - Coordinate : ";
cin>>x1;
cout<<"Enter First Y - Coordinate : ";
cin>>y1;
cout<<"Enter Second X - Coordinate : ";
cin>>x2;
cout<<"Enter Second Y - Coordinate : ";
cin>>y2;
cout<<"Enter Third X - Coordinate : ";
cin>>x3;
cout<<"Enter Third Y - Coordinate : ";
cin>>y3;
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
cout<<"Enter Rotation Angle in Degrees : ";
cin>>rot;
double fpx;
double fpy;
cout<<"Enter Point of rotation x1 & X2 : ";
cin>>fpx>>fpy;

double rad = rot*3.14/180;


double X1 = (x1-fpx)*cos(rad) - (y1-fpy)*sin(rad) + fpx;
double Y1 = (x1-fpx)*sin(rad) + (y1-fpx)*cos(rad) + fpy;
double X2 = (x2-fpx)*cos(rad) - (y2-fpy)*sin(rad) + fpx;
double Y2 = (x2-fpx)*sin(rad) + (y2-fpx)*cos(rad) + fpy;
double X3 = (x3-fpx)*cos(rad) - (y3-fpy)*sin(rad) + fpx;
double Y3 = (x3-fpx)*sin(rad) + (y3-fpx)*cos(rad) + fpy;

line(X1, Y1, X2, Y2);


line(X2, Y2, X3, Y3);
line(X3, Y3, X1, Y1);

getch();
closegraph();
}

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

OUTPUT:-

Experiment-11
AIM- To clip a line intersecting at one point with given window using cohen
Sutherland line clipping algorithm.
#include<iostream.h>

#include<conio.h>

#include<dos.h>

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

#include<graphics.h>

#define xmin 200

#define ymin 200

#define xmax 400

#define ymax 350

enum{left=0x1 , right=0x2 ,below=0x4 ,above=0x8};

unsigned int encode(int x, int y)

{ unsigned int code=0x0;

if(x<xmin)

code=code|left;

if(x>xmax)

code=code|right;

if(y<ymin)

code=code|below;

if(y>ymax)

code=code|above;

return(code); }

void check(unsigned int code1,unsigned int code2, int x1,int y1,int x2,int y2)

{ float m=(y2-y1)/float(x2-x1);

int accept=0,done=0;

do { if(!(code1|code2))

{ accept=1;done=1; }

else if(code1 & code2)

{ accept=0;done=1; }

else { float x,y;


14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

int code=code1?code1:code2;

if(code & above)

{ y=ymax;x=x1+(y-y1)/m; }

else if(code & below)

{ y=ymin;x=x1+(y-y1)/m; }

else if(code & right)

{ x=xmax; y=y1+m*(x-x1); }

else if(code & left)

{ x=xmin; y=y1+m*(x-x1); }

if(code==code1)

{ x1=x; y1=y;

code1=encode(x1,y1); }

else { x2=x; y2=y;

code2=encode(x2,y2); }

} }while(!done);

if(accept==1)

{ setcolor(BLUE);

line(x1,y1,x2,y2); }

void bin(unsigned int n)

{ unsigned int i;

for(i=8;i>0;i=i/2)

{ (n&i)?cout<<"1":cout<<"0"; }

void main()
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

{ clrscr();

int x1,y1,x2,y2;

int gd=DETECT,gm;

initgraph(&gd,&gm," ");

cout<<"enter end points of line x1,y1: ";

cin>>x1>>y1;

cout<<"enter end points of line x2,y2: ";

cin>>x2>>y2;

line(x1,y1,x2,y2);

unsigned code1,code2;

code1=encode(x1,y1);

code2=encode(x2,y2);

cout<<"region code of end point 1 is ";

bin(code1);

cout<<"\nregion code of end point 2 is ";

bin(code2);

cout<<endl;

check(code1,code2,x1,y1,x2,y2);

line(xmin,0,xmin,getmaxy());

line(xmax,0,xmax,getmaxy());

line(0,ymax,getmaxx(),ymax);

line(0,ymin,getmaxx(),ymin);

setcolor(RED);

rectangle(xmin,ymin,xmax,ymax);

getch();
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

closegraph();

OUTPUT:

Experiment:12
AIM- To clip a line intersecting at one point with given window using cohen
Sutherland line clipping algorithm.
#include<iostream.h>

#include<conio.h>

#include<dos.h>
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

#include<graphics.h>

#define xmin 200

#define ymin 200

#define xmax 400

#define ymax 350

enum{left=0x1 , right=0x2 ,below=0x4 ,above=0x8};

unsigned int encode(int x, int y)

{ unsigned int code=0x0;

if(x<xmin)

code=code|left;

if(x>xmax)

code=code|right;

if(y<ymin)

code=code|below;

if(y>ymax)

code=code|above;

return(code); }

void check(unsigned int code1,unsigned int code2, int x1,int y1,int x2,int y2)

{ float m=(y2-y1)/float(x2-x1);

int accept=0,done=0;

do { if(!(code1|code2))

{ accept=1;done=1; }

else if(code1 & code2)

{ accept=0;done=1; }

else { float x,y;


14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

int code=code1?code1:code2;

if(code & above)

{ y=ymax;x=x1+(y-y1)/m; }

else if(code & below)

{ y=ymin;x=x1+(y-y1)/m; }

else if(code & right)

{ x=xmax; y=y1+m*(x-x1); }

else if(code & left)

{ x=xmin; y=y1+m*(x-x1); }

if(code==code1)

{ x1=x; y1=y;

code1=encode(x1,y1); }

else { x2=x; y2=y;

code2=encode(x2,y2); }

} }while(!done);

if(accept==1)

{ setcolor(BLUE);

line(x1,y1,x2,y2); }

void bin(unsigned int n)

{ unsigned int i;

for(i=8;i>0;i=i/2)

{ (n&i)?cout<<"1":cout<<"0"; }

void main()
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

{ clrscr();

int x1,y1,x2,y2;

int gd=DETECT,gm;

initgraph(&gd,&gm," ");

cout<<"enter end points of line x1,y1: ";

cin>>x1>>y1;

cout<<"enter end points of line x2,y2: ";

cin>>x2>>y2;

line(x1,y1,x2,y2);

unsigned code1,code2;

code1=encode(x1,y1);

code2=encode(x2,y2);

cout<<"region code of end point 1 is ";

bin(code1);

cout<<"\nregion code of end point 2 is ";

bin(code2);

cout<<endl;

check(code1,code2,x1,y1,x2,y2);

line(xmin,0,xmin,getmaxy());

line(xmax,0,xmax,getmaxy());

line(0,ymax,getmaxx(),ymax);

line(0,ymin,getmaxx(),ymin);

setcolor(RED);

rectangle(xmin,ymin,xmax,ymax);

getch();
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

closegraph();

OUTPUT:

Experiment-13

Aim: To display the result of window to viewport transformation.

Source Code:
#include<iostream.h>

#include<conio.h>

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

#include<graphics.h>

#include<math.h>

void main()

int gd=DETECT,gm;

initgraph(&gd,&gm," ");

int wx1,wy1,wx2,wy2,wxmin,wymin,wxmax,wymax,vxmin,vymin,vxmax,vymax,vx1,vx2

int vy1,vy2;

clrscr();

cout<<"Enter co-ordinates of window:";

cin>>wxmin>>wymin>>wxmax>>wymax;

rectangle(wxmin,wymin,wxmax,wymax);

outtextxy(wxmin+10,wymax+10,"window");

cout<<"Enter co-ordinates of viewport:";

cin>>vxmin>>vymin>>vxmax>>vymax;

rectangle(vxmin,vymin,vxmax,vymax);

cout<<"enter the co-ordinates of line:";

cin>>wx1>>wy1>>wx2>>wy2;

outtextxy(vxmin+10,vymax+10,"viewport");

line(wx1,wy1,wx2,wy2);

float sx=(vxmax-vxmin)/(wxmax-wxmin);

float sy=(vymax-vymin)/(wymax-wymin);

vx1=sx*(wx1-wxmin)+vxmin;

vy1=sy*(wy1-wymin)+vymin;

vx2=sx*(wx2-wxmin)+vxmin;
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

vy2=sy*(wy2-wymin)+vymin;

line(vx1,vy1,vx2,vy2);

getch();

closegraph();

Output :

Experiment-7

Aim: To perform reflection of a point about a line y= mx+c.

Source Code:
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

void main()
{
int gd=DETECT,gm=DETECT,x1,x2,y1,y2,px,py,rx,ry;
float ang,t;
float angle,a,b,c,d,e,f;
initgraph(&gd,&gm,"");
cout<<"Enter Coordinates"<<endl;
cout<<"Enter X1,Y1: ";
cin>>x1>>y1;
cout<<"Enter X2,Y2: ";
cin>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"Enter Point Coordinates: ";
cin>>px>>py;
putpixel(px,py,15);
outtextxy(px,py+10,"O");
px-=x1;py-=y1;
t=(y2-y1)/float(x2-x1);
ang=atan(t);
ang=-ang;
rx=px*cos(ang)-py*sin(ang);
ry=px*sin(ang)+py*cos(ang);
ry=-ry;
ang=-ang;
rx=rx*cos(ang)-ry*sin(ang);
ry=rx*sin(ang)+ry*cos(ang);
rx+=x1;ry+=y1;
putpixel(rx,ry,15);
outtextxy(rx,ry+10,"R");
getch();
}

Output

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

Experiment -14
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

AIM:To calculate and draw bezier curve passing through four control points.
CODE:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x[4],y[4];
double t,xt,yt;
cout<<"Enter coordinates for control point";
for(int i=0;i<4;i++)
{ cout<<" control points"<<(i+1)<<":";
cin>>x[i]>>y[i];
}
for(t=0.0;t<1.0;t=t+0.0005){
xt=pow(1-t,3)*x[0]+3*t*pow(1-t,2)*x[1]+3*pow(t,2)*(1-t)*x[2]+pow(t,3)*x[3];
yt=pow(1-t,3)*y[0]+3*t*pow(1-t,2)*y[1]+3*pow(t,2)*(1-t)*y[2]+pow(t,3)*y[3];
putpixel(xt,yt,15);}
for(i=0;i<4;i++){
putpixel(x[i],y[i],RED);}
getch();
}

Experiement-15
14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

AIM- To draw a B-Spline curve.


Source Code :
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
double basis(int i,int k,double knot[],double stpos)
{
double val;
if(k==1)
{
if (knot[i]<=stpos&&stpos<knot[i+1])
return (1);
else
return (0);
}
val=((stpos-knot[i])*basis(i,k-1,knot,stpos))/(knot[i+k-1]-knot[i])+((knot[i+k]-
stpos)*basis(i+1,k-1,knot,stpos))/(knot[i+k]-knot[i+1]);
return val;
}
void main()
{
int xc[6]={10,80,250,300,500,500};
int yc[6]={180,130,10,400,50,70};
double knot[]={0,1,2,3,4,5,6,7};
int k=4;
double bas,stpos=knot[k-1],endpos=knot[8-k],slice=(endpos-stpos)/100;
double x,y,lx,ly;

14BCS1271
Mukul Sharma
Department of Computer Science and Engineering
Chandigarh University

int gd=DETECT,gm;
initgraph(&gd,&gm,"");
lx=xc[0];ly=yc[0] ;
for(;stpos<endpos;stpos+=slice)
{ x=y=0;
for(int i=0;i<=5;i++)
{ bas=basis(i,k,knot,stpos);
x=x+(xc[i]*bas);
y=y+(yc[i]*bas); }
line(lx,ly,x,y) ;
lx=x;ly=y; }
getch();
closegraph();
}

OUTPUT:-

14BCS1271
Mukul Sharma

You might also like