• Status: Solved
• Priority: Medium
• Security: Public
• Views: 444
• Last Modified:

# MAKING TRANSFORMATIONS USING BORLAND C++

can anyone help me, please
how can I make some transformations "like translation,
scaling, rotation, ...." using the Borland C++ 3.1
Graphics Package?

thanks
ARSSES.
0
ARSSES
Asked:
• 3
2 Solutions

Commented:
Hi,

This code performs all 2D tranformations and this works till four vertex figures.
Enter the co-ordinates in clockwise order.

//2D Transformations
#include<math.h>
#include<iostream.h>
#include <graphics.h>
#include <conio.h>
#include<dos.h>

float r2d=M_PI/180.0f;
float co[4][3];
float tr[3][3];
float res[4][3]={0};
int r=4,c=3,n;
void draw(int);
void disp();
void rotate(int,int,float);
void translate(int,int);
void scale(int,int);
void matmul();
int main()
{
int gdriver = DETECT, gmode;
int i,j;
cout<<"Enter the no. of pts:";
cin>>n;
cout<<"Enter the Pairs of co-ordinates:\n";
for(i=0;i<n;i++)
{
cin>>co[i][0]>>co[i][1];
co[i][2]=1;
}
initgraph(&gdriver, &gmode, "\\tc\\bgi");
cleardevice();
draw(1);
getch();
//scale(4,4);
//translate(100,100);
//draw(15);
//getch();
//scale(2,2);

rotate(0,0,45.0f);
disp();
draw(15);
getch();
closegraph();
return 0;
}
void disp()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
cout<<co[i][j]<<" ";
}
cout<<"\n";
}
}
void draw(int col)
{
int i,j;
setcolor(col);
/*
for(i=0;i<n;i++)
{
circle(co[i][0],co[i][1],5);
}
*/
for(i=0;i<n;i++)
{
if(i==n-1) j=0; else j=i+1;
line(co[i][0],co[i][1],co[j][0],co[j][1]);
}
}
void translate(int tx,int ty)
{
int i;
for(i=0;i<3;i++)
{tr[i][i]=1;}
tr[0][1]=0;tr[0][2]=0;
tr[1][0]=0;tr[1][2]=0;
tr[2][0]=tx;tr[2][1]=ty;
matmul();
}

void scale(int sx,int sy)
{
tr[0][0]=sx;tr[1][1]=sy;tr[2][2]=1;
tr[0][1]=0;tr[0][2]=0;
tr[1][0]=0;tr[1][2]=0;
tr[2][0]=0;tr[2][1]=0;
matmul();
}

void rotate(int x,int y,float th)
{
//translate(-x,-y);
float ang=th*r2d;
tr[0][0]=cos(ang);tr[1][1]=cos(ang);tr[2][2]=1;
tr[0][1]=sin(ang);tr[0][2]=0;
tr[1][0]=-sin(ang);tr[1][2]=0;
tr[2][0]=0;tr[2][1]=0;
matmul();
//translate(x,y);
}
void matmul()
{
int i,j,k;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
for(k=0;k<c;k++)
{
res[i][j]=res[i][j]+(co[i][k]*tr[k][j]);
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
co[i][j]=res[i][j];
}
}
}
0

Commented:
u must be following some standard graphics algorithms for these. Most of them generally represent the co-ordinates of an arbitrary figure as a matrix and apply different matrix transformations to achieve different configurations of the figures.U can also find some important stuff here
http://alumni.imsa.edu/~stendahl/comp/links.html
Try them.

Avik.
0

Commented:
Hi,

All these transformations i.e. translation,scaling and rotation have standard matrices that
are used.
Just do a Google for :2D transformations "computer graphics"

U'll get plenty of links and there are a number of standard matrices that are used by dif.
authors.

Some represent the matrices as row matrices,some as column matrices.

In my program ,i have used the fol. matrix format
x' and y' are the co-ordinates after tranformations
x and y are the co-ordinates before tranformations.

Matrices of 3 columns(why the extra 1?u could have asked) have been used for 2D to make the co-ordinates homogeneous i.e. all the operations can be represented as matrix multiplications

[x' y' 1]=[x y 1][1 0 0
0 1 0
tx ty 1]

For translation where tx and ty are the translation displacements respectively.

[x' y' 1]=[x y 1][sx  0 0
0  sy 0
0   0  1]

For scaling where sx and sy are the scale ratios in x and y respectively.

[x' y' 1]=[x y 1][cos a  sin a  0
-sin a cos a  0
0        0      1]

For rotation where a is the angle in degrees and this is rotation anti-clockwise.
For clockwise,replace a by -a

So,if u have a number of points say 3 points,
store them in matrix as:
[x1 y1 1
x2 y2 1
x3 y3 1]
and multiply this matrix with the appropriate transformation matrix to get the new co-ordinates.

HTH
0

Author Commented:
HI,

I WNTA TO SAY THANKS FOR ANKURATUB & AVIK77
FOR HELPING ME "MANY THANKS".

ARSSES
0

Commented:
Glad to be of help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 3
Tackle projects and never again get stuck behind a technical roadblock.