2D Transformation
2D Transformation
1615128 BATCH: B3
1. 2D TRANSFORMATION
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void put(float x[],float y[],int n)
{
setcolor(RED);
for(int i=0;i<n;i++)
{
line(x[i%n],y[i%n],x[(i+1)%n],y[(i+1)%n]);
}
}
void trans(float x[],float y[],int n,float tx,float ty)
{
setcolor(GREEN);
for(int i=0;i<n;i++)
{
x[i]=x[i]+tx;
y[i]=y[i]+ty;
}
cout<<"The translated figure is"<<endl;
for(i=0;i<n;i++)
line(x[i%n],y[i%n],x[(i+1)%n],y[(i+1)%n]);
}
void rotat(float x[],float y[],float t,int n)
{
setcolor(WHITE);
float c=cos(t);
float s=sin(t);
for(int i=0;i<n;i++)
{
x[i]=(x[i]*c)-(y[i]*s);
y[i]=(x[i]*s)+(y[i]*c);
}
for(i=0;i<n;i++)
line(x[i%n],y[i%n],x[(i+1)%n],y[(i+1)%n]);
}
void Scal(float x[],float y[],int n,float sx,float sy)
{
setcolor(GREEN);
for(int i=0;i<n;i++)
{
x[i]=x[i]*sx;
y[i]=y[i]*sy;
}
for(i=0;i<n;i++)
line(x[i%n],y[i%n],x[(i+1)%n],y[(i+1)%n]);
}
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
int i=1,n,v;
cout<<"Enter no. of sides"<<endl;
cin>>n;
float x[10],y[10];
for(i=0;i<n;i++)
{
cout<<"Enter the co-ordinates of "<<(i+1)<<"point"<<endl;
cout<<"X=";
cin>>x[i];
cout<<"Y=";
cin>>y[i];
}
put(x,y,n);
do
{
cout<<"2-D TRANSFORMATIONS"<<endl;
cout<<"1.TRANSLATION"<<endl;
cout<<"2.ROTATION"<<endl;
cout<<"3.SCALING"<<endl;
cout<<"Enter your choice"<<endl;
cin>>i;
switch(i)
{
case 1:
cout<<"Enter the translation co-ordinates"<<endl;
cout<<"TX=";
float tx,ty;
cin>>tx;
cout<<"TY=";
cin>>ty;
trans(x,y,n,tx,ty);
break;
case 2:
cout<<"Enter angle in degree"<<endl;
float t;
cin>>t;
t=t*((22.0/7)/180);
rotat(x,y,t,n);
break;
case 3:
cout<<"Enter Scaling Co-ordinates"<<endl;
float sx,sy;
cout<<"SX=";
cin>>sx;
cout<<"SY=";
cin>>sy;
Scal(x,y,n,sx,sy);
break;
}
cout<<"Enter 1 to continue"<<endl;
cin>>v;
}while(v==1);
getch();
closegraph();
2. BEZIER CURVE
# include<iostream.h>
# include<conio.h>
# include<dos.h>
# include<graphics.h>
# include<math.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
double x[4],y[4],px,py;
cout<<"Enter four control points of bezier curve"<<endl;
for(int i=0;i<4;i++)
{
cout<<"X"<<(i+1)<<"=";
cin>>x[i];
cout<<"Y"<<(i+1)<<"=";
cin>>y[i];
}
double t;
for(t=0.0;t<=1.0;t=t+0.001)
{
double j=1.0-t;
px=(pow(j,3)*x[0])+(3*t*pow(j,2)*x[1])+(3*t*t*j*x[2])+(t*t*t*x[3]);
py=(pow(j,3)*y[0])+(3*t*pow(j,2)*y[1])+(3*t*t*j*y[2])+(t*t*t*y[3]);
putpixel(px,py,3);
delay(10);
}
getch();
closegraph();
}
3. BRESENHAM
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
# include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
float x1,y1,x2,y2,x,y,dx,dy,e;
cout<<"Enter the co-ordinates of the line"<<endl;
cout<<"X1=";
cin>>x1;
cout<<"Y1=";
cin>>y1;
cout<<"X2=";
cin>>x2;
cout<<"Y2=";
cin>>y2;
dx=abs(x2-x1);
dy=abs(y2-y1);
e=2*dy-dx;
x=x1;
y=y1;
int i=1;
do
{
putpixel(x,y,3);
while(e>=0)
{
y=y+1;
e=e-2*dx;
}
x=x+1;
e=e+2*dy;
i=i+1;
}while(i<=dx);
getch();
closegraph();
}
3. CIRCLE
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
float r,xc,yc,x,y,p;
cout<<"Enter the radius:";
cin>>r;
cout<<"Enter the center co-ordinates"<<endl;
cout<<"Xc=";
cin>>xc;
cout<<"Yc=";
cin>>yc;
x=0;
y=r;
p=1-r;
do
{
putpixel(x+xc,y+yc,3);
putpixel(y+yc,x+xc,2);
putpixel(-y+yc,x+xc,4);
putpixel(x+xc,-y+yc,5);
putpixel(-x+xc,-y+yc,6);
putpixel(-y+yc,-x+xc,7);
putpixel(y+yc,-x+xc,8);
putpixel(-x+xc,y+yc,9);
if(p<0)
{
x=x+1;
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*x+1-2*y;
}
delay(100);
}while(x<y);
getch();
closegraph();
}
4. ELLIPSE
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<dos.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
float rx,ry,xc,yc,x,y,p,i,j;
cout<<"Enter the radius of ellipse"<<endl;
cout<<"Rx=";
cin>>rx;
cout<<"Ry=";
cin>>ry;
cout<<"Enter center co-ordinates"<<endl;
cout<<"Xc=";
cin>>xc;
cout<<"Yc=";
cin>>yc;
x=0;
y=ry;
p=(ry*ry)-(rx*rx*ry)+(0.25*rx*rx);
do
{
putpixel(x+xc,y+yc,2);
putpixel(-x+xc,-y+yc,3);
putpixel(-x+xc,y+yc,4);
putpixel(x+xc,-y+yc,5);
i=(2*ry*ry*x)+(2*ry*ry);
j=(2*rx*rx*y)-(2*rx*rx);
if(p<0)
{
x=x+1;
p=p+i+(ry*ry);
}
else
{
x=x+1;
y=y-1;
p=p+i+ (ry*ry)-j;
}
delay(100);
}while(i<j);
p=((ry*ry)*(x+0.5)*(x+0.5))+((rx*rx)*(y-1)*(y-1))-(rx*rx*ry*ry);
do
{
putpixel(x+xc,y+yc,2);
putpixel(-x+xc,-y+yc,3);
putpixel(-x+xc,y+yc,4);
putpixel(x+xc,-y+yc,5);
i=(2*ry*ry*x)+(2*ry*ry);
j=(2*rx*rx*y)-(2*rx*rx);
if(p>0)
{
y=y-1;
p=p-j+(rx*rx);
}
else
{
x=x+1;
y=y-1;
p=p+i-j+(rx*rx);
}
delay(100);
}while(y!=0);
getch();
closegraph();
}
5.BRESEN
#include <iostream.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
// Part of Cosmos by OpenGenus Foundation
class BresenhamCircle
{
public:
BresenhamCircle(int radius_) : radius_(radius_)
{
}
void getRadiusCenter();
void drawBresenhamCircle();
void displayBresenhmCircle(int xc_,int yc_, int x, int y);
private:
int radius_;
int xc_;
int yc_;
};
void BresenhamCircle::drawBresenhamCircle() {
clrscr();
int x = 0, y = radius_;
int decesionParameter = 3 - 2 * radius_;
displayBresenhmCircle(xc_, yc_, x, y);
while (y >= x)
{
x++;
if (decesionParameter > 0)
{
y--;
decesionParameter = decesionParameter + 4 * (x - y) + 10;
}
else
decesionParameter = decesionParameter + 4 * x + 6;
displayBresenhmCircle(xc_, yc_, x, y); //displaying all the Eight
Pixels of (x,y)
delay(30);
}
}
void BresenhamCircle::getRadiusCenter() {
cout << "\nEnter Radius of the Circle : ";
cin >> radius_;
cout << "\nEnter X-Coordinate of Center of Circle : ";
cin >> xc_;
cout << "\nEnter Y-Coordinate of Center of Circle : ";
cin >> yc_;
}
void BresenhamCircle::displayBresenhmCircle(int xc_,int yc_, int x, int y)
{
//displaying all 8 coordinates of(x,y) residing in 8-octants
putpixel(xc_+x, yc_+y, WHITE);
putpixel(xc_-x, yc_+y, WHITE);
putpixel(xc_+x, yc_-y, WHITE);
putpixel(xc_-x, yc_-y, WHITE);
putpixel(xc_+y, yc_+x, WHITE);
putpixel(xc_-y, yc_+x, WHITE);
putpixel(xc_+y, yc_-x, WHITE);
putpixel(xc_-y, yc_-x, WHITE);
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
BresenhamCircle b(0);
//accepting the radius and the centre coordinates of the circle
b.getRadiusCenter();
/*
* selecting the nearest pixel and displaying all the corresponding
* points of the nearest pixel point lying in 8-octants.
*
*/
b.drawBresenhamCircle();
delay(200);
closegraph();
return(0);
}
6. 2D FIGURES
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;
getch();
closegraph();
return 0;
}
7. DDA
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
float x1,x2,y1,y2,dx,dy,length,x,y;
cout<<"Enter the co-ordinates of the line"<<endl;
cout<<"X1=";
cin>>x1;
cout<<"Y1=";
cin>>y1;
cout<<"X2=";
cin>>x2;
cout<<"Y2=";
cin>>y2;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
length=dx;
else
length=dy;
dx=(x2-x1)/length;
dy=(y2-y1)/length;
x=x1+0.5;
y=y1+0.5;
for(int i=1;i<=length;i++)
{
putpixel(x,y,3);
x=x+dx;
y=y+dy;
}
getch();
closegraph();
}