You are on page 1of 46

CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-3
Introduction to Computer Graphics and to draw basic components like line,
circle, arc, rectangle and ellipse
Computer Graphics are pictures and movies created using computers usually referring to
image data created by a computer specifically with help from specialized graphical hardware
and software.

The term computer graphics includes almost everything on computers that is not text or
sound. Today almost every computer can do some graphics, and people have even come to
expect to control their computer through icons and pictures rather than just by typing.

Here in our lab at the Program of Computer Graphics, we think of computer graphics as
drawing pictures on computers, also called rendering. The pictures can be photographs,
drawings, movies, or simulations -- pictures of things which do not yet exist and maybe could
never exist. Or they may be pictures from places we cannot see directly, such as medical
images from inside your body.

In programming output can be generated in two modes :-

Text mode Here we are not provided with assistance of mouse and pictorial figures,
we only deal with text.
Graphic mode- It is defined as pictorial representation of data

Syntax:-

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setbkcolor(2);
circle(100,100,50);
outtextxy(75,98,"CIRCLE");
rectangle(210,60,400,140);
outtextxy(270,100,"RECTANGLE");
line(455,85,555,85);
outtextxy(490,95,"LINE");
arc(80,350,10,80,100);
outtextxy(100,340,"ARC");
ellipse(380,300,0,360,40,80);

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
outtextxy(355,300,"ELLIPSE");
getch();
}

Output:-

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-4
To display simple shapes(hut, star, car) using graphics primitive
STAR-

(a) Syntax to draw star with five vertices

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
line(100,100,80,150);
line(100,100,120,150);
line(75,117,125,117);
line(125,117,80,150);
line(120,150,75,117);
getch();
}

(b) Syntax to draw star with five vertices

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
line(100,100,80,150);
line(80,150,120,150);
line(120,150,100,100);
line(100,167,80,117);
line(80,117,120,117);
line(120,117,100,167);
getch();
}

(c) Syntax to draw star using drawpoly

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");

int i=2;
int
arr[]={100*i,100*i,90*i,117*i,72*i,117*i,85*i,130*i,80*i,150*i,100*i,134*i,120*i,150
*i,115*i,130*i,128*i,117*i,110*i,117*i,100*i,100*i};
setfillstyle(9,WHITE);
setbkcolor(BLACK);
drawpoly(11,arr);
fillpoly(11,arr);
getch();

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
}

CAR - A moving car using graphics primitive functions

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
int x=0;
for(x=-30;x<500;x=x+15)
{
setcolor(8);
setlinestyle(4,0,WHITE);
line(0,185,700,185);
setfillstyle(6,3);
bar(70+x,140,240+x,170);
bar(100+x,100,200+x,140);
setfillstyle(1,15);
bar(120+x,115,140+x,140);
bar(160+x,115,180+x,140);
setfillstyle(7,WHITE);
pieslice(120+x,170,0,360,15);
pieslice(200+x,170,0,360,15);
if(x<20)
delay(300);
if(x<100&&x>20)

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
delay(200);
if(x<190&&x>100)
delay(100);
else
delay(50);
cleardevice();
}
getch();
}

