Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# MAKING TRANSFORMATIONS USING BORLAND C++

Posted on 2004-03-25
Medium Priority
437 Views
how can I make some transformations "like translation,
scaling, rotation, ...." using the Borland C++ 3.1
Graphics Package?

thanks
0
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3

LVL 9

Accepted Solution

ankuratvb earned 100 total points
ID: 10683394
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

LVL 2

Assisted Solution

Avik Dasgupta earned 100 total points
ID: 10683408
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
Try them.

Avik.
0

LVL 9

Expert Comment

ID: 10685946
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 Comment

ID: 10709971
HI,

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

0

LVL 9

Expert Comment

ID: 10709988
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month7 days, 15 hours left to enroll