C++ Program to implement scaling in graphics

A scaling can be represented by a scaling matrix. To scale an object by a vector v = (vx, vy, vz), each point p = (px, py, pz) would need to be multiplied with this scaling matrix:
 S_v = 
\begin{bmatrix}
v_x & 0 & 0  \\
0 & v_y & 0  \\
0 & 0 & v_z  \\
\end{bmatrix}.
As shown below, the multiplication will give the expected result:

S_vp =
\begin{bmatrix}
v_x & 0 & 0  \\
0 & v_y & 0  \\
0 & 0 & v_z  \\
\end{bmatrix}
\begin{bmatrix}
p_x \\ p_y \\ p_z 
\end{bmatrix}
=
\begin{bmatrix}
v_xp_x \\ v_yp_y \\ v_zp_z
\end{bmatrix}.
Such a scaling changes the diameter of an object by a factor between the scale factors, the area by a factor between the smallest and the largest product of two scale factors, and the volume by the product of all three.

Program

#include <iostream.h>
#include <conio.h>
#include <graphics.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,sx,sy;
initgraph(&gd,&gm,"C:\\Tc\\BGI");
cout<<"SCALING OF A LINE\n";
cout<<"Enter the first coordinate of a line:";
cin>>x1>>y1;
cout<<"Enter the second coordinate of a line:";
cin>>x2>>y2;
line(x1,y1,x2,y2);
cout<<"Enter the scaling factor:";
cin>>sx>>sy;
setcolor(RED);
x1=x1*sx;
y1=y1*sy;
x2=x2*sx;
y2=y2*sy;
line(x1,y1,x2,y2);
getch();
closegraph();
}

1 comment: