0% found this document useful (0 votes)
508 views37 pages

Write A Program To Draw The Line Using Line Function.: Output

The document contains C++ programs to perform various 2D geometric transformations and drawings using graphics functions. It includes programs to: 1) Draw basic shapes like lines, rectangles, ellipses and circles using functions like line(), rectangle(), ellipse(), circle(). 2) Perform transformations like translation, reflection about X and Y axes on a triangle. 3) Draw curves using midpoint and Bresenham's algorithms for circle, ellipse.

Uploaded by

AshleyKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
508 views37 pages

Write A Program To Draw The Line Using Line Function.: Output

The document contains C++ programs to perform various 2D geometric transformations and drawings using graphics functions. It includes programs to: 1) Draw basic shapes like lines, rectangles, ellipses and circles using functions like line(), rectangle(), ellipse(), circle(). 2) Perform transformations like translation, reflection about X and Y axes on a triangle. 3) Draw curves using midpoint and Bresenham's algorithms for circle, ellipse.

Uploaded by

AshleyKumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 37

Write a program to draw the line using line function.

OUTPUT:-

Write a program to draw the line using line function


#include<graphics.h>
#include<iostream.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gm,&gd," ");
int x1,y1,x2,y2;
setcolor(YELLOW);
cout<<"enter the valueof x1:=";
cin>>x1;
cout<<"enter the value of y1:=";
cin>>y1;
cout<<"enter the value of x2:=";
cin>>x2;
cout<<"enter the value of y2:=";
cin>>y2;
line(x1,y1,x2,y2);
getch();
closegraph();
}

Write a program to draw the rectangle using rectangle function.


OUTPUT:-

Write a program to draw the rectangle using rectangle function


#include<graphics.h>
#include<iostream.h>
#include<conio.h>
void main()

{
int gd=DETECT,gm;
initgraph(&gm,&gd," ");
int x1,y1,x2,y2,radius,i;
setcolor(YELLOW);
cout<<"enter the valueof x1:=";
cin>>x1;
cout<<"enter the value of y1:=";
cin>>y1;
cout<<"enter the value of x2:=";
cin>>x2;
cout<<"enter the value of y2:=";
cin>>y2;
rectangle(x1,y1,x2,y2);
getch();
closegraph();
}

Write a program to draw the Ellipse using ellipse function.


OUTPUT:-

Write a program to draw the Ellipse using ellipse function.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int x,y,sa,ea,sr,er;
cout<<"enter the value of x:=";
cin>>x;
cout<<"enter the value of y:=";
cin>>y;
cout<<"enter the starting angle:=";
cin>>sa;
cout<<"enter the ending angle:=";
cin>>ea;
cout<<"enter the x radius:=";
cin>>sr;
cout<<"enter the y radius:=";
cin>>er;
ellipse(x,y,sa,ea,sr,er);
getch();
}

Write a program to draw the circle using circle function.


OUTPUT:-

Write a program to draw the circle using circle function


#include<graphics.h>
#include<iostream.h>
#include<conio.h>
void main()

{
int gd=DETECT,gm;
initgraph(&gm,&gd," ");
int x,y,radius,i;
setcolor(YELLOW);
cout<<"enter the valueof x:=";
cin>>x;
cout<<"enter the value of y:=";
cin>>y;
cout<<"enter the radius:=";
cin>>radius;
circle(x,y,radius);
getch();
closegraph();
}

Write a program to draw the triangle using line function.

OUTPUT

Write a program to draw the triangle using line function.


#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int x1,y1,x2,y2,x3,y3;
setcolor(RED);
cout<<"enter the value of x1:=";
cin>>x1;
cout<<"enter the value of y1:=";
cin>>y1;
cout<<"enter the value of x2:=";
cin>>x2;
cout<<"enter the value of y2:=";
cin>>y2;
cout<<"enter the value of x3:=";
cin>>x3;
line(x1,y1,x2,y2);
line(x2,y2,x3,y1);
line(x3,y1,x1,y1);
getch();
closegraph();}

