Sunday 18 November 2012

Program To Implement 3D Scaling


#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
int a[1][4],i,j,sx,sy,sz,k,b[4][4],c[1][4];
int left=150,top=50,right=200,bottom=150,depth=25,topflag;
void far bar3d(int left,int top,int right,int bottom,int depth,int topflag);
void main()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setfillstyle(LINE_FILL,2);
bar3d(left,top,right,bottom,depth,1);
i=0;
for(j=0;j<3;j++)
c[i][j]=0;
printf("\nEnter the scaling points:");
scanf("%d%d%d",&sx,&sy,&sz);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(i==j)
b[i][j]=1;
else
b[i][j]=0;
}
b[0][0]=sx;
b[1][1]=sy;
b[2][2]=sz;
a[0][0]=left;
a[0][1]=top;
a[0][2]=depth;
a[0][3]=1;
i=0;
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
if(j==0)
left=c[i][j];
if(j==1)
top=c[i][j];
if(j==2)
depth=c[i][j];
c[i][j]=0;
}
a[0][0]=right;
a[0][1]=bottom;
a[0][2]=depth;
a[0][3]=1;
i=0;
for(j=0;j<4;j++) {
for(k=0;k<4;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
if(j==0)
right=c[i][j];
if(j==1)
bottom=c[i][j];
if(j==2)
depth=c[i][j];
c[i][j]=0;
}
bar3d(left,top,right,bottom,depth,1);
getch();
}

Program To Implement 3D Rotation About An Arbitrary Axis


#include<iostream.h>
#include<math.h>
#include<conio.h>
 //using namespace std;
 typedef struct {
    float x;
    float y;
    float z;
}Point;
Point points;
 float rotationMatrix[4][4];
float inputMatrix[4][1] = {0.0, 0.0, 0.0, 0.0};
float outputMatrix[4][1] = {0.0, 0.0, 0.0, 0.0};
 void showPoint(){
    cout<<"("<<outputMatrix[0][0]<<","<<outputMatrix[1][0]<<","<<outputMatrix[2][0]<<")"<<endl;
}
 void multiplyMatrix()
{
    for(int i = 0; i < 4; i++ ){
            for(int j = 0; j < 1; j++){
                outputMatrix[i][j] = 0;
                for(int k = 0; k < 4; k++){
                        outputMatrix[i][j] += rotationMatrix[i][k] * inputMatrix[k][j];
                }
            }
    }
}
void setUpRotationMatrix(float angle, float u, float v, float w)
{
    float L = (u*u + v * v + w * w);
    angle = angle * M_PI / 180.0; //converting to radian value
    float u2 = u * u;
    float v2 = v * v;
    float w2 = w * w;
    rotationMatrix[0][0] = (u2 + (v2 + w2) * cos(angle)) / L;
    rotationMatrix[0][1] = (u * v * (1 - cos(angle)) - w * sqrt(L) * sin(angle)) / L;
    rotationMatrix[0][2] = (u * w * (1 - cos(angle)) + v * sqrt(L) * sin(angle)) / L;
    rotationMatrix[0][3] = 0.0;
    rotationMatrix[1][0] = (u * v * (1 - cos(angle)) + w * sqrt(L) * sin(angle)) / L;
    rotationMatrix[1][1] = (v2 + (u2 + w2) * cos(angle)) / L;
    rotationMatrix[1][2] = (v * w * (1 - cos(angle)) - u * sqrt(L) * sin(angle)) / L;
    rotationMatrix[1][3] = 0.0;
    rotationMatrix[2][0] = (u * w * (1 - cos(angle)) - v * sqrt(L) * sin(angle)) / L;
    rotationMatrix[2][1] = (v * w * (1 - cos(angle)) + u * sqrt(L) * sin(angle)) / L;
    rotationMatrix[2][2] = (w2 + (u2 + v2) * cos(angle)) / L;
    rotationMatrix[2][3] = 0.0;
    rotationMatrix[3][0] = 0.0;
    rotationMatrix[3][1] = 0.0;
    rotationMatrix[3][2] = 0.0;
    rotationMatrix[3][3] = 1.0;
}
int main()
{
    float angle;
    float u, v, w;
    clrscr();
    cout<<" PROGRAM FOR THE ROTATION \n";
    cout<<" Enter the initial point you want to transform:";
    cin>>points.x>>points.y>>points.z;
    inputMatrix[0][0] = points.x;
    inputMatrix[1][0] = points.y;
    inputMatrix[2][0] = points.z;
    inputMatrix[3][0] = 1.0;
    cout<<" Enter axis vector: ";
    cin>>u>>v>>w;
    cout<<" Enter the rotating angle in degree: ";
    cin>>angle;
    setUpRotationMatrix(angle, u, v, w);
    multiplyMatrix();
    showPoint();
    return 0;
}

. Program To Implement Scaling,Rotation and Translation of The Triangle


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
            int gm;
            int gd=DETECT;
            int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
            int sx,sy,xt,yt,r;
            float t;
                initgraph(&gd,&gm,"c:\\turboc3\\bgi");
            printf("\t Program for basic transactions");
            printf("\n\t Enter the points of triangle");
                setcolor(15);
                scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
                line(x1,y1,x2,y2);
                line(x2,y2,x3,y3);
                line(x3,y3,x1,y1);
                getch();
                printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.Exit");
                printf("\nEnter your choice:");
                scanf("%d",&c);
                switch(c)
                {
                                    case 1:
                                                    printf("\n Enter the translation factor");
                                                    scanf("%d%d",&xt,&yt);
                                                    nx1=x1+xt;
                                                    ny1=y1+yt;
                                                    nx2=x2+xt;
                                                    ny2=y2+yt;
                                                    nx3=x3+xt;
                                                    ny3=y3+yt;
                                                    line(nx1,ny1,nx2,ny2);
                                                    line(nx2,ny2,nx3,ny3);
                                                    line(nx3,ny3,nx1,ny1);
                                                    getch();
                                                    break;
                                    case 2:
                                                    printf("\n Enter the angle of rotation");
                                                    scanf("%d",&r);
                                                    t=3.14*r/180;
                                                    nx1=abs(x1*cos(t)-y1*sin(t));
                                                    ny1=abs(x1*sin(t)+y1*cos(t));
                                                    nx2=abs(x2*cos(t)-y2*sin(t));
                                                    ny2=abs(x2*sin(t)+y2*cos(t));
                                                    nx3=abs(x3*cos(t)-y3*sin(t));
                                                    ny3=abs(x3*sin(t)+y3*cos(t));
                                                    line(nx1,ny1,nx2,ny2);
                                                    line(nx2,ny2,nx3,ny3);
                                                    line(nx3,ny3,nx1,ny1);
                                                    getch();
                                                    break;
                                    case 3:
                                                    printf("\n Enter the scalling factor");
                                                    scanf("%d%d",&sx,&sy);
                                                    nx1=x1*sx;
                                                    ny1=y2*sy;
                                                    nx2=x2*sx;
                                                    ny2=y2*sy;
                                                    nx3=x3*sx;
                                                    ny3=y3*sy;
                                                    line(nx1,ny1,nx2,ny2);
                                                    line(nx2,ny2,nx3,ny3);
                                                    line(nx3,ny3,nx1,ny1);
                                                    getch();
                                                    break;
                                    case 4:
                                                    break;
                                    default:
                                                    printf("Enter the correct choice");
                                                    }
                                                    closegraph();
                                                    }

Program To Implement A Line Using Slope Intercept Formula


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
float m,x1,y1,x2,y2;
int x,y;
int gdriver=DETECT,gmode,gerror;
clrscr();
printf(" PROGRAM FOR THE LINE INTERCEPT \n");
printf(" Enter the value of x1");
scanf("%f",&x1);
printf(" Enter the value of y1");
scanf("%f",&y1);
printf(" Enter the value of x2");
scanf("%f",&x2);
printf(" Enter the value of y2");
scanf("%f",&y2);
initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi");
m=(y2-y1)/(x2-x1);
for(x=1;x<=x2;x++)
{
y=m*(x-x1)+y1;
putpixel(x,y,15);
delay(50);
}
getch();
closegraph();
}

Program To Implement 2D Translation of Polygon.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,X4,Y4,mx,my;

void draw();
void poly();
 void main()
{
    int gd=DETECT,gm;
    int c;
    initgraph(&gd,&gm,"c:\\turboc3\\bgi ");
    printf("Enter the 1st point for the polygon:");
    scanf("%d%d",&x1,&y1);
    printf("Enter the 2nd point for the polygon:");
    scanf("%d%d",&x2,&y2);
    printf("Enter the 3rd point for the polygon:");
    scanf("%d%d",&x3,&y3);
    printf("Enter the 4th point for the polygon:");
    scanf("%d%d",&x4,&y4);
    cleardevice();
    draw();
    getch();
    poly();
    getch();
}

void draw()
{
    line(x1,y1,x2,y2);
    line(x2,y2,x3,y3);
    line(x3,y3,x4,y4);
    line(x4,y4,x1,y1);
}
void poly()
{
    int x,y,a1,a2,a3,a4,b1,b2,b3,b4;
    printf("Enter the Transaction coordinates");
    scanf("%d%d",&x,&y);
    cleardevice();
    a1=x1+x;
    b1=y1+y;
    a2=x2+x;
    b2=y2+y;
    a3=x3+x;
    b3=y3+y;
    a4=x4+x;
    b4=y4+y;
    line(a1,b1,a2,b2);
    line(a2,b2,a3,b3);
    line(a3,b3,a4,b4);
    line(a4,b4,a1,b1);

}

Program To Implement 2D Translation of Triangle


#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdio.h>
int x1,y1,x2,y2,x3,y3;
void draw();
void tri();
void main()
{
int gdriver=DETECT,gmode,gerror;
initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi");
printf("\n");
printf("  Enter first point coordinates");
scanf("%d%d",&x1,&y1);
printf("  Enter second point coordinates");
scanf("%d%d",&x2,&y2);
printf("  Enter third point coordinates");
scanf("%d%d",&x3,&y3);
cleardevice();
draw();
getch();
tri();
getch();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void  tri()
{
int x,y,a1,b1,a2,b2,a3,b3;
printf("Enter translation coordinates");
scanf("%d%d",&x,&y);
cleardevice;
a1=x1+x;
b1=y1+y;
a2=x2+x;
b2=y2+y;
a3=x3+x;
b3=y3+y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);}

Friday 2 November 2012

PROGRAM TO IMPLEMENT BEZIR'S ALGORITHM



#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x,y,z;
void main()
{
float u;
int gd,gm,ymax,i,n,c[4][3];
for(i=0;i<4;i++) { c[i][0]=0; c[i][1]=0; }
printf("\n\n Enter four points : \n\n");
for(i=0; i<4; i++)
{
printf("\t X%d Y%d : ",i,i);
scanf("%d %d",&c[i][0],&c[i][1]);
}
c[4][0]=c[0][0];
c[4][1]=c[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
ymax = 480;
setcolor(13);
for(i=0;i<3;i++)
{
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
}
setcolor(3);
n=3;
for(i=0;i<=40;i++)
{
u=(float)i/40.0;
bezier(u,n,c);
if(i==0)
{ moveto(x,ymax-y);}
else
{ lineto(x,ymax-y); }
getch();
}
getch();
}
bezier(u,n,p)
float u;int n; int p[4][3];
{
int j;
float v,b;
float blend(int,int,float);
x=0;y=0;z=0;
for(j=0;j<=n;j++)
{
b=blend(j,n,u);
x=x+(p[j][0]*b);
y=y+(p[j][1]*b);
z=z+(p[j][2]*b);
}
}

float blend(int j,int n,float u)
{
int k;
float v,blend;
v=C(n,j);
for(k=0;k<j;k++)
{ v*=u; }
for(k=1;k<=(n-j);k++)
{ v *= (1-u); }
blend=v;
return(blend);
}

C(int n,int j)
{
int k,a,c;
a=1;
for(k=j+1;k<=n;k++) { a*=k; }
for(k=1;k<=(n-j);k++) { a=a/k; }
c=a;
return(c);
}