A scaling transformation changes the size of an object. Scaling may be in any terms means either increasing the original size or decreasing the original size. An exemplary scaling is shown in the above figure where scaling factors used Sx=3 and Sy=2. So, what are these scaling factors and how they work very simple, simply we multiply each coordinate with its respective scaling factor.
Therefore, scaling with respect to origin is achieved by multiplying x coordinate with factor Sx and y coordinate with factor Sy. Therefore, following equations can be expressed:
x′ = x.Sx
y′ = y.Sy
In matrix form it can be expressed as:
P′ = S.P
#include<stdio.h>#include<dos.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#define ROUND(a) ((int)(a+0.5))
void initialize_graph(void)
{
/* select a driver and mode that supports */
/* setalso background color */
/* for example: int gdriver=EGA; int gmode=EGAHI */
int gdriver=DETECT;
int gmode;
initgraph(&gdriver,&gmode,"\\tc2");
/* setbkcolor(BLUE); */
} /* end of initialize_graph */
void draw_pixel(w,q)
int w,q;
{
/* This function draws a pixel */
/* recorrelate to start from the lower left corner */
putpixel(w,480-q,RED);
} /* End of draw_pixel */
void scale(void)
{
int xf,yf,i,j,pixelcolor;
float sx,sy,newx,newy;
cleardevice();
printf("\nEnter the scaling point: \n");
printf("xf= "); scanf("%d",&xf);
printf("yf= "); scanf("%d",&yf);
printf("\nEnter the scaling factor: \n");
printf("sx= "); scanf("%d",&sx);
printf("sy= "); scanf("%d",&sy);
cleardevice();
line(250,300,400,450);
draw_pixel(xf,yf);
getch();
for(i=0;i<=getmaxx();i++)
{
for(j=0;j<=getmaxy();j++)
{
pixelcolor=getpixel(i,j);
if(pixelcolor==WHITE)
{
newx = xf+(i-xf)*sx;
newy = yf+(j-yf)*sy;
newx = ROUND(newx);
newy = ROUND(newy);
draw_pixel(newx,newy);
}
}
}
} /* End of scale */
void close_graph(void)
{
/* clean up */
closegraph();
} /* End of close_graph */
/* Start of main Program */
void main(void)
{
initialize_graph();
scale();
getch();
close_graph();
}
0 comments
Post a Comment