Вы находитесь на странице: 1из 8

EXPERIMENT -

LINE DRAWING BY DDA


#include<graphics.h>
#include<dos.h>
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main( )
{ clrscr();
intgdriver=DETECT, gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bg");
int x1,x2,y1,y2,dx,dy,len,xin,yin,i,x,y;
cout<<"Enter (x1,y1) -";
cin>>x1>>y1;
cout<<endl<<"Enter (x2,y2) -";
cin>>x2>>y2;
cout<<endl;
dx=abs(x1-x2);
dy=abs(y1-y2);
if(dx>=dy){
len=dx;
}else{
len=dy;
}
xin=dx/len;
yin=dy/len;
x=x1;
y=y1;
for(i=0;i<len;i++){
putpixel(x,y,7);
x=x+xin;
y=y+yin;
}
getch();
}
OUTPUT:

ISHDEEP SINGH
IT/16/408
EXPERIMENT –
Draw a line using BRESENHAM’S LINE algorithm

#include<graphics.h>
#include<dos.h>
#include<iostream.h>
#include<conio.h>
void main(){
intgdriver=DETECT, gmode;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI ");
cleardevice();
int x1,x2,y1,y2,dx,dy,dx2,dy2,p;
cout<<"Enter points (x1,y1) -";
cin>>x1>>y1;
cout<<endl<<"Enter points (x2,y2) -";
cin>>x2>>y2;
cout<<endl;
dx = abs(x1-x2);
dy = abs(y1-y2);
dx2 = dx*2;
dy2 = dy*2;
p = dy2-dx;
int x=x1,y=y1;
putpixel(x,y,4);
while(x<=x2){
if(p<0){
++x;
putpixel(x,y,7);
p+=dy2;
}else{
++x;
++y;
putpixel(x,y,7);
p+=(dy2-dx2);
}
}
getch();
}

OUTPUT:

