C++ Implementation of Scaling in Computer Graphics
Last Updated :
27 Nov, 2022
In terms of computer graphics, Scaling is a process used for altering the size of objects. It changes the coordinate point of the original object. Modifying the object's size with the help of the object's dimension is dependent on the scaling factor(S).
The scaling factor determines whether the object size is increased or decreased. There are two scaling factors i.e., Sx, in the x-direction, and Sy, in the y-direction.
If scaling factor( Sx/Sy) > 1 => size of the object will be enlarged.
If scaling factor( Sx/Sy) < 1 => size of the object will be reduced.
If Sx and Sy are equal then we call it uniform scaling. If both of them are not equal to each other then we can term it as non-uniform scaling or differential scaling or independent scaling. In the non-uniform scaling case, they have the effect of distorting pictures by elongating or shrinking them along the directions parallel to the coordinate axes.
Non-uniform scaling factorsFormula for the Scaling:
Let us have object O on which we will perform the scaling.
initial coordinates of the object O = (x , y).
scaling factor in x-direction = Sx
scaling factor in y-direction = Sy
new coordinates after scaling = (x1, y1)
hence we have equations for scaling :
Matrix form of above equations,
matrix representation
For homogeneous coordinates, the above scaling matrix may be represented as a 3X3 matrix,
Homogeneous representation of scaling equations
For example, we have a triangle with coordinates (20,0), (60,0), and (40, 100) and scaling factors are Sx = Sy = 2. The new coordinates for the object after scaling will be (40, 0), (120, 0), and (80, 200).
Enlarged image of a given example
Here Sx = Sy = 2, indicates the picture of the object to be enlarged to twice its original size. Also, the enlargement is relative to the origin of the coordinate system.
Program For Scaling :
C++
#include <iostream>
#include <conio.h>
#include <graphics.h>
void main()
{
int gd=DETECT,gm;
float p,q,r,s,Sx,Sy;
initgraph(&gd,&gm,"C:\\Tc\\BGI");
cout<<"Enter the first coordinate of a line:";
cin>>p>>" ">>q;
cout << endl;
cout<<"Enter the second coordinate of a line:";
cin>>r>>" ">>s;
cout << endl;
line(p,q,r,s);
cout<<"Enter the scaling factor:";
cin>>Sx>>" ">>Sy;
cout << endl;
p=p*Sx;
q=q*Sy;
r=r*Sx;
s=s*Sy;
line(p,q,r,s);
getch();
closegraph();
}
Output:
the output of the code
Similar Reads
Implementation of Graph in C Graphs are a versatile data structure that can be used to represent various real-world problems, from social networks to transportation systems. In C, graphs are typically represented using arrays for adjacency matrices or linked lists for adjacency lists. This article will introduce the concept of
13 min read
Multiple Windowing in Computer Graphics Pre-requisites: Line Clipping Clipping is the process of removing undesired parts of an image and displaying only a certain portion of the image. Clipping is used to remove lines or objects that are not inside the viewing pane. The portion that is being removed or clipped is called the clipped part.
2 min read
Introduction to Computer Graphics The term 'Computer Graphics' was coined by Verne Hudson and William Fetter from Boeing who were pioneers in the field. Computer graphics is a dynamic and essential field within computing that involves the creation, manipulation, and rendering of visual content using computers.In today's digital era,
5 min read
Segments in Computer Graphics Introduction : Introduction segments are a fundamental concept in computer graphics, used to represent the basic building blocks of a graphical scene. They are commonly used in 2D graphics to represent lines or curves that connect two or more points. An introduction segment is defined by two endpoin
8 min read
Projections in Computer Graphics Representing an n-dimensional object into an n-1 dimension is known as projection. It is process of converting a 3D object into 2D object, we represent a 3D object on a 2D plane {(x,y,z)->(x,y)}. It is also defined as mapping or transforming of the object in projection plane or view plane. When g
5 min read
Motion Capture in Computer Graphics Today, we see many movies, games, animations, 2D and 3D images, and drug designing. Have you ever wondered how these things have been created and how they work? These projections replicate the same of living beings and creatures. This could done with Computer Graphics. What is Computer Graphics?Comp
5 min read
Graphics Software Standards in Computer Graphics Graphics software standards help to create and set a standard for the development of computer graphics, as there are mainly two categories or general classes of computer graphics: General programming packages and Special-purpose applications packages. IntroductionEach of these classes has its softwa
4 min read
Computer Graphics - 3D Scaling Transformation Prerequisite: Computer Graphics â 3D Translation Transformation Scaling Transformation : It is performed to resize the 3D-object that is the dimension of the object can be scaled(alter) in any of the x, y, z direction through Sx, Sy, Sz scaling factors. Matrix representation of Scaling transformatio
3 min read
Gouraud Shading in Computer Graphics Gouraud shading is a method used in computer graphics to simulate the differing effect of light and color across the surface of an object. Intensity- interpolation is used to develop Gouraud shading. By intensity interpolation, the intensity of each and every pixel is calculated. Gouraud shading sha
3 min read
Constant-Intensity Shading in Computer Graphics In computer graphics shading is referred to as the process of altering the color of an object in the 3D scene based on the surface angle to lights, its distance from the light, and the material properties. Constant intensity is also known as flat shading. This is the most simple and very fast method
2 min read