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<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,q,RED);

} /* End of draw_pixel */



void LineDDA(int xa, int ya, int xb, int yb)

{

        /* This function draws a line using Digital Differential Analyzer Algorithm */

        int dx,dy,steps,k;

        float xIncrement, yIncrement, x, y;



        x=xa;

        y=ya;

        dx=xb-xa;

        dy=yb-ya;



        if(abs(dx)>abs(dy))

                steps=abs(dx);

        else  

                steps=abs(dy);



        xIncrement = dx / (float) steps;

        yIncrement = dy / (float) steps;



        /* plot the first point */

        draw_pixel(ROUND(x),ROUND(y));



        for(k=0;k<steps;k++)

        {

                x += xIncrement;

                y += yIncrement;

                draw_pixel(ROUND(x),ROUND(y));

        }

/* End of LineDDA */

}



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

        LineDDA(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==RED)

                        {

                                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