Search This Blog

C Program for Scaling 2D transformation - Scaling Polygons in Computer Graphics

This is a c program for scaling transformation in computer graphics. Scaling is one of the important 2d transformations in computer graphics. The program will tell you how to scale lines or polygons. This CG lab program in c language using the graphics library reads the number of sides of polygon, co-ordinates of its vertices and the scale factors in horizontal and vertical directions. It displays the original polygon and scaled polygon in different colors in same screen. More Computer graphics lab problems are available under related posts heading.



#include<stdio.h>
#include<graphics.h>
#include<math.h>
int graDriver=DETECT,graMode;
int n,xs[100],ys[100],i;
float sfx,sfy;

void DrawFn()
{
for(i=0;i<n;i++)
 line(xs[i],ys[i],xs[(i+1)%n],ys[(i+1)%n]);
}

void scale()
{
for(i=0;i<n;i++)
 {
 xs[i]=xs[0]+(int)((float)(xs[i]-xs[0])*sfx);
 ys[i]=ys[0]+(int)((float)(ys[i]-ys[0])*sfy);
 }
}

void main()
{
printf("Enter number of sides: ");
scanf("%d",&n);
printf("Enter co-rdinates: x,y for each point ");
for(i=0;i<n;i++)
 scanf("%d%d",&xs[i],&ys[i]);
printf("Enter scale factors: (xs,ys) ");
scanf("%f%f",&sfx,&sfy);
initgraph(&graDriver,&graMode,"C:\\TURBOC3\\BGI\\");
setcolor(RED);
DrawFn();//original
scale();//scaling
setcolor(BLUE);
DrawFn();
getch();
}


The above program first reads the polygon. Then original polygon is displayed in red color. Then the scaled one is displayed in blue color.

Related Posts

C Program to Rotate a Polygon - Computer Graphics Lab Program
C Program for Translation 2D Transformation in Computer Graphics 2D Transformations C Program - Computer Graphics Lab 2D Transformation
Two Stroke Engine Piston Movement Animation C Program - CG Lab Program DDA Line Drawing Program in C : Computer Graphics Lab Program
Computer Graphics Program in C to Simulate Motion of Simple Pendulum
Blooming Flower and Bee Animation C Program - Computer Graphics Lab Experiment
Caterpillar Eating Leaf - Computer Graphics Lab Animation in C

No comments: