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

Hi Hello, What I am going to do in this article I will see in detail about C++ Graphics Program to Implement Line and Point Translation of Objects and Shapes in Screen. This article is a favorite of everyone and many people are suffering without knowing what the reason is. This article will be very useful for them.

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

// 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:tcbgi");
	
	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:tcbgi");
	
	// 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;
}

Read Also: C++ Minecraft Game Clone Program Using OpenGL Graphics Library

Final Words

By knowing clearly about C++ Graphics Program to Implement Line and Point Translation of Objects and Shapes in Screen through this article. If you have any doubts please leave a comment via the comment box. Thank You.

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

Share on:

Leave a Comment