Solved

MAKING TRANSFORMATIONS USING BORLAND C++

Posted on 2004-03-25
7
429 Views
Last Modified: 2008-02-20
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
Comment
Question by:ARSSES
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
ankuratvb earned 25 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

by:Avik77
Avik77 earned 25 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
 http://alumni.imsa.edu/~stendahl/comp/links.html
Try them.

Avik.
0
 
LVL 9

Expert Comment

by:ankuratvb
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

by:ARSSES
ID: 10709971
HI,

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

ARSSES
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10709988
Glad to be of help.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now