Saturday, August 25, 2007

program for transtlation polygon using C

/* program for transtlation polygon */

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

float matrix[3][3];

void draw_poly(float array[][2],int n)
{
int i;
for(i=0;i<n-1;i++)
{
line(array[i][0],array[i][1],array[i+1][0],array[i+1][1]);
}

line(array[i][0],array[i][1],array[0][0],array[0][1]);
}

void transpoint(float array[][2],int n)
{
float p[3][2],x,y;
int i;

for(i=0;i<n;i++)
{
x=matrix[0][0]*array[i][0] + matrix[0][1]*array[i][1] + matrix[0][2];
y=matrix[1][0]*array[i][0] + matrix[1][1]*array[i][1] + matrix[1][2];
p[i][0]=x;
p[i][1]=y;
}
draw_poly(p,n);
}

void translate(float tx,float ty)
{
matrix[0][0]=1;
matrix[0][1]=0;
matrix[0][2]=tx;
matrix[1][0]=0;
matrix[1][1]=1;
matrix[1][2]=ty;
matrix[2][0]=0; //
matrix[2][1]=0; //not used in traslation
matrix[2][2]=1; //
}

void main()
{

int gd = DETECT, gm,n,i,j;

float pts[10][2];//={{320,50},{160,150},{480,150}}
float tx,ty;

initgraph(&gd, &gm, "c:\\tc\\bgi");
printf("Enter no of edges");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter X Of edge %d ",i+1);
scanf("%f",&pts[i][0]);
printf("Enter Y of edge %d ",i+1);
scanf("%f",&pts[i][1]);
}
draw_poly(pts,n);
printf("Enter the 'tx' and 'ty' :");
scanf("%f %f",&tx,&ty);
translate(tx,ty);
transpoint(pts,n);
getch();
cleardevice();
}

No comments: