Search This Blog

C Program for Translation 2D Transformation in Computer Graphics

This is a c program for translation transformation in computer graphics. Translation (or shifting) is a very basic 2d transformation operation in computer graphics. Translation or shifting is done by adding the distance to shifted to the co-ordinates. For example, if we want to shift a polygon 50 units (pixels) horizontally, we just have to add 50 to x co-ordinates of all vertices of the polygon. Similarly, if it is translate only vertically by 100 unit, add 100 to y co-ordinates of all vertices. 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 translation distance along both axes. It displays the original polygon and translated polygon in different colors in same screen. This is only a very simple and basic Computer graphics lab problem.



#include<stdio.h>
#include<graphics.h>
int graDriver=DETECT,graMode;
int n,xs[100],ys[100],i,xshift,yshift;

void DrawFn();
void translate();

void main()
{
printf("Enter number of sides of polygon: ");
scanf("%d",&n);
printf("Enter co-rdinates: x,y for each vertex ");
for(i=0;i<n;i++)
 scanf("%d%d",&xs[i],&ys[i]);
printf("Enter distances for translation (in x and y directions): ");
scanf("%d%d",&xshift,&yshift);
initgraph(&graDriver,&graMode,"C:\\TURBOC3\\BGI\\");
cleardevice();
//drawing original polygon in RED color
setcolor(RED);
DrawFn();
//Doing translation
translate();
//drawing translated polygon in BLUE color
setcolor(BLUE);
DrawFn();
getch();
}

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

void translate()
{
for(i=0;i<n;i++)
 {
 xs[i]+=xshift;
 ys[i]+=yshift;
 }
}

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

Related Posts

2D Transformations C Program - Computer Graphics Lab 2D Transformation 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

No comments: