Computer Graphics Program source codes with full description.

Study of Various C Graphics Functions

Implementation and Using mouse in DOS

Implementation of DDA line algorithm with source code in C/C++.

Implementation of Bresenham Line algorithm with source code in C/C++.

Implementation of Midpoint Line algorithm with source code in C/C++.

Implementation of Bresenham Circle algorithm with source code in C/C++.

Implementation of Mid-point Circle algorithm with source code in C/C++.

Implementation of Mid-point Ellipse algorithm with source code in C/C++.

Implementation of Polygon Filling using Scan Fill with source code in C/C++.

Implementation of Polygon Filling using Flood Fill with source code in C/C++.

Implementation of Polygon Filling using Boundary Fill Algorithms.

Implementation of algorithm of 2D Transformation of an Object with source code in C/C++.

Implementation of Line Clipping using Cohen- Sutherland algorithm with source code in C/C++.

Implementation of Line Clipping using Liang-Barky algorithm with source code in C/C++.

Implementation of Polygon clipping using Sutherland-hodgeman algorithm with source code in C/C++.

Search

Friday 1 July 2011

Implement algorithm of 2D Transformation of an Object.

Source Code: Translation ,Rotation, Scaling, Reflection , Shearing

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int ch,x,y,az,i,w,ch1,ch2,xa,ya,ra,a[10],b[10],da,db;
float x1,y1,az1,w1,dx,dy,theta,x1s,y1s,sx,sy,a1[10],b1[10];
void main()
{
int gm ,gr,xmax,ymax;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Enter the upper left corner:");
scanf("%d%d",&x,&y);
printf("Enter the lower right corner:");
scanf("%d%d",&az,&w);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
//setviewport(1,35,xmax-1,ymax-1,1);
rectangle(((xmax)/2)+x,((ymax)/2)+y,((xmax)/2)+az,((ymax)/2)+w);
da=az-x;
db=w-y;
a[0]=x;
b[0]=y;
a[1]=x+da;
b[1]=y;
a[2]=x+da;
b[2]=y+db;
a[3]=x;b[3]=y+db;
while(1)
{
printf(" Transformations\n");
printf("Enter your choice\n");
printf("1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing\n6.Exit\nEnter your choice:\n");
scanf("%d",&ch);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
switch(ch)
{
case 1:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(((xmax)/2)+x,((ymax)/2)+y,((xmax)/2)+az,((ymax)/2)+w);
printf("Translation\n\n");
printf("Enter the two values of shift vectors tx & ty:\n");
scanf("%f%f",&dx,&dy);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
x1=x+dx;
y1=y+dy;
az1=az+dx;
w1=w+dy;
rectangle(((xmax)/2)+x1,((ymax)/2)+y1,((xmax)/2)+az1,((ymax)/2)+w1);
break;
case 2:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(((xmax)/2)+x,((ymax)/2)+y,((xmax)/2)+az,((ymax)/2)+w);
printf("Rotation\n\n");
printf("Enter the two value of fixed point and angle of rotation:\n");
scanf("%d%d%d",&xa,&ya,&ra);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
theta=(float)(ra*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[i+1],((ymax)/2)+b1[i+1]);
else
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[0],((ymax)/2)+b1[0]);
}
break;
case 3:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(((xmax)/2)+x,((ymax)/2)+y,((xmax)/2)+az,((ymax)/2)+w);
printf("Scaling\n\n");
printf("Enter the two value sx and sy of scaling factor:\n");
scanf("%f%f",&sx,&sy);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
x1=x*sx;
y1=y*sy;
az1=az*sx;
w1=w*sy;
rectangle(((xmax)/2)+x1,((ymax)/2)+y1,((xmax)/2)+az1,((ymax)/2)+w1);
break;
case 4:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(((xmax)/2)+x,((ymax)/2)+y,((xmax)/2)+az,((ymax)/2)+w);
printf("Reflection\n");
printf("Select your choice\n");
printf("1.About x-axis\n2.About y-axis\n3.About both axis\nEnter your choice:\n");
scanf("%d",&ch1);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
switch(ch1)
{
case 1:
printf("Enter the two fixed point\n");
scanf("%d%d",&xa,&ya);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
theta=(float)(90*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[i+1],((ymax)/2)+b1[i+1]);
else
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[0],((ymax)/2)+b1[0]);
}
break;
case 2:
printf("Enter the two fixed point\n");
scanf("%d%d",&xa,&ya);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
theta=(float)(270*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[i+1],((ymax)/2)+b1[i+1]);
else
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[0],((ymax)/2)+b1[0]);
}
break;
case 3:
printf("Enter the two fixed point\n");
scanf("%d%d",&xa,&ya);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
theta=(float)(180*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[i+1],((ymax)/2)+b1[i+1]);
else
line(((xmax)/2)+a1[i],((ymax)/2)+b1[i],((xmax)/2)+a1[0],((ymax)/2)+b1[0]);
}
break;
}
break;
case 5:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(((xmax)/2)+x,((ymax)/2)+y,((xmax)/2)+az,((ymax)/2)+w);
printf("Shearing\n\n");
printf("1.x-direction shear\n2.y-direction shear\nEnter your choice:\n");
scanf("%d",&ch2);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
switch(ch2)
{
case 1:
printf("Enter the value of shx:\n");
scanf("%f",&x1s);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
x1=x+(y*x1s);
y1=y;
az1=az+(w*x1s);
w1=w;
rectangle(((xmax)/2)+x1,((ymax)/2)+y1,((xmax)/2)+az1,((ymax)/2)+w1);
break;
case 2:
printf("Enter the value of shy:\n");
scanf("%f",&y1s);
xmax=getmaxx();
ymax=getmaxy();
line((xmax/2)+1,0,(xmax/2)+1,ymax);
line(0,(ymax/2),xmax,(ymax/2));
x1=x;
y1=y+(x*y1s);
az1=az;
w1=w+(az*y1s);
rectangle(((xmax)/2)+x1,((xmax)/2)+y1,((xmax)/2)+az1,((xmax)/2)+w1);
break;
}
break;
case 6:
exit(0);
}
}
getch();
}

Download the sorce code

No comments:

Post a Comment

Copyright Text

Copyright @ LDRP Student Community, webmaster Rahul Bhadauriya