2D Translation Triangle Program Using C Programming
🎯 Introduction
Welcome to the "2D Translation Triangle Program Using C Programming" tutorial. In this tutorial, we will explore a C program that demonstrates the translation of a triangle in a 2D graphics environment. The program utilizes the Turbo C graphics library to achieve the graphical representation. We will provide a comprehensive explanation of the concepts involved and guide you through the code step by step.
🎯 Concept
Translation is a fundamental geometric transformation that involves moving an object from one position to another in a coordinate plane. In 2D graphics, translation is achieved by shifting the object's coordinates by a certain amount along the x and y axes.
🎯 2D Translation Triangle Example Program
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
int x1, y1, x2, y2, x3, y3;
void draw();
void tri();
int main()
{
int gd = DETECT, gm;
int c;
// Initialize the graphics system
initgraph(&gd, &gm, "d:\\tc\\bgi ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d", &x1, &y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d", &x2, &y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d", &x3, &y3);
// Clear the graphics screen and draw the original triangle
cleardevice();
draw();
printf("Enter the Translation coordinates: ");
int x, y;
scanf("%d%d", &x, &y);
// Perform the translation and display the transformed triangle
cleardevice();
tri();
getch();
closegraph();
return 0;
}
void draw()
{
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}
void tri()
{
int a1, a2, a3, b1, b2, b3;
a1 = x1 + x;
b1 = y1 + y;
a2 = x2 + x;
b2 = y2 + y;
a3 = x3 + x;
b3 = y3 + y;
line(a1, b1, a2, b2);
line(a2, b2, a3, b3);
line(a3, b3, a1, b1);
}
🎯 2D Translation Triangle Program Explanation
The draw() Function:
The draw() function is responsible for drawing the original triangle based on the user's input. It uses the line() function from the Turbo C graphics library to draw the three sides of the triangle.
The tri() Function:
The tri() function implements the translation of the triangle. It prompts the user to enter the translation coordinates (x and y), and then it calculates the new coordinates of each vertex after applying the translation. The line() function is used again to draw the transformed triangle.
The main() Function:
The main() function is the entry point of the program. It initializes the graphics system using the initgraph() function, which sets up the graphical environment. The user is prompted to input the coordinates of the three vertices of the triangle. After drawing the original triangle, the program waits for the user to press any key before applying the translation. Once the translation is done, the transformed triangle is displayed.
🎯 Summary
This C program demonstrates 2D triangle translation using the Turbo C graphics library. The user is asked to input the coordinates of the three vertices of the triangle, and the program graphically displays the original triangle. Afterward, the user is prompted to provide translation coordinates, and the transformed triangle is shown on the screen.
🎯 Key Points
Translation is a geometric transformation that moves an object in a 2D plane.
The Turbo C graphics library provides functions like initgraph() and line() to draw graphics.
The draw() function is used to draw the original triangle, and the tri() function applies the translation and displays the transformed triangle.
The user is required to input the triangle's coordinates and the translation values.
Computer Graphics Programs
2D Computer Graphics Programs
3D Computer Graphics Programs