C++ Graphics Program to Implement Line and Point Translation of Objects and Shapes in Screen

Shall I start the article to teach the topic “C++ Graphics Program to Implement Line and Point Translation of Objects and Shapes in Screen” to you? Come on! Let’s go for an article to see the possibilities to learn about it.

C++ Graphics Program to Implement Line and Point Translation of Objects and Shapes in Screen

C++ Graphics Program to Implement Line and Point Translation of Objects and Shapes in Screen

Program Code

// 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;
}
// 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;
}

Final Words

I hope this article helps you to know about “C++ Graphics Program to Implement Line and Point Translation of Objects and Shapes in Screen”. If you face any issues please let me know via the comment section. Share this article with other C/C++ developers via social networks. Thank You!

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment