### Archive

Posts Tagged ‘INVERSE TRANSLATION of RECTANGLE’

## C++ Graphics – 07 – INVERSE TRANSLATION of LINE, TRIANGLE and RECTANGLE

INVERSE TRANSLATION of LINE, TRIANGLE and RECTANGLE.

```#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>

const int MAX = 5;

void GetCords(int mat[][MAX], int n){
int i, j;
for(i=0; i<n; i++){
printf("\n Enter Coordinate no %d:- \n", i+1);
for(j=0; j<2; j++){
scanf("%d", &mat[j][i]);
}
}

for(i=0; i<n; i++){
mat[0][i] = 320 + mat[0][i];
mat[1][i] = 240 - mat[1][i];
}

setcolor(WHITE);
line(0, 240, 640, 240);
outtextxy(630, 245, "x");
line(320, 0, 320, 480);
outtextxy(310, 0, "y");
outtextxy(280, 245, "(0,0)");

setcolor(YELLOW);
moveto(mat[0][0], mat[1][0]);
for(i=0; i<n; i++){
lineto(mat[0][i], mat[1][i]);
moveto(mat[0][i], mat[1][i]);
}
lineto(mat[0][0], mat[1][0]);

}

void invTransform(int mat[][MAX], int n){
int dx, dy, i;
printf("\n Enter Cordinates (dx, dy):- \n");
scanf("%d %d", &dx, &dy);

for(i=0; i<n; i++){
mat[0][i] -= dx;
mat[1][i] += dy;
}

setcolor(LIGHTRED);
moveto(mat[0][0], mat[1][0]);
for(i=0; i<n; i++){
lineto(mat[0][i], mat[1][i]);
moveto(mat[0][i], mat[1][i]);
}
lineto(mat[0][0], mat[1][0]);

}

void main(){
int gd=DETECT, gm;
initgraph(&gd, &gm,"\\tc");
int mat[MAX][MAX], n;
int ch;
while(1){
clrscr();
cleardevice();
printf("\n INVERSE TRANSLATION ");
printf("\n ~~~~~~~~~~~~~~\n");
printf("\n 1 -> LINE.");
printf("\n 2 -> TRIANGLE.");
printf("\n 3 -> RECTANGLE");
printf("\n 4 -> Exit.");
scanf("%d", &ch);
clrscr();
cleardevice();
switch(ch){
case 1:		// LINE.
n=2;
break;
case 2:		// TRIANGLE.
n=3;
break;
case 3:		// RECTANGLE.
n=4;
break;
default:
closegraph();
exit(1);
} // end of switch.
GetCords(mat, n);	// Input Cordinates
invTransform(mat, n);	// Inverse Translating Figures.
getch();
} // end of while.
} // end of main.
```