0% found this document useful (0 votes)
37 views13 pages

CG Practicals

Uploaded by

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

CG Practicals

Uploaded by

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

1.

DDA
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
class DDA
{
private:float x1,y1,x2,y2;
public:void getdata();
void drawline();
};
void DDA::getdata()
{
clrscr();
cout<<"Enter the starting coordinates :";
cin>>x1>>y1;
cout<<"Enter the ending coordinates :";
cin>>x2>>y2;
drawline();
clrscr();
}
void DDA::drawline()
{
int gd=DETECT,gm,errorcode;
initgraph(&gd,&gm,"c:\tc\bgi");
float dx,dy,m,t1,t2;
cout<<"\t\t(x1,y1)=("<<x1<<","<<y1<<")";
cout<<"\t\t(x2,y2)=("<<x2<<","<<y2<<")";
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
if(dx>dy)
{
t1=y1;
t2=y2;
}
else
{
t1=x1;
t2=x2;
}
putpixel(x1,y1,15);
do
{
x1=x1+m;
y1=y1+m;
putpixel(x1,y1,15);
t1++;
}while(t1<t2);
}
void main()
{
class DDA d;
d.getdata() ;
getch();
closegraph();
}

OUTPUT :
2. BRESENHAM
#include<graphics.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
class Bresenham
{
private:int maxx,maxy,i,x1,x2,y1,y2,len,dy,dx;
char msg[5];
public: Bresenham();
void getdata();
void plotaxis();
int sign(int);
void calculate();
};
Bresenham::Bresenham()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:/tc/bgi");
setcolor(RED);
maxx=getmaxx();
maxy=getmaxy();
}
void Bresenham::getdata()
{
cout<<"Enter 1st coordinate :";
cin>>x1>>y1;
cout<<"Enter 2nd coordinate :";
cin>>x2>>y2;
}
void Bresenham::plotaxis()
{
line(0,maxy/2,maxx,maxy/2);
line(maxx/2,0,maxx/2,maxy);
for(i=0;i<=maxx;i+=40)
{
sprintf(msg,"%d",((((maxx/2)-i)+1)*(-1)));
outtextxy(i,(maxy/2)+4,msg);
line(i,(maxy/2)-2,i,(maxy/2)+2);
}
for(i=0;i<=maxy;i+=40)
{
if(i==(maxy/2)+1)
{
continue;
}
sprintf(msg,"%d",((((maxy/2)-i)+1)));
outtextxy((maxx/2)+4,i,msg);
line((maxx/2)-2,i,(maxx/2)+2,i);
}
}
int Bresenham::sign(int a)
{
if(a<0)
{
return -1;
}
else if(a>0)
{
return 1;
}
else if(a==0)
{
return 0;
}
}
void Bresenham::calculate()
{
x1=(maxx/2)+x1;
x2=(maxx/2)+x2;
y1=(maxy/2)-y1;
y2=(maxy/2)-y2;
if(abs(x2-x1)>=abs(y2-y1))
{
len=abs(x2-x1);
}
else
{
len=abs(y2-y1);
}
if(y2-y1<0)
{
dy=-1;
}
else if(y2-y1>0)
{
dy=1;
}
else if(y2-y1==0)
{
dy=0;
}
if(x2-x1<0)
{
dx=-1;
}
else if(x2-x1>0)
{
dx=1;
}
else if(x2-x1==0)
{
dx=0;
}
int x=x1;
int y=y1;
i=0;
x=x+(0.5*sign(dx));
y=y+(0.5*sign(dy));
while(i<len)
{
putpixel(x,y,GREEN);
x=x+dx;
y=y+dy;
i++;
}
}
void main()
{
class Bresenham b;
b.plotaxis();
b.getdata();
b.calculate();
getch();
closegraph();
}

OUTPUT :
3. SCALE
#include<graphics.h>
#include<math.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
class Scale
{
private:
int sx,sy;
public:
void figure();
void getscalefactor();
void scale();
};
void Scale::figure()
{
setcolor(RED);
rectangle(10,100,60,150);
outtextxy(10,90,"Before");
}
void Scale::getscalefactor()
{
cout<<"\nEnter scale factor for x: ";
cin>>sx;
cout<<"\nEnter scale factor for y: ";
cin>>sy;
}
void Scale::scale()
{
setcolor(BROWN);
if(sx==0)
sx=1;
if(sy==0)
sy=1;
outtextxy(10*sx,(100 * sy-10),"After");
rectangle(10*sx,100*sy,60*sx,150*sy);
}
void main()
{
Scale s;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
s.figure();
s.getscalefactor();
s.scale();
getch();
closegraph();
}

