Archive

Archive for December 6, 2009

C++ Graphics – 06 – Translation of LINE, TRIANGLE and RECTANGLE

December 6, 2009 Leave a comment

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(GREEN);
	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 Transform(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 TRANSFORAMTION ");
		printf("\n ~~~~~~~~~~~~~~\n");
		printf("\n 1 -> LINE.");
		printf("\n 2 -> TRIANGLE.");
		printf("\n 3 -> RECTANGLE");
		printf("\n 4 -> Exit.");
		printf("\n Enter your choice: ");
		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:
				gotoxy(10, 10);
				printf(" Dev. By: Manoj Pandey.");
				gotoxy(15, 15);
				printf("MCA 3d Sem.");
				getch();
				closegraph();
				exit(1);
			} // end of switch.
		GetCords(mat, n);	// Input of Coordinates.
		Transform(mat, n);	// Translating and Displaying.
		getch();
		} // end of while.
	} // end of main.