Programs: // Program To Implement Scan Conversion of Line Using Direct/Polynomial Method
Programs: // Program To Implement Scan Conversion of Line Using Direct/Polynomial Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
float x,y,x1,y1,x2,y2,xend,yend,m,c,dx,dy;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
clrscr();
printf("Enter starting coordinates:");
scanf("%f%f",&x1,&y1);
printf("Enter ending coordinates:");
scanf("%f%f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
c=y1-m*x;
if(dx<0)
{
x=x2;
y=y2;
xend=x1;
yend=y1;
}
else
{
x=x1;
y=y1;
xend=x2;
yend=y2;
}
putpixel(x,y,WHITE);
if(abs(m)<=1)
{
while(x<=xend)
{
x=x+1;
y=m*x+c;
putpixel(x,y,WHITE);
}
}
else
{
while(y<=yend)
{
y=y+1;
x=(y-c)/m;
putpixel(x,y,WHITE);
}
}
getch();
}
// Program to draw a line using Digital Differential
Analyser(DDA) Method
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
float x,y,x1,y1,x2,y2,xend,yend,m,m_inv,dx,dy;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
clrscr();
printf("Enter starting coordinates:");
scanf("%f%f",&x1,&y1);
printf("Enter ending coordinates:");
scanf("%f%f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
m_inv=1/m;
if(dx<0)
{
x=x2;
y=y2;
xend=x1;
yend=y1;
}
else
{
x=x1;
y=y1;
xend=x2;
yend=y2;
}
putpixel(x,y,WHITE);
if(abs(m)<=1)
{
while(x<=xend)
{
x=x+1;
y=y+m;
putpixel(x,y,WHITE);
}
}
else
{
while(y<=yend)
{
x=x+m_inv;
y=y+1;
putpixel(x,y,WHITE);
}
}
getch();
}
// Program to draw a line using Breshenham's Method with
slope between 0 and 1 (0<m<1)
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int x,y,x1,y1,x2,y2,dx,dy,ds,dt,d;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"enter starting coordinates:";
cin>>x1>>y1;
cout<<"enter ending coordinates:";
cin>>x2>>y2;
dx=x2-x1;
dy=y2-y1;
d=2*dy-dx;
ds=2*dy;
dt=2*(dy-dx);
x=x1;
y=y1;
while(x<=x2)
{
x=x+1;
if(d<0)
{
d=d+ds;
}
else
{
y=y+1;
d=d+dt;
}
putpixel(x,y,WHITE);
}
getch();
}
// Program to draw a line using Breshenham's Method with
slope greater than 1 (m>1)
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int x,y,x1,y1,x2,y2,dx,dy,ds,dt,d;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"enter starting coordinates:";
cin>>x1>>y1;
cout<<"enter ending coordinates:";
cin>>x2>>y2;
dx=x2-x1;
dy=y2-y1;
d=2*dx-dy;
ds=2*dx;
dt=2*(dx-dy);
x=x1;
y=y1;
while(y<=y2)
{
y=y+1;
if(d<0)
{
d=d+ds;
}
else
{
x=x+1;
d=d+dt;
}
putpixel(x,y,WHITE);
}
getch();
}
// Program to draw a line using Breshenham's Method with
slope less than 0 (m<0)
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int x,y,x1,y1,x2,y2,dx,dy,ds,dt,d;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"enter starting coordinates:";
cin>>x1>>y1;
cout<<"enter ending coordinates:";
cin>>x2>>y2;
dx=x2-x1;
dy=y2-y1;
d=-2*dx-dy;
ds=2*dx;
dt=2*(dx+dy);
x=x1;
y=y1;
while(y<=y2)
{
y=y-1;
if(d<0)
{
d=d-ds;
}
else
{
x=x+1;
d=d-dt;
}
putpixel(x,y,WHITE);
}
getch();
}
// Program to draw a line when slope lies between
(0<m<1,m>1,m<0) using Breshenham’s Method
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void show();
void display();
void run();
void main()
{
int x,y,x1,y1,x2,y2,d,dx,dy,ds,dt;
int op;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm," ");
cout<<"Enter initial co-ordinates:";
cin>>x1>>y1;
cout<<"Enter endind co-ordinates:";
cin>>x2>>y2;
do
{
cout<<"Enter option(slope lies between 0&1):1";
cout<<"Enter option(slope>1):2";
cout<<"Enter option(slope<0):3";
cout<<"Enter option(for exit):4";
cout<<"Enter option";
cin>>op;
switch(op)
{
case 0:
get();
break;
case 1:
display();
break;
case 2:
run();
break;
case 3:
exit(1);
default:
cout<<"enter vaid option!!!!";
}
}while(1); //end of do-while statement
void get()
{
dx=x2-x1;
dy=y2-y1;
d=2*dy-dx;
ds=2*dy;
dt=2*(dy-dx);
x=x1;
y=y1;
while(x<=x2)
{
x=x+1;
if(d<0)
{
d=d+ds;
}
else
{
y=y+1;
d=d+dt;
}
putpixel(x,y,WHITE);
}
} //end of get()
void display()
{
dx=x2-x1;
dy=y2-y1;
d=2*dx-dy;
ds=2*dx;
dt=2*(dx-dy);
x=x1;
y=y1;
while(y<=y2)
{
y=y+1;
if(d<0)
{
d=d+ds;
}
else
{
x=x+1;
d=d+dt;
}
putpixel(x,y,WHITE);
}
} //end of display()
void run()
{
dx=x2-x1;
dy=y2-y1;
d=-2*dx-dy;
ds=2*dx;
dt=2*(dy+dx);
x=x1;
y=y1;
while(y<=y2)
{
y=y-1;
if(d<0)
{
d=d-ds;
}
else
{
x=x+1;
d=d-dt;
}
putpixel(x,y,WHITE);
}
} //end of run()
getch();
}
//Program to draw a circle using Breshenham
Polynomial/Direct Method
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int h,k,r,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"Enter the coordinate of centre and radius:";
cin>>h>>k>>r;
for(x=0;x<=0.707*r;x++)
{
y=sqrt(r*r-x*x);
putpixel(x+h,y+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
putpixel(y+h,x+k,WHITE);
putpixel(-y+h,x+k,WHITE);
putpixel(y+h,-x+k,WHITE);
putpixel(-y+h,-x+k,WHITE);
putpixel(x+h,y+k,WHITE);
}
getch();
}
//Program to implement circle using Breshenham circle
generating algorithm
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int h,k,r,x,y,d;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"Enter the coordinates of centre and radius:";
cin>>h>>k>>r;
d=3-(2*r);
y=r;
for(x=0;x<=0.707*r;x++)
{
if(d<0)
{
d=d+6+(4*x);
}
else
{
y=y-1;
d=d+10+(4*(x-y));
}
putpixel(x+h,y+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
putpixel(y+h,x+k,WHITE);
putpixel(-y+h,x+k,WHITE);
putpixel(y+h,-x+k,WHITE);
putpixel(-y+h,-x+k,WHITE);
putpixel(x+h,y+k,WHITE);
}
getch();
}
/*Program to draw a circle using Breshenham mid point
circle generating algorithm*/
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int h,k,r,x,y,d;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"Enter the coordinates of centre:";
cin>>h>>r;
cout<<"Enter radius of circle:";
cin>>r;
d=1-r;
y=r;
for(x=0;x<=0.707*r;x++)
{
if(d<0)
{
d=d+3+(2*x);
}
else
{
y=y-1;
d=d+5+(2*x)-(2*y);
}
putpixel(x+h,y+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
putpixel(y+h,x+k,WHITE);
putpixel(-y+h,x+k,WHITE);
putpixel(y+h,-x+k,WHITE);
putpixel(-y+h,-x+k,WHITE);
putpixel(x+h,y+k,WHITE);
}
getch();
}
#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
float h,k,a,b,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"enter coordinates:";
cin>>a>>b;
cout<<"enter center:";
cin>>h>>k;
for(x=0;x<=a;x++)
{
y=(b*sqrt(a*a-x*x))/a;
putpixel(x+h,y+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
}
getch();
}
//Program to implement ellipse using Breshenham midpoint
algorithm
#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
float h,k,a,b,d,x,y,x1,y1,d1;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cout<<"enter coordinates:";
cin>>a>>b;
cout<<"enter center:";
cin>>h>>k;
d=(b*b)+(a*a)/4-(a*a*b);
x=0;
y=b;
while((b*b*x)==(a*a*y))
{
if(d<0)
{
x=x+1;
d=d+(2*b*b*x)+(b*b);
}
else
{
x=x+1;
y=y-1;
d=d+(2*b*b*x)-(2*a*a*y)+(b*b);
}
putpixel(x+h,y+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
}
x1=x;
y1=y;
d1=d;
while(x1<=a)
{
if(d<0)
{
x1=x1+1;
y1=y1-1;
d1=d1+(2*b*b*x1)-(2*a*a*y1)+(a*a);
}
else
{
y1=y1-1;
d1=d1+(a*a)-(2*a*a*y1);
}
putpixel(x+h,y+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
}
getch();
}
//Program to translate a Rectangle
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
line(50,50,200,50);
line(200,50,200,100);
line(200,100,50,100);
line(50,100,50,50);
line(50+50,50+100,200+50,50+100);
line(200+50,50+100,200+50,100+100);
line(200+50,100+100,50+50,100+100);
line(50+50,100+100,50+50,50+100);
getch();
}
//Program to rotate a triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int q;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
line(50,50,20,100);
line(20,100,70,100);
line(70,100,50,50);
line(floor(50*0.86-50*0.5),floor(50*0.5+50*0.86),floor(20*0.86-
100*0.5),floor(20*0.5+100*0.86));
line(floor(20*0.86-100*0.5),floor(20*0.86+100*0.5),floor(70*0.86-
100*0.5),floor(70*0.86+100*0.5));
line(floor(70*0.86-100*0.5),floor(70*0.5+100*0.86),floor(50*0.86-
50*0.5),floor(50*0.5+50*0.86));
getch();
}
//Program to Scale a Rectangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int sx=2,sy=3;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
line(50,100,90,100);
line(50,100,50,150);
line(50,150,90,150);
line(90,100,90,150);
line(50*sx,100*sy,90*sx,100*sy);
line(50*sx,100*sy,50*sx,150*sy);
line(50*sx,150*sy,90*sx,150*sy);
line(90*sx,100*sy,90*sx,150*sy);
getch();
}