Saturday, August 25, 2007

program for shearing of object in C

/* program for shearing of object */

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

float matrix[3][3];
float Xref=200,Yref=200;

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[10][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 shx,float shy)
{
matrix[0][0]=1;
matrix[0][1]=shx;
matrix[0][2]=-shx*Yref;
matrix[1][0]=shy;
matrix[1][1]=1;
matrix[1][2]=-shy*Xref;
matrix[2][0]=0;
matrix[2][1]=0;
matrix[2][2]=1;
}

void main()
{

int gd = DETECT, gm,n=4,i;
float pts[4][2]={{100.0,200.0},{200.0,200.0},{200.0,50.0},{100.0,50.0}},tx,ty;
initgraph(&gd, &gm, "c:\\tc\\bgi\\");
draw_poly(pts,n);
printf("\n\nEnter the 'shx' shy:");
scanf("%f%f",&tx,&ty);

translate(tx,ty);
transpoint(pts,n);
getch();
}

No comments: