Triangle scaling in computer graphics

Transformation of any object in a 2D or 3D plane means the change in the looks of the object by applying certain rules.

 Scaling-

It is one of the transformation technique. Scaling changes the size of an object.This operation is carried out in any polygon by multiplying the scaling factor (Sx,Sy) with each vertex coordinate value (x,y) of the polygon.This results in transform coordinates (x',y') and then we plot the transformed coordinates.

Here is the program to scale a triangle so 
Let's suppose (x1,y1) (x2,y2) (x3,y3) are the coordinates of a triangle and the scaling factor is (Sx,Sy) then transform coordinates will be-




                     (x1',y1')=(Sx*x1,Sy*y1)
                     (x2',y2')=(Sx*x2,Sy*y2) 
                     (x3',y3')=(Sx*x3,Sy*y3)  

One thing you need to keep in mind that theco-ordinates you are providing must form a triangle.

 C program for triangle scaling-



#include<stdio.h>
#include<graphics.h>
#include<math.h>
//function for triangle scaling
void scaling(int x1,int y1,int x2,int y2,int x3,int y3)
{
int sx,sy,xn1,yn1,xn2,xn3,yn3,yn2,gd,gm;
printf("enter the scaling vector\n");
scanf("%d %d",&sx,&sy);
//scale the co-ordinates of triangle vertices by multiplying the scaling vector
xn1=x1*sx;
yn1=y1*sy;
xn2=x2*sx;
yn2=y2*sy;
xn3=x3*sx;
yn3=y3*sy;
//initializing the graph
initgraph(&gd,&gm,"");
//print the intitial triangle
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
delay(600);
//print the scaled triangle
line(xn1,yn1,xn2,yn2);
line(xn1,yn1,xn3,yn3);
line(xn2,yn2,xn3,yn3);
delay(600);
cleardevice();

}
int main()
{
int ch,x1,y1,x2,y2,x3,y3;
printf("enter the vertex co-ordinates of triangle\n");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
scaling(x1,y1,x2,y2,x3,y3);
return 0;
}

 OUTPUT-

Execution steps for Linux users-

  • first of all, save the code given above with .c extension i.e.(filename.c) 
  • After that open your terminal in the same folder where you have saved the file.
  • Run the command given below in terminal-          
 

             gcc filename.c -lgraph
    • type ./a.out and see the output.
    You can scale the same triangle with respect to any vertex.To do this just don't scale that vertex.


        Comments :

        Post a Comment