2D Scaling Program in C: Understanding Graphics Transformation with Detailed Explanation
🎯 Introduction
In computer graphics, scaling is a fundamental transformation that alters the size of an object while preserving its shape. It is commonly used to zoom in or out of an image, adjust the size of graphical elements, and resize geometrical shapes. This blog post presents a 2D scaling program implemented in C programming language. The program takes user inputs for the coordinates of a triangle, performs the scaling transformation based on user-defined scaling factors, and displays the scaled triangle on the screen.
🎯 Concept of Scaling
Scaling is a transformation that multiplies the coordinates of an object by scaling factors along the x and y axes. Given a point (x, y) in the original object, the new coordinates after scaling are (x * scaleX, y * scaleY), where scaleX and scaleY are the scaling factors along the x and y axes, respectively. To apply scaling to a 2D shape, we typically perform the following steps:
Calculate the center of the object.
Compute the new coordinates of each vertex using the scaling formula.
Draw the scaled object on the screen.
🎯 2D scaling program in C
Below is the 2D scaling program in C with a detailed explanation:
#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();
}
Now you have both the C program and the detailed explanation for the 2D scaling program. Remember that the graphics-related functions in this program are specific to certain compilers and might not work in modern C compilers. For cross-platform graphics rendering, consider using alternative libraries like SDL or OpenGL.
🎯 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.
🎯 Summary
This C program demonstrates 2D scaling using the graphics.h library. It allows the user to input the coordinates of a triangle and specify the scaling factors along the x and y axes. The program then calculates the centroid of the triangle and applies the scaling transformation to the vertices. The scaled triangle is displayed on the screen, providing a visual representation of the scaling effect.
🎯 Key Points
Scaling is a transformation that changes the size of an object while preserving its shape.
The C program uses the graphics.h library for basic graphics operations.
The centroid of the triangle is calculated as the average of the coordinates of its three vertices.
The scaling factors along the x and y axes are user-defined.
The program performs the scaling transformation based on the user's input and displays the scaled triangle on the screen.
Computer Graphics Programs
2D Computer Graphics Programs
3D Computer Graphics Programs