Implementing 2-D Transformation in C++

Here is C++ code to perform 2-D transformation.

#include<iostream.h>
#include<math.h>
#include<graphics.h>
#include<conio.h>
#include<stdio.h>

void main() {
  int x1, y1, x2, y2, i, gd = DETECT, gm;
  char d;
  void translation(int, int, int, int);
  void scaling(int, int, int, int);
  void rotation(int, int, int, int);
  clrscr();
  initgraph( & gd, & gm, "c:\\tc\\bgi");
  cout << "enter the end points of the line:\n";
  cin >> x1 >> y1 >> x2 >> y2;
  line(x1, y1, x2, y2);
  do {
    cout << "enter the type of transformation:\n";
    cout << "1--->translation \n2--->scaling\n3--->rotation\n";
    cin >> i;
    switch (i) {
    case 1:
      translation(x1, y1, x2, y2);
      break;
    case 2:
      scaling(x1, y1, x2, y2);
      break;
    case 3:
      rotation(x1, y1, x2, y2);
      break;
    default:
      break;
    }
    cout << "do u wanna continue??? 'Y' or 'N'\n";
    cin >> d;
  }
  while (d == 'y' || d == 'Y');
  getch();
}
void translation(int x1, int y1, int x2, int y2) {
  int tx, ty;
  cout << "\nenter the translation factors:\n";
  cin >> tx >> ty;
  x1 += tx;
  y1 += ty;
  x2 += tx;
  y2 += ty;
  cout << "the new translated line is:\n";
  line(x1, y1, x2, y2);
}
void scaling(int x1, int y1, int x2, int y2) {
  int sx, sy;
  cout << "\nenter the scaling factors:";
  cin >> sx >> sy;
  x1 *= sx;
  y1 *= sy;
  x2 *= sx;
  y2 *= sy;
  cout << "the new scaling line is:\n";
  line(x1, y1, x2, y2);
}
void rotation(int x1, int y1, int x2, int y2) {
  int a1, a2, b1, b2, th;
  cout << "enter the value for theta:\n";
  cin >> th;
  a2 = (x2 * cos(th)) - (y2 * sin(th));
  b2 = (y2 * cos(th)) + (x2 * sin(th));
  cout << "the new rotated line is:\n";
  line(x1, y1, a2, b2);
}

Output:

Output 1
Output 1
Output 2
Output 2
Output 3
Output 3

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.