HUT- A hut with sun, tree and birds.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
setfillstyle(7,8);
circle(100,235,9);
floodfill(100,235,15);
line(100,200,125,250);
line(100,200,75,250);
line(80,240,80,350);

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
line(120,240,120,350);
line(120,350,80,350);
setfillstyle(6,6);
rectangle(90,305,110,349);
floodfill(91,306,15);
line(100,200,200,150);
line(200,150,225,200);
line(125,250,225,200);
line(220,204,220,300);
line(220,300,120,350);
line(120,190,145,240);
line(140,180,165,230);
line(160,170,185,220);
line(180,160,205,210);
setfillstyle(6,10);
floodfill(102,250,15);
setfillstyle(9,10);
floodfill(215,250,15);
setfillstyle(1,4);
floodfill(105,200,15);
setfillstyle(5,4);
floodfill(125,200,15);
setfillstyle(9,4);
floodfill(145,200,15);
setfillstyle(1,4);
floodfill(165,200,15);
setfillstyle(9,4);
floodfill(180,200,15);
setfillstyle(1,4);
floodfill(205,200,15);
line(150,265,190,245);
line(150,265,150,310);
line(190,245,190,290);
line(190,290,150,310);
line(150,287,190,267);
line(170,255,170,300);
setcolor(14);
setfillstyle(9,14);
circle(400,100,35);
floodfill(400,100,14);
line(400,65,400,35);

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
line(400,135,400,165);
line(365,100,335,100);
line(435,100,465,100);
line(375,75,350,50);
line(425,125,450,150);
line(425,75,450,50);
line(375,125,350,150);
line(352,79,368,86);
line(432,88,448,82);
line(432,112,448,118);
line(368,112,352,118);
line(388,68,382,52);
line(412,68,418,52);
line(385,132,379,147);
line(415,132,421,147);
setbkcolor(7);
setcolor(8);
arc(260,95,0,90,5);
arc(270,95,90,180,5);
arc(220,90,0,90,5);
arc(230,90,90,180,5);
arc(240,110,0,90,5);
arc(250,110,90,180,5);
setcolor(2);
setfillstyle(2,2);
ellipse(380,268,0,360,60,20);
floodfill(380,268,2);
ellipse(380,238,0,360,50,20);
floodfill(380,238,2);
ellipse(380,208,0,360,30,20);
floodfill(380,208,2);
setfillstyle(9,6);
setcolor(6);
arc(270,325,335,25,90);
arc(490,325,155,205,90);
ellipse(380,268,240,300,60,20);
line(350,363,410,363);
floodfill(380,325,6);
getch();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-1(a)
To draw a line using simple DDA algorithm for positive line slope
A digital differential analyzer (DDA) is hardware or software used for linear
interpolation of variables over an interval between start and end point. DDAs are
used for rasterization of lines, triangles and polygons. In its simplest implementation,
the DDA algorithm interpolates values in interval by computing for each xi in the
equations.

Syntax:-