ISHDEEP SINGH
IT/16/408
EXPERIMENT -
Program to draw a Circle using Mid Point Algorithm
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void draw(intx,inty,int c){
int a = 200;
int b = 120;
putpixel(x+a,y+b,c);
putpixel(x+a,-y+b,c);
putpixel(-x+a,y+b,c);
putpixel(-x+a,-y+b,c);
putpixel(y+a,x+b,c);
putpixel(-y+a,x+b,c);
putpixel(y+a,-x+b,c);
putpixel(-y+a,-x+b,c);
}
void main(){
intgd = DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
intx,y,r,h;
cout<<"Enter the radius of the circle : ";
cin>>r;
x=0,y=r;
h=1-r;
while(y>x){
if(h<0){
h+=(2*x)+3;
}else{
h+=(2*x)-(2*y)+5;
y--;
}
++x;
draw(x,y,15);
}
getch();
closegraph();

OUTPUT :

ISHDEEP SINGH
IT/16/408
EXPERIMENT –
PROGRAM TO DRAW A CIRCLE USING BRISENHAM"S ALGORITHM
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void draw(intx,inty,int c){
int a = 200;
int b = 120;
putpixel(x+a,y+b,c);
putpixel(x+a,-y+b,c);
putpixel(-x+a,y+b,c);
putpixel(-x+a,-y+b,c);
putpixel(y+a,x+b,c);
putpixel(-y+a,x+b,c);
putpixel(y+a,-x+b,c);
putpixel(-y+a,-x+b,c);
}
void main(){
intgd = DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
intx,y,r,h;
cout<<"Enter the radius of the circle : ";
cin>>r;
x=0,y=r;
h=3-(2*r);
while(y>x){
if(h<0){
h+=(4*x)+6;
}else{
h+=(4*x)-(4*y)+10;
y--;
}
++x;
draw(x,y,15);
}
getch();
closegraph();
}
OUTPUT

ISHDEEP SINGH
IT/16/408
EXPERIMENT –
Program to show 2D Transformations
#include&lt;iostream.h&gt;
#include&lt;math.h&gt;
#include&lt;graphics.h&gt;
#include&lt;conio.h&gt;
void translation(int x1,int y1,int x2,int y2,int color){
int dx=0,dy=0;
cout&lt;&lt;&quot;Enter dx and dy\n&quot;;
cin&gt;&gt;dx&gt;&gt;dy;
setcolor(color);
outtextxy(x1+10,y1-10,&quot;Old&quot;);
rectangle(x1,y1,x2,y2);
setcolor(WHITE);
outtextxy(x1+dx+10,y1-10+dy,&quot;Translated&quot;);
rectangle(x1+dx,y1+dy,x2+dx,y2+dy);
}
void rotate(int x1,int y1,int x2,int y2,int color){
double angle;
cout&lt;&lt;&quot;Enter angle of rotation\n&quot;;
cin&gt;&gt;angle;
angle=(angle*3.14)/180;
setcolor(color);
outtextxy(x1+10,y1-10,&quot;Old&quot;);
rectangle(x1,y1,x2,y2);
setcolor(WHITE);
int xx1 = (x1*cos(angle))-(y1*sin(angle));
int xx2 = (x1*cos(angle))-(y2*sin(angle));
int xx3 = (x2*cos(angle))-(y2*sin(angle));
int xx4 = (x2*cos(angle))-(y1*sin(angle));
int yy1 = (x1*sin(angle))+(y1*cos(angle));
int yy2 = (x1*sin(angle))+(y2*cos(angle));
int yy3 = (x2*sin(angle))+(y2*cos(angle));
int yy4 = (x2*sin(angle))+(y1*cos(angle));
outtextxy(xx1+10,yy1-10,&quot;Rotated&quot;);
line(xx1,yy1,xx2,yy2);
line(xx2,yy2,xx3,yy3);
line(xx3,yy3,xx4,yy4);
line(xx4,yy4,xx1,yy1);
}
void scale(int x1,int y1,int x2,int y2,int color){
floatsx=1,sy=1;
cout&lt;&lt;&quot;Entersx and sy \n&quot;;
cin&gt;&gt;sx&gt;&gt;sy;
setcolor(color);
outtextxy(x1+10,y1-10,&quot;Old&quot;);
rectangle(x1,y1,x2,y2);
setcolor(WHITE);
ISHDEEP SINGH
IT/16/408
outtextxy(x1*sx+10,y1*sy-10,&quot;Scaling&quot;);
rectangle(x1*sx,y1*sy,x2*sx,y2*sy);

}
void main(){
intgd = DETECT,gm;
initgraph(&amp;gd,&amp;gm,&quot;c:\\turboc3\\bgi&quot;);
int x1=150,y1=150,x2=350,y2=250;
intch;
rectangle(x1,y1,x2,y2);
cout&lt;&lt;&quot;Enter your choice&quot;;
cout&lt;&lt;&quot;\n 1.Translation 2.Scale 3. Rotate\n&quot;;
cin&gt;&gt;ch;
switch(ch){
case 1: translation(x1,y1,x2,y2,RED);
break;
case 2: scale(x1,y1,x2,y2,RED);
break;
case 3: rotate(x1,y1,x2,y2,RED);
break;
default: cout&lt;&lt;&quot;Wrongchoice&quot;;
}
getch();
closegraph();
}

OUTPUT:
1)TRANSLATION

ISHDEEP SINGH
IT/16/408
2) SCALING

3)ROTATE

ISHDEEP SINGH
IT/16/408
EXPERIMENT –
Program to implement Bezier Curves
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
voidbezier(int x[4],int y[4]){
intgd = DETECT,gm;
inti;
double t;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI ");
for(t=0;t<1;t+=0.0005){
double 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];
double 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,WHITE);
}
for(i=0;i<4;i++){
putpixel(x[i],y[i],YELLOW);
}
getch();
closegraph();
}
void main(){
int x[4],y[4];
puts("Enter x&y co-ordinates");
for(inti=0;i<4;i++){
scanf("%d %d",&x[i],&y[i]);
}
bezier(x,y);
}

ISHDEEP SINGH
IT/16/408

Вам также может понравиться