0% found this document useful (0 votes)
16 views5 pages

C Program for 2D Triangle Scaling and Rotation

Uploaded by

Akshat Mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

C Program for 2D Triangle Scaling and Rotation

Uploaded by

Akshat Mishra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Program for 2D Scaling:

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <process.h>
#include <math.h>

int x1, y1, x2, y2, x3, y3;

void draw();
void scale();

void main()
{
int gd = DETECT, gm;
int c;
initgraph(&gd, &gm, " ");

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);

draw();
scale();
}

void draw()
{
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}

void scale()
{
int x, y;
int mx, my;

printf("Enter the scaling factors (x y): ");


scanf("%d%d", &x, &y);

mx = (x1 + x2 + x3) / 3;
my = (y1 + y2 + y3) / 3;

cleardevice();

int a1, a2, a3, b1, b2, b3;


a1 = mx + (x1 - mx) * x;
b1 = my + (y1 - my) * y;
a2 = mx + (x2 - mx) * x;
b2 = my + (y2 - my) * y;
a3 = mx + (x3 - mx) * x;
b3 = my + (y3 - my) * y;

line(a1, b1, a2, b2);


line(a2, b2, a3, b3);
line(a3, b3, a1, b1);

draw();
getch();
}

Detailed Explanation of the Code:


▪ The program begins by including necessary header files such as stdio.h, conio.h, graphics.h,
process.h, and math.h.
▪ The global variables x1, y1, x2, y2, x3, and y3 represent the coordinates of the three vertices of
the triangle, while mx and my will store the coordinates of the triangle's centroid.
▪ The draw() function is used to draw the original triangle on the screen. It connects the three
vertices with lines.
▪ The scale() function is responsible for applying the scaling transformation to the triangle.
▪ Inside the main() function, the initgraph() function is used to initialize the graphics mode.
▪ The user is prompted to input the coordinates of the three vertices of the triangle.
▪ The draw() function is called to display the original triangle.
▪ The scale() function is then called to perform the scaling operation.
▪ In the scale() function, the user is asked to enter the scaling factors along the x and y axes.
▪ The centroid of the triangle is calculated using the formula (mx, my) = ((x1 + x2 + x3) / 3, (y1 +
y2 + y3) / 3).
▪ The new coordinates for each vertex after scaling are computed using the formula (a, b) = (mx +
(x - mx) * scaleX, my + (y - my) * scaleY), where (x, y) are the original coordinates, and scaleX and
scaleY are the scaling factors along the x and y axes, respectively.
▪ The original triangle is cleared from the screen using cleardevice().
▪ The scaled triangle is drawn on the screen using the new coordinates obtained from the scaling
transformation.
▪ The draw() function is called again to display the scaled triangle.
▪ The getch() function waits for a key press before the program terminates.

C program for rotating a 2D triangle using rotation transformation


techniques:

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3);
void RotateTriangle(int x1, int y1, int x2, int y2, int x3, int y3, float angle);

int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
float angle;

initgraph(&gd, &gm, "");

printf("Enter the 1st point for the triangle (x1 y1): ");
scanf("%d%d", &x1, &y1);

printf("Enter the 2nd point for the triangle (x2 y2): ");
scanf("%d%d", &x2, &y2);

printf("Enter the 3rd point for the triangle (x3 y3): ");
scanf("%d%d", &x3, &y3);

DrawTriangle(x1, y1, x2, y2, x3, y3);


printf("Enter the angle for rotation (in degrees): ");
scanf("%f", &angle);

RotateTriangle(x1, y1, x2, y2, x3, y3, angle);

getch();
closegraph();
return 0;
}

void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}

void RotateTriangle(int x1, int y1, int x2, int y2, int x3, int y3, float angle)
{
int p = x2, q = y2;
float radianAngle = (angle * 3.14) / 180.0;

int a1 = p + (x1 - p) * cos(radianAngle) - (y1 - q) * sin(radianAngle);


int b1 = q + (x1 - p) * sin(radianAngle) + (y1 - q) * cos(radianAngle);

int a2 = p + (x2 - p) * cos(radianAngle) - (y2 - q) * sin(radianAngle);


int b2 = q + (x2 - p) * sin(radianAngle) + (y2 - q) * cos(radianAngle);

int a3 = p + (x3 - p) * cos(radianAngle) - (y3 - q) * sin(radianAngle);


int b3 = q + (x3 - p) * sin(radianAngle) + (y3 - q) * cos(radianAngle);

setcolor(1);
DrawTriangle(a1, b1, a2, b2, a3, b3);
}
Detailed Explanation:

Header Files and Main Function

The code starts by including the necessary header files: <stdio.h>, <conio.h>, <graphics.h>,
<process.h>, and <math.h>. These headers provide functions for input/output, graphics handling,
and math calculations. The main() function initializes the graphics mode and sets up the screen
for drawing.

Triangle Function

The DrawTriangle () function takes six integer arguments representing the coordinates of the
three points of the triangle. It uses the line() function from the graphics.h library to draw the
triangle on the screen.

Rotation Function

The RotateTriangle() function performs the rotation of the triangle. It takes six integer arguments
representing the coordinates of the three points of the original triangle. The user is prompted to
enter an angle for rotation. The function then calculates the new coordinates (a1, b1), (a2, b2),
and (a3, b3) for the rotated triangle using the rotation formulas mentioned earlier. The line()
function is used again to draw the rotated triangle on the screen.

Program Execution

In the main() function, the user is prompted to input the coordinates of the triangle's three points.
The TriAngle() function is called to draw the initial triangle on the screen. The user is then
prompted to enter the rotation angle. After the Rotate() function is called, the rotated triangle is
drawn on the screen.

You might also like