C++ Graphics Program to Implement Line & Point Translation of Objects and Shapes in Screen Full Project For Beginners

  • Post author:
  • Post category:c++
  • Post comments:0 Comments

 

 

main.cpp for point translation

 

 

// C++ program for translation
// of a single coordinate
#include<bits/stdc++.h>
#include<graphics.h>

using namespace std;

// function to translate point
void translatePoint ( int P[], int T[])
{
	/* init graph and putpixel are used for
	representing coordinates through graphical
	functions
	*/
	int gd = DETECT, gm, errorcode;
	initgraph (&gd, &gm, "c:\\tc\\bgi");
	
	cout<<"Original Coordinates :"<<P[0]<<","<<P[1];

	putpixel (P[0], P[1], 1);

	// calculating translated coordinates
	P[0] = P[0] + T[0];
	P[1] = P[1] + T[1];

	cout<<"\nTranslated Coordinates :"<< P[0]<<","<< P[1];
	
	// Draw new coordinatses
	putpixel (P[0], P[1], 3);
	closegraph();
}

// driver program
int main()
{
	int P[2] = {5, 8}; // coordinates of point
	int T[] = {2, 1}; // translation factor
	translatePoint (P, T);
	return 0;
}

 

 

 

main.cpp for line translation

 

 

// cpp program for translation
// of a single line
#include<bits/stdc++.h>
#include<graphics.h>

using namespace std;

// function to translate line
void translateLine ( int P[][2], int T[])
{
	/* init graph and line() are used for
	representing line through graphical
	functions
	*/
	int gd = DETECT, gm, errorcode;
	initgraph (&gd, &gm, "c:\\tc\\bgi");
	
	// drawing original line using graphics functions
	setcolor (2);
	line(P[0][0], P[0][1], P[1][0], P[1][1]);

	// calculating translated coordinates
	P[0][0] = P[0][0] + T[0];
	P[0][1] = P[0][1] + T[1];
	P[1][0] = P[1][0] + T[0];
	P[1][1] = P[1][1] + T[1];

	// drawing translated line using graphics functions
	setcolor(3);
	line(P[0][0], P[0][1], P[1][0], P[1][1]);
	closegraph();
}

// driver program
int main()
{
	int P[2][2] = {5, 8, 12, 18}; // coordinates of point
	int T[] = {2, 1}; // translation factor
	translateLine (P, T);
	return 0;
}

Leave a Reply