OUTPUT:
4. TRANSLATE
#include<graphics.h>
#include<math.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
class Translate
{
private:
int x1,y1,tx,ty;
public:
void figure();
void gettranslationfactor();
void translate();
};
void Translate::figure()
{
setcolor(RED);
rectangle(10,100,60,150);
outtextxy(10,90,"Before");
}
void Translate::gettranslationfactor()
{
cout<<"\nEnter translate factor for x: ";
cin>>tx;
cout<<"\nEnter Translate factor for y: ";
cin>>ty;
}
void Translate::translate()
{
setcolor(BROWN);
rectangle(10+tx,100+ty,60+tx,150+ty);
outtextxy(10+tx,90+ty,"after");
}
void main()
{
Translate t;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
t.figure();
t.gettranslationfactor();
t.translate();
getch();
closegraph();
}

OUTPUT:
5. ROTATION
#include<graphics.h>
#include<math.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
class ArbitayRotation
{
private:
int a,x1,x2,y1,y2,d;
public:
void getline();
void getrotationfactor();
void rotate();
};
void ArbitayRotation::getline()
{
setcolor(RED);
cout<<"Enter Starting point of line :";
cin>>x1>>y1;
cout<<"Enter ending point of line :";
cin>>x2>>y2;
line(x1,y1,x2,y2);
outtextxy(x2,y2,"Before");
}
void ArbitayRotation::getrotationfactor()
{
cout<<"\nEnter the angle with x axis :";
cin>>a;
}
void ArbitayRotation::rotate()
{
setcolor(BROWN);
float x,y;
x=cos((3.142/180)*a);
y=sin((3.142/180)*a);
float l;
l=sqrt(pow(100,2)+pow(0,2));
x=ceil(x*l);
y=ceil(y*l);
outtextxy(x1+x,y1-y-10,"After");
line(x1,y1,x1+x,y1-y);
}
void main()
{
ArbitayRotation r;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
r.getline();
r.getrotationfactor();
r.rotate();
getch();
closegraph();
}

OUTPUT:
6. DRAW HOUSE
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class House
{
private: int l1_x1,l1_y1,l1_x2,l1_y2;
int l2_x1,l2_y1,l2_x2,l2_y2;
int l3_x1,l3_y1,l3_x2,l3_y2;
int l4_x1,l4_y1,l4_x2,l4_y2;
int l5_x1,l5_y1,l5_x2,l5_y2;
int l6_x1,l6_y1,l6_x2,l6_y2;
int l7_x1,l7_y1,l7_x2,l7_y2;
int l8_x1,l8_y1,l8_x2,l8_y2;
int l9_x1,l9_y1,l9_x2,l9_y2;
public: House();
void drawhouse();
};
House::House()
{
l1_x1=100,l1_y1=100,l1_x2=50,l1_y2=150;
l2_x1=50,l2_y1=150,l2_x2=50,l2_y2=250;
l3_x1=50,l3_y1=250,l3_x2=150,l3_y2=250;
l4_x1=150,l4_y1=250,l4_x2=150,l4_y2=150;
l5_x1=150,l5_y1=150,l5_x2=100,l5_y2=100;
l6_x1=50,l6_y1=150,l6_x2=150,l6_y2=150;
l7_x1=75,l7_y1=250,l7_x2=75,l7_y2=200;
l8_x1=75,l8_y1=200,l8_x2=125,l8_y2=200;
l9_x1=125,l9_y1=200,l9_x2=125,l9_y2=250;
}
void House::drawhouse()
{
line(l1_x1,l1_y1,l1_x2,l1_y2);
line(l2_x1,l2_y1,l2_x2,l2_y2);
line(l3_x1,l3_y1,l3_x2,l3_y2);
line(l4_x1,l4_y1,l4_x2,l4_y2);
line(l5_x1,l5_y1,l5_x2,l5_y2);
line(l6_x1,l6_y1,l6_x2,l6_y2);
line(l7_x1,l7_y1,l7_x2,l7_y2);
line(l8_x1,l8_y1,l8_x2,l8_y2);
line(l9_x1,l9_y1,l9_x2,l9_y2);
}
void main()
{
House d;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c://tc//bgi");
d.drawhouse();
getch();
closegraph();
}

OUTPUT:

You might also like