#include<iostream.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
void dda_line(int x1,int y1,int x2,int y2)
{
int dx=(x2-x1);
int dy=(y2-y1);
int length;
if(abs(dy)>abs(dx))
length=abs(dy);
else
length=abs(dx);
float xinc,yinc,x=x1,y=y1;
xinc=dx/(float)length;
yinc=dy/(float)length;

putpixel(round(x),round(y),15);
for(int k=1;k<=length;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),15);
delay(100);
}
}
void main()
{

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
clrscr();
int x1,x2,y1,y2;
int gd=DETECT,gm;
cout<<"Enter the x-coordinate of starting point : ";
cin>>x1;
cout<<"Enter the y-coordinate of ending point : ";
cin>>y1;
cout<<endl;
cout<<"Enter the x-coordinate of starting point : ";
cin>>x2;
cout<<"Enter the y-coordinate of ending point : ";
cin>>y2;
getch();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
dda_line(x1,y1,x2,y2);
setcolor(4);
getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-2(a)
To draw a line using simple DDA algorithm for negative line slope
A digital differential analyzer (DDA) is hardware or software used for linear
interpolation of variables over an interval between start and end point. DDAs are
used for rasterization of lines, triangles and polygons. In its simplest implementation,
the DDA algorithm interpolates values in interval by computing for each xi in the
equations.

Syntax:-

#include<iostream.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
void dda_line(int x1,int y1,int x2,int y2)
{
int dx=(x2-x1);
int dy=(y2-y1);
int length;
if(abs(dy)>abs(dx))
length=abs(dy);
else
length=abs(dx);
float xinc,yinc,x=x1,y=y1;
xinc=dx/(float)length;
yinc=dy/(float)length;

putpixel(round(x),round(y),15);
for(int k=1;k<=length;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),15);
delay(100);
}
}
void main()
{

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
clrscr();
int x1,x2,y1,y2;
int gd=DETECT,gm;
cout<<"Enter the x-coordinate of starting point : ";
cin>>x1;
cout<<"Enter the y-coordinate of ending point : ";
cin>>y1;
cout<<endl;
cout<<"Enter the x-coordinate of starting point : ";
cin>>x2;
cout<<"Enter the y-coordinate of ending point : ";
cin>>y2;
getch();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
dda_line(x1,y1,x2,y2);
setcolor(4);
getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-9
To draw a circle with given centre and radius using circle generator algorithm

Syntax:-

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<math.h>
#define round(a) ((int)(a+0.5))
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");

// METHOD1
int xc=100,yc=150,r=50;
float x=0,y=0;
for(int i=0;i<=45;i++)
{
double ang=double(i)*(3.142/180);
x = r*cos(ang);
y = r*sin(ang);
putpixel(xc+round(x),yc+round(y),15);
putpixel(xc-round(x),yc+round(y),15);
putpixel(xc+round(x),yc-round(y),15);
putpixel(xc-round(x),yc-round(y),15);
putpixel(xc+round(y),yc+round(x),15);
putpixel(xc-round(y),yc+round(x),15);
putpixel(xc+round(y),yc-round(x),15);
putpixel(xc-round(y),yc-round(x),15);
delay(100);
}

//METHOD2
x=0;y=r;
xc=300,yc=150;
for(x=0;x<y;x++)
{
double temp=((r*r)-(x*x));
y=sqrt(temp);

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
putpixel(xc+round(x),yc+round(y),15);
putpixel(xc-round(x),yc+round(y),15);
putpixel(xc+round(x),yc-round(y),15);
putpixel(xc-round(x),yc-round(y),15);
putpixel(xc+round(y),yc+round(x),15);
putpixel(xc-round(y),yc+round(x),15);
putpixel(xc+round(y),yc-round(x),15);
putpixel(xc-round(y),yc-round(x),15);
delay(100);
}
getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-1(C)
To draw a line using bresenhams algorithm for positive line slope

Syntax:-

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
int sign(int x)
{
if(x<0)
return(-1);
if(x>0)
return(1);
else
return(0);
}
void lineBres(int xa,int ya,int xb,int yb)
{
int sx,sy,t,length,flag;
int x=xa;
int y=ya;
int dx=abs(xa-xb),dy=abs(ya-yb);
sx=sign(xb-xa);
sy=sign(yb-ya);
if(dy>dx)
{
t=dx;
dx=dy;
dy=t;
length=dy;
flag=1;
}
else
{
length=dx;
flag=0;
}
int p=(2*dy)-dx;

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
int twoDx=2*dx,twoDy=2*dy;
putpixel(x,y,15);
delay(50);
for(int i=0;i<length;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(50);
}
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
int xa,ya,xb,yb;
cout<<"Enter the starting point of x :";
cin>>xa;
cout<<"Enter the starting point of y :";
cin>>ya;
cout<<"Enter the ending point of x :";
cin>>xb;
cout<<"Enter the ending point of x :";
cin>>yb;
cleardevice();
lineBres(xa,ya,xb,yb);
getch();
closegraph();

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-2(C)
To draw a line using bresenhams algorithm for negative line slope

Syntax:-

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
int sign(int x)
{
if(x<0)
return(-1);
if(x>0)
return(1);
else
return(0);
}
void lineBres(int xa,int ya,int xb,int yb)
{
int sx,sy,t,length,flag;
int x=xa;
int y=ya;
int dx=abs(xa-xb),dy=abs(ya-yb);
sx=sign(xb-xa);
sy=sign(yb-ya);
if(dy>dx)
{
t=dx;
dx=dy;
dy=t;
length=dy;
flag=1;
}
else
{
length=dx;
flag=0;
}
int p=(2*dy)-dx;

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
int twoDx=2*dx,twoDy=2*dy;
putpixel(x,y,15);
delay(50);
for(int i=0;i<length;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(50);
}
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
int xa,ya,xb,yb;
cout<<"Enter the starting point of x :";
cin>>xa;
cout<<"Enter the starting point of y :";
cin>>ya;
cout<<"Enter the ending point of x :";
cin>>xb;
cout<<"Enter the ending point of x :";
cin>>yb;
cleardevice();
lineBres(xa,ya,xb,yb);
getch();
closegraph();

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-5(a)
To perform translation transformation on a given triangle

Syntax:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
int x1=100,y1=100,x2=80,y2=150,x3=120,y3=150,tx,ty;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
cout<<"Enter translation value among x-axis :";
cin>>tx;
cout<<"Enter translation value among y-axis :";
cin>>ty;
x1+=tx;x2+=tx;x3+=tx;
y1+=ty;y2+=ty;y3+=ty;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-5(b)
To perform scaling transformation on a given triangle

Syntax:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
int x1=100,y1=100,x2=80,y2=150,x3=120,y3=150,sx,sy;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
cout<<"Enter scaling value among x-axis :";
cin>>sx;
cout<<"Enter scaling value among y-axis :";
cin>>sy;
x1*=sx;x2*=sx;x3*=sx;
y1*=sy;y2*=sy;y3*=sy;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-5(c)
To perform rotation transformation on a given triangle

Syntax:-
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://turboc3//bgi");
int x1=200,y1=200,x2=180,y2=250,x3=220,y3=250;
float angle;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
cout<<"Enter the rotation angle : ";
cin>>angle;
angle=angle*3.1428/180;
x1=x1*cos(angle)-y1*sin(angle);
y1=(x1*sin(angle))+y1*cos(angle);
x2=x2*cos(angle)-y2*sin(angle);
y2=(x2*sin(angle))+y2*cos(angle);
x3=x3*cos(angle)-y3*sin(angle);
y3=(x3*sin(angle))+y3*cos(angle);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-8
To draw a circle with given centre and radius by using midpoint circle algorithm
Syntax:-
#include<iostream.h>
#include<conio.h>
# include<graphics.h>
#include<math.h>
#include<dos.h>
#define round(a) ((int)a+0.5)
void putcircle(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);
}
void circlemid(int xc,int yc,float r)
{
float x=0,y=r;
int p=1-r;
while(x<y)
{
x++;
if(p<0)
p=p+(2*x)+1;
else
{
y--;
p=p+(2*(x-y)+1);
}
putcircle(xc,yc,round(x),round(y));
delay(10);
}
}
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int xc,yc,r;
cout<<"Enter centre co-ordinates:";
cin>>xc>>yc;
cout<<"Enter radius:";

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
cin>>r;
circlemid(xc,yc,r);
setcolor(10);
circle(xc,yc,r);
getch();
closegraph();
}
OUTPUT

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-6
To rotate a given triangle clockwise and anticlockwise about a given point

(a) To rotate clockwise

Syntax:-

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x1,y1,x2,y2,x3,y3;
cout<<"Enter (x1,y1),(x2,y2),(x3,y3) for triangle : ";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
int a[]={x1,y1,x2,y2,x3,y3,x1,y1};
drawpoly(4,a);
int xf=(x1+x2+x3)/3;
int yf=(y1+y2+y3)/3;
float ang;
cout<<"Enter the rotation angle :";
cin>>ang;
float rad=ang*3.1428/180;
int X1=(x1-xf)*cos(rad)-(y1-yf)*sin(rad)+xf;
int Y1=(x1-xf)*sin(rad)+(y1-yf)*cos(rad)+yf;
int X2=(x2-xf)*cos(rad)-(y2-yf)*sin(rad)+xf;
int Y2=(x2-xf)*sin(rad)+(y2-yf)*cos(rad)+yf;
int X3=(x3-xf)*cos(rad)-(y3-yf)*sin(rad)+xf;
int Y3=(x3-xf)*sin(rad)+(y3-yf)*cos(rad)+yf;
int b[]={X1,Y1,X2,Y2,X3,Y3,X1,Y1};
drawpoly(4,b);
getch();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
(b) To rotate anti-clockwise

Syntax:-

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x1,y1,x2,y2,x3,y3;
cout<<"Enter (x1,y1),(x2,y2),(x3,y3) for triangle : ";
cin>>x1>>y1>>x2>>y2>>x3>>y3;
int a[]={x1,y1,x2,y2,x3,y3,x1,y1};
drawpoly(4,a);
int xf=(x1+x2+x3)/3;
int yf=(y1+y2+y3)/3;
float ang;
cout<<"Enter the rotation angle :";
cin>>ang;
float rad=ang*3.1428/180;
int X1=(x1-xf)*cos(rad)+(y1-yf)*sin(rad)+xf;
int Y1=-(x1-xf)*sin(rad)+(y1-yf)*cos(rad)+yf;
int X2=(x2-xf)*cos(rad)+(y2-yf)*sin(rad)+xf;
int Y2=-(x2-xf)*sin(rad)+(y2-yf)*cos(rad)+yf;
int X3=(x3-xf)*cos(rad)+(y3-yf)*sin(rad)+xf;
int Y3=-(x3-xf)*sin(rad)+(y3-yf)*cos(rad)+yf;
int b[]={X1,Y1,X2,Y2,X3,Y3,X1,Y1};
drawpoly(4,b);
getch();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM-10
To display 4-bit region code for end points of a line and check whether line is completely
on screen or off the screen
Syntax:-

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
int xmax=400,ymax=300,xmin=200,ymin=150;
line(xmin,0,xmin,getmaxy());
line(xmax,0,xmax,getmaxy());
line(0,ymax,getmaxx(),ymax);
line(0,ymin,getmaxx(),ymin);
cout<<"Enter the endpoints of the line :";
int x[2],y[2],num[2];
cin>>x[0]>>y[0]>>x[1]>>y[1];
line(x[0],y[0],x[1],y[1]);
for(int i=0;i<2;i++)
{
int bit1=0,bit2=0,bit3=0,bit4=0;
if(y[i]<ymin)
bit1=1;
if(y[i]>ymax)
bit2=1;
if(x[i]>xmax)
bit3=1;
if(x[i]<xmin)
bit4=1;
cout<<For <<i<<th endpoint region code is :<<bit1<<bit2<<bit3<<bit4;
num[i]=bit4*1+bit3*2+bit2*4+bit1*8;
}
if(!(num[0]|num[1]))
cout<<"Line is completely in window";
else if(!(num[0]&num[1]))
cout<<"Line is required to be clipped";
else
cout<<"Line is off the window";
getch();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
PROGRAM-11
To clip a line intersecting at one point with given window using Cohen
Sutherland Line Clipping algorithm.
Syntax:-

#include"stdio.h"
#include"conio.h"
#include"graphics.h"
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"");
printf("\n\tEnter the bottom-left coordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\nEnter the coordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("\nEnter the coordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)
{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1 <xmin) start[0]=1;
if(x1 >xmax) start[1]=1;
if(y1 >ymax) start[2]=1;
if(y1 <ymin) start[3]=1;
if(x2 <xmin) end[0]=1;
if(x2 >xmax) end[1]=1;
if(y2 >ymax) end[2]=1;
if(y2 <ymin) end[3]=1;
for(i=0;i <4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]
==0)&&(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
}
else
{
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
clrscr();
cleardevice();

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
PROGRAM-12
To clip a line intersecting at Two or more points with given window using
Cohen Sutherland Line Clipping algorithm.
Syntax:-

#include"stdio.h"
#include"conio.h"
#include"graphics.h"
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"");
printf("\n\tEnter the bottom-left coordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\nEnter the coordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("\nEnter the coordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)
{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1 <xmin) start[0]=1;
if(x1 >xmax) start[1]=1;
if(y1 >ymax) start[2]=1;
if(y1 <ymin) start[3]=1;
if(x2 <xmin) end[0]=1;
if(x2 >xmax) end[1]=1;
if(y2 >ymax) end[2]=1;
if(y2 <ymin) end[3]=1;
for(i=0;i <4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]
==0)&&(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
getch();
}
else
{
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
clrscr();

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
PROGRAM-13
To display the result of window to viewport transformation.
Syntax:-
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
float wxmin=10,wxmax=150,wymin=10,wymax=250;
float vxmin=200,vxmax=600,vymin=10,vymax=250;
int wx1=30,wy1=50,wx2=100,wy2=180;
rectangle(wxmin,wymin,wxmax,wymax);
rectangle(vxmin,vymin,vxmax,vymax);
float sx=(vxmax-vxmin)/(wxmax-wxmin);
float sy=(vymax-vymin)/(wymax-wymin);
line(wx1,wy1,wx2,wy2);
float vx1=sx*(wx1-wxmin)+vxmin;
float vy1=sy*(wy1-wymin)+vymin;
float vx2=sx*(wx2-wxmin)+vxmin;
float vy2=sy*(wy2-wymin)+vymin;
line(vx1,vy1,vx2,vy2);
outtextxy(60,260,"Window");
outtextxy(360,260,"Viewport");
getch();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
PROGRAM 1(b)
To draw a line using Symmetrical DDA Algorithm for positive line slope.
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#define ROUND(a)((int)(a+0.5))
void symDDA(int xa,int ya,int xb,int yb)
{
int dx=xb-xa,dy=yb-ya;float length;
float xinc,yinc,x=xa,y=ya;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dy);
float n=log10(length)/log10(2);
xinc=dx/(pow(2,n));
yinc=dy/(pow(2,n));
putpixel(ROUND(x),ROUND(y),15);
delay(50);
for(int i=0;i<length;i++)
{
x=x+xinc;
y=y+yinc;
putpixel(ROUND(x),ROUND(y),15);
delay(50);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int xa,xb,ya,yb;
cout<<"enter the points";
cin>>xa>>xb>>ya>>yb;
cleardevice();
symDDA(xa,xb,ya,yb);
getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

PROGRAM 2(b)
To draw a line using Symmetrical DDA for negative line slope.
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#define ROUND(a)((int)(a+0.5))
void symDDA(int xa,int ya,int xb,int yb)
{
int dx=xb-xa,dy=yb-ya;float length;
float xinc,yinc,x=xa,y=ya;
if(abs(dx)>abs(dy))
length=abs(dx);
else
length=abs(dy);
float n=log10(length)/log10(2);
xinc=dx/(pow(2,n));
yinc=dy/(pow(2,n));
putpixel(ROUND(x),ROUND(y),15);
delay(50);

for(int i=0;i<length;i++)
{
x=x+xinc;
y=y+yinc;
putpixel(ROUND(x),ROUND(y),15);
delay(50);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int xa,xb,ya,yb;
cout<<"enter the points";
cin>>xa>>xb>>ya>>yb;
cleardevice();
symDDA(xa,xb,ya,yb);
getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
PROGRAM-14
To calculate and draw a Bezier curve passing through four control points.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int i;
initgraph (&gd,&gm," ");
int x[4],y[4];
cout<< "Enter the x- and y-coordinates of the four control points.\n";
for (i=0; i<4; i++)
cin>>x[i]>>y[i];
double u;
for (u= 0.0; u<1.0; u=u+0.0005)
{
double xt = pow (1-u,3) * x[0] + 3 * u * pow (1-u,2) * x[1] +
3 * pow (u, 2) * (1-u) * x[2] + pow (u, 3) * x[3];

double yt = pow (1-u,3) * y[0] + 3 * u * pow (1-u, 2) * y[1] +


3 * pow (u, 2) * (1-u) * y[2] + pow (u,3) * y[3];
putpixel (xt, yt, YELLOW);
}
getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
PROGRAM-15
To draw a B-Spline curve.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
double basis(int i,int k,double knot[],double stpos)
{
double val; //recursion
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 gm,gd=DETECT;
int xc[6]={10,80,250,400,500,550}; //control points
int yc[6]={180,130,10,700,50,70};
double knot[]={0,1,2,3,4,5,6,7}; //knot vector
initgraph(&gd,&gm," ");
int k=4,i;
double bas,stpos=knot[k-1],endpos=knot[8-k],slice=(endpos-stpos)/100;
double x,y,lx,ly;
lx=xc[0]; //first point
ly=yc[0];
for(;stpos<endpos;stpos+=slice)
{
x=y=0;
for(i=1;i<=6;i++)
{
bas=basis(i-1,k,knot,stpos);
x=x+(xc[i-1]*bas); //x,y for bspline curves
y=y+(yc[i-1]*bas);
}
line(lx,ly,x,y);
lx=x; //last point
ly=y;

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
}

getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN
PROGRAM-7
To perform reflection of a point about a line y= mx+c
#include<iostream.h>
#include<conio.h>
#include<graphics.h>

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int x1,y1,x2,y2;
cout<<"Enter the coordinates of the line\n";
cin>>x1>>y1>>x2>>y2;
int m=getmaxx();
int n=getmaxy();
setcolor(6);
line(x1,y1,x2,y2);
outtextxy(x1,y1+10,"Original Object");
setcolor(4);
line((m/2),0,(m/2),n);
line(0,(n/2),m,(n/2));
setcolor(3);
int c=(n/2)-y1;
int d=(n/2)-y2;
y2=y2+(d*2);
y1=y1+(c*2);
line(x2,y2,x1,y1);
outtextxy(x1,y1+10,"Reflection along X-axis");
setcolor(9);
int a=(m/2)-x1;
int b=(m/2)-x2;
x1=x1+(a*2);
x2=x2+(b*2);
line(x1,y1,x2,y2);
outtextxy(x2-20,y2+10,"Reflection along Y-axis");
getch();
closegraph();
}

NAME:MOHIT JAIN
UID:13BCS1157
CHANDIGARH UNIVERSITY,GHARUAN

NAME:MOHIT JAIN
UID:13BCS1157

You might also like