Solved

MAKING TRANSFORMATIONS USING BORLAND C++

Posted on 2004-03-25
7
434 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

820 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