
Program to draw a polygon by accepting vertex coordinates and perform basic transformations (translation, scaling, rotation) about a given point based on user’s choice. - MASTER GUIDE
Program to draw a polygon by accepting vertex coordinates and perform basic transformations (translation, scaling, rotation) about a given point based on user’s choice.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void input();
void drawobj();
void translation();
void scaling();
void rotation();
int n,i,ch;
float x[10],y[10],tx,ty,sx,sy,xt,yt,r,px,py;
int maxx=640,maxy=480,midx=320,midy=240;
void input()
{
printf("Enter the number of vertices : ");
scanf("%d",&n);
printf("Enter %d x-y co-ordinates\n",n);
for(i=0;i<n;i++)
{
printf("Enter the x and y coordinates : ");
scanf("%f%f",&x[i],&y[i]);
}
printf("Original Object\n");
setcolor(WHITE);
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
outtextxy(midx-2,midy+2,"0,0");
drawobj();
getch();
}
void drawobj()
{
for(i=0;i<n-1;i++)
line(midx+x[i],midy-y[i],midx+x[i+1],midy-y[i+1]);
line(midx+x[i],midy-y[i],midx+x[0],midy-y[0]);
}
Baca Juga
void translation()
{
printf("\nEnter the translation unit tx,ty : ");
scanf("%f%f",&tx,&ty);
printf("After translation");
for(i=0;i<n;i++)
{
x[i]=x[i]+tx;
y[i]=y[i]+ty;
}
setcolor(RED);
drawobj();
getch();
}
void scaling()
{
printf("Enter scaling unit sx,sy : ");
scanf("%f%f",&sx,&sy);
printf("Enter the fixedpoint px and py : ");
scanf("%f%f",&px,&py);
printf("After scaling");
for(i=0;i<n;i++)
{
x[i]=x[i]*sx + px*(1-sx);
y[i]=y[i]*sy + py*(1-sy);
}
setcolor(RED);
drawobj();
getch();
}
void rotation()
{
int xshift,yshift;
printf("Enter angle of rotation : ");
scanf("%f",&r);
r=r*(3.1415/180);
printf("Enter the pivot point px and py : ");
scanf("%f%f",&px,&py);
printf("After rotation\n");
for(i=0;i<n;i++)
{
xshift=x[i]-px;
yshift=y[i]-py;
x[i]=px+ (xshift)*cos(r) - (yshift) * sin(r);
y[i]=py+ (xshift) * sin(r) + (yshift) *cos(r);
}
setcolor(RED);
drawobj();
getch();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
while(1)
{
clrscr();
cleardevice();
printf("Menu\n");
printf("1:Translation\n2:Scaling\n3:Rotation\n4:Exit\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
input();
translation();
break;
case 2:
input();
scaling();
break;
case 3:
input();
rotation();
break;
case 4: closegraph();
exit();
}
}
}
0 Response to "Program to draw a polygon by accepting vertex coordinates and perform basic transformations (translation, scaling, rotation) about a given point based on user’s choice. - MASTER GUIDE"
Post a Comment