Write a program to draw line using DDA algorithm.

OUTPUT

Write a program to draw line using DDA algorithm


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int x1,y1,x2,y2,dx,dy,k;
float x,y,steps=0,m=0;
cout<<"enter the value of x1:=";
cin>>x1;
cout<<"enter the value of y1:=";
cin>>y1;
cout<<"enter the value of x2:=";
cin>>x2;
cout<<"enter the value of y2:=";
cin>>y2;
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
if(abs(dx)>abs(dy))
{
steps=abs(dx);

}
else
{
steps=abs(dy);
}
x=x1;
y=y1;
putpixel(x,y,1);
for(k=0;k<steps;k++)
{
x++;
y=y+m;
putpixel(x,y,1);
}
getch();
closegraph();
}

Write a program to draw line using Bresenhams line algorithm.


OUTPUT

Write a program to draw line using Bresenhams line algorithm.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int x1,y1,x2,y2,dx,dy,k,pk,pk2,x,y;
cout<<"enter the value of x1:=";
cin>>x1;
cout<<"enter the value of y1:=";
cin>>y1;
cout<<"enter the value of x2:=";
cin>>x2;
cout<<"enter the value of y2:=";
cin>>y2;
x=x1;
y=y1;
putpixel(x,y,1);
dx=x2-x1;
cout<<"value of dx:="<<dx<<endl;
dy=y2-y1;
cout<<"value of dy:="<<dy<<endl;

pk=2*dy-dx;
cout<<"inital value:="<<pk;
while(x<x2)
{
x++;
if(pk<0)
{
pk2=pk+2*dy;
}
else
{
y++;
pk2=pk+2*dy-2*dx;
}
putpixel(x,y,1);
}
getch();
closegraph();
}

Write a program to draw circle using midpoint circle algorithm.


OUTPUT

Write a program to draw circle using midpoint circle algorithm.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int x=0,y,r,p,xc,yc;
cout<<"enter the value of r:=";
cin>>r;
cout<<"enter the value of xc:=";
cin>>xc;
cout<<"enter the value of yc:=";
cin>>yc;
y=r;
p=1-r;
cout<<"initial value:="<<p;
while(x<y)
{
if(p<0)
{
x++;
p=p+2*x+3;
}

else
{
x++;
y--;
p=p+2*(x-y)+5;
}
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
}
getch();
}

Program to draw a circle using Bresenhams circle algorithm.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm, "");
float xc,yc,p,x,y,r;
cout<<"enter the coordinates of center of the circle :";
cin>>xc>>yc;
cout<<"enter the radius of the circle : ";
cin>>r;
p=(3/2)*r;
x=0; y=r;
while(x<=y)
{
if(p<0)
p=p+(4*x)+6;
else
{
p=p+(4*x)-(4*y)+10;
y--;
}
x++;

putpixel(xc+x,yc+y,WHITE);
putpixel(xc+y,yc+x, WHITE;
putpixel(xc-x,yc+y, WHITE);
putpixel(xc+y,yc-x, WHITE);
putpixel(xc-x,yc-y, WHITE);
putpixel(xc-y,yc+x, WHITE);
putpixel(xc+x,yc-y, WHITE);
putpixel(xc-y,yc-x, WHITE);
}
getch();
closegraph();
}

Program to draw the ellipse using midpoint algorithm:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,xc,yc,rx,ry,rx2,ry2,x,y,p0,p1,py,px,trx2,try2;
initgraph(&gd,&gm, "");
cout<<"\n\t Enter the coordinates of center of ellipse: ";
cin>>xc>>yc;
cout<<"\n\t Now enter the x-radius: ";
cin>>rx;
cout<<"\n\t Now enter the y-radius: ";
cin>>ry;
rx2=rx*rx;
ry2=ry*ry;
trx2=2*rx2;
try2=2*ry2;
x=0;y=ry;px=0;py=trx2*y;
// Region 1 ..!
p0=abs(ry2-(rx2*ry2)+(0.25*rx2));
while(px<py)
{
x++;

px+=try2;
if(p0<0)
p0+=ry2+px;
else
{
y--;
py-=trx2;
p0+=ry2+px-py;
}
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y, WHITE);
putpixel(xc+x,yc-y, WHITE);
putpixel(xc-x,yc-y, WHITE);
delay(100);
}
p1=abs(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y--;
py-=trx2;
if(p1>0)
p1+=rx2-py;
else
{
x++;

px+=try2;
p1=rx2-py+px;
}
putpixel(xc+x,yc+y, WHITE);
putpixel(xc-x,yc+y, WHITE);
putpixel(xc+x,yc-y, WHITE);
putpixel(xc-x,yc-y, WHITE);
delay(100);
}
getch();
closegraph();
}

Write a program to Translate 2D triangle

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm, "");
int xa,xb,ya,yb,xc,tx,ty,yc,x,y;
cout<<"enter the coordinate of triangle:=";
cin>>xa>>ya>>xb>>yb>>xc>>yc;
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";

cout<<"enter the translation vector tx:=";

cin>>x;
cout<<"enter the translation vector ty:=";
cin>>y;
xa=x+xa;
xb=x+xb;
ya=y+ya;
yb=y+yb;
xc=x+xc;
yc=y+yc;
line(xa,ya,xb,yb);
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);
getch();
}

Write a program to Perform 2D reflection about X-axis.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm, "");
int xa,xb,ya,yb,xc,yc,i,j,k;
int a[3][3],b[3][3],c[3][3];
cout<<"enter the coordinate of triangle:=";
cin>>xa>>ya>>xb>>yb>>xc>>yc;
line(xa,ya,xb,yb);
line(xa,ya,xc,yc);
line(xb,yb,xc,yc);
cout<<"Reflection about xaxis:=";
a[0][0]=1;
a[0][1]=0;
a[0][2]=0;
a[1][0]=0;
a[1][1]=-1;
a[1][2]=480;
a[2][0]=0;
a[2][1]=0;
a[2][2]=1;
b[0][0]=xa;

b[0][1]=xb;
b[0][2]=xc;
b[1][0]=ya;
b[1][1]=yb;
b[1][2]=yc;
b[2][0]=1;
b[2][1]=1;
b[2][2]=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
xa=c[0][0];
xb=c[0][1];
xc=c[0][2];
ya=c[1][0];
yb=c[1][1];
yc=c[1][2];

line(xa,ya,xb,yb);
line(xa,ya,xc,yc);
line(xb,yb,xc,yc);
getch();
}

Write a program to Perform 2D reflection about Y-axis.


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm, "");
int xa,xb,ya,yb,xc,yc,i,j,k;
int a[3][3],b[3][3],c[3][3];
cout<<"enter the coordinate of triangle:=";
cin>>xa>>ya>>xb>>yb>>xc>>yc;
line(xa,ya,xb,yb);
line(xa,ya,xc,yc);
line(xb,yb,xc,yc);
cout<<"Reflection about yaxis:=";
a[0][0]=-1;
a[0][1]=0;
a[0][2]=640;
a[1][0]=0;
a[1][1]=1;
a[1][2]=0;
a[2][0]=0;

a[2][1]=0;
a[2][2]=1;
b[0][0]=xa;
b[0][1]=xb;
b[0][2]=xc;
b[1][0]=ya;
b[1][1]=yb;
b[1][2]=yc;
b[2][0]=1;
b[2][1]=1;
b[2][2]=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
xa=c[0][0];
xb=c[0][1];
xc=c[0][2];

ya=c[1][0];
yb=c[1][1];
yc=c[1][2];
line(xa,ya,xb,yb);
line(xa,ya,xc,yc);
line(xb,yb,xc,yc);
getch();
}

You might also like