Wednesday, 10 December 2014

Program to represent a 3D object using polygon surfaces

#include <stdio.h>
#include <GL/glut.h>
void display(void)
{
 glClear( GL_COLOR_BUFFER_BIT);
 glColor3f(0.0, 1.0, 0.0);

 glBegin(GL_LINES);
 
    glVertex3f(1.61,1.45,0.0);
glVertex3f(2.01,1.45,0.0);

glVertex3f(2.01,1.45,0.0);
glVertex3f(2.01,2.65,0.0);

glVertex3f(2.01,2.65,0.0);
glVertex3f(2.81,2.65,0.0);

glVertex3f(2.81,2.65,0.0);
glVertex3f(2.81,3.05,0.0);

glVertex3f(2.81,3.05,0.0);
glVertex3f(0.81,3.05,0.0);

glVertex3f(0.81,3.05,0.0);
glVertex3f(0.81,2.65,0.0);

glVertex3f(0.81,2.65,0.0);
glVertex3f(1.61,2.65,0.0);

glVertex3f(1.61,2.65,0.0);
     glVertex3f(1.61,1.45,0.0);
//BACK T END
glVertex3f(1.31,1.15,0.0);
glVertex3f(1.71,1.15,0.0);

glVertex3f(1.71,1.15,0.0);
glVertex3f(1.71,2.35,0.0);

glVertex3f(1.71,2.35,0.0);
glVertex3f(2.51,2.35,0.0);


glVertex3f(2.51,2.35,0.0);
glVertex3f(2.51,2.75,0.0);

glVertex3f(2.51,2.75,0.0);
glVertex3f(0.51,2.75,0.0);

glVertex3f(0.51,2.75,0.0);
glVertex3f(0.51,2.35,0.0);

glVertex3f(0.51,2.35,0.0);
glVertex3f(1.31,2.35,0.0);

glVertex3f(1.31,2.35,0.0);
     glVertex3f(1.31,1.15,0.0);

//FRONT END
glVertex3f(1.31,1.15,0.0);
glVertex3f(1.61,1.45,0.0);

glVertex3f(1.71,1.15,0.0);
glVertex3f(2.01,1.45,0.0);

glVertex3f(1.71,2.35,0.0);
glVertex3f(2.01,2.65,0.0);

glVertex3f(2.51,2.35,0.0);
 glVertex3f(2.81,2.65,0.0);

glVertex3f(2.51,2.75,0.0);
glVertex3f(2.81,3.05,0.0);

glVertex3f(0.51,2.75,0.0);
 glVertex3f(0.81,3.05,0.0);

 glVertex3f(0.51,2.35,0.0);
  glVertex3f(0.81,2.65,0.0);

glVertex3f(1.31,2.35,0.0);
glVertex3f(1.61,2.65,0.0);

 glEnd();
 glFlush();
}

int main(int argc, char **argv)
{
 //printf("hello world\n");
 glutInit(&argc, argv);
 glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

 glutInitWindowPosition(300,300);
 glutInitWindowSize(500,500);
 glutCreateWindow ("lines");

 glClearColor(0.0, 0.0, 0.0, 0.0);         // black background
 glMatrixMode(GL_PROJECTION);              // setup viewing projection
 glLoadIdentity();                           // start with identity matrix
 glOrtho(0.0, 5.0, 0.0, 5.0, -10.0, 10.0);   // setup a 10x10x2 viewing world

 glutDisplayFunc(display);
 glutMainLoop();

 return 0;
}

POLYGON CLIPPING USING SUTHERLAND-HODGEMAN ALGORITHM

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

void clip(float,float,float);
int i,j=0,n;
int rx1,rx2,ry1,ry2;
float x1[8],y1[8];
void main()
{
int gd=DETECT,gm;
int i,n;
float x[8],y[8],m;
clrscr();
initgraph(&gd,&gm,"..//BGI");
printf("Enter the Coordinates for Window :-\t ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("Enter the no. of sides for polygon :-\t ");
scanf("%d",&n);
printf("Enter coordinates for polygon:-\t ");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
cleardevice();
outtextxy(10,10,"Before clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
clip(x[i+1],y[i+1],m);
}
m=(y[i]-y[0])/(x[i]-x[0]);
clip(x[i],y[i],m);
clip(x[0],y[0],m);
outtextxy(10,10,"After clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
}
void clip(float e,float f,float m)
{
while(e<rx1 || e>rx2 || f<ry1 || f>ry2)
{
if(e<rx1)
{
f+=m*(rx1-e);
e=rx1;
}
else if(e>rx2)
{
f+=m*(rx2-e);
e=rx2;
}
if(f<ry1)
{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
{
e+=(ry2-f)/m;
f=ry2;
}
}
x1[j]=e;
y1[j]=f;
j++;
}
OUTPUT:-






LINE CLIPPING USING LIANG-BARSKY METHOD

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd = DETECT,gmode;
int i,x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2;
float t1,t2,p[4],q[4],temp;
initgraph(&gd,&gmode,"C://TurboC3/BGI");
/*x1 = 100;
y1 = 200;
x2 = 300;
y2 = 380;
*/
printf("Enter the end points of your line\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
xmin = 150;
xmax = 350;
ymin = 150;
ymax = 350;
//line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
p[0] = -(x2-x1);
p[1] = (x2-x1);
p[2] = -(y2-y1);
p[3] = (y2-y1);

q[0] = (x1-xmin);
q[1] = (xmax-x1);
q[2] = (y1-ymin);
q[3] = (ymax-y1);

for (i = 0; i < 4; i++)
{
if (p[i] == 0)
{
printf("Line is parallel to one of the clipping boundary\n");
if (q[i]>=0)
{
if (i<2)
{
if (y1<ymin)
{
y1 = ymin;
}
if (y2>ymax)
{
y2 = ymax;
}
line(x1,y1,x2,y2);
}
if (i>1)
{
if (x1<xmin)
{
x1 = xmin;
}
if (x2>xmax)
{
x2 = xmax;
}
line(x1,y1,x2,y2);
}
}
getch();
}
}
t1 = 0;
t2 = 1;
for (i = 0; i < 4; i++)
{
temp = q[i]/p[i];
if(p[i]<0)
{
if (t1<=temp)
{
t1 = temp;
}
}
else
{
if (t2>temp)
{
t2 = temp;
}
}
}
if (t1<t2)
{
xx1 = x1 + t1*p[1];
xx2 = x1 + t2*p[1];
yy1 = y1 + t1*p[3];
yy2 = y1 + t2*p[3];
line(xx1,yy1,xx2,yy2);
}
getch();
}

OUPUT:-
Before clipping:

After clipping:


Implementation of Area Filling Algorithm - Flood Fill

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void floodFill(int x, int y, int old, int fill)
{
int current;
current=getpixel(x,y);
if(current==old && current!=fill)
{
putpixel(x,y,fill);
delay(5);
floodFill(x+1,y,old,fill);
floodFill(x-1,y,old,fill);
floodFill(x, y+1,old,fill);
floodFill(x,y-1,old,fill);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");

line(300,300,320,300);
line(320,300,320,220);
line(320,220,360,220);
line(360,220,360,200);
line(360,200,260,200);
line(260,200,260,220);
line(260,220,300,220);
line(300,220,300,300);
      floodFill(310,290,0,20);
     // floodFill(90,70,0,25);
getch();
}
Outpot :-

Program for performing Two Dimensional Transformations : Translation , Scaling , Rotation , Reflection , Shear by using a homogeneous Matrix representation ,use of a function for matrix multiplication is desirable , so as to perform composite transformation.

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

float ptmatr[3][9]={{100,120,120,160,160,60,60,100,100},{20,20,100,100,120,120,100,100,20},{1,1,1,1,1,1,1,1,1}};
void trans();
void scal();
void rotn();
void shear();
void refln();
void matrixmul(float a[3][3], float b[3][9]);
void displayorgobj();
void dda(int x1,int y1,int x2,int y2);


void main()
 {
int op,flag=1,a,gm,gd=DETECT;
initgraph(&gd,&gm,"..\\bgi");
clrscr();
do
{
printf("\t\n Operation :-");
printf("\t\n1. Display original object.\n");
printf("\t\n2. Perform Translation.\n");
printf("\t\n3. Perform Scaling.\n");
printf("\t\n4. Perform  Rotation.\n");
printf("\t\n5. Sharing the Object.\n");
printf("\t\n6. Perform Reflection.\n");
printf("\t\n7. Exit\n");
printf("\t\n Choose the operation to be performed :-\t");

scanf("%d",&op);
switch(op)
{
case 1:  printf("\t\n Displaying original object ::-\t\n");
displayorgobj();
break;
case 2:  printf("\t\n TRANSLATION ::-\t\n");
trans();
displayorgobj();
break;
case 3:  printf("\t\n SCALING ::-\t\n");
scal();
displayorgobj();
break;
case 4:  printf("\t\n ROTATION ::-\t\n");
rotn();
displayorgobj();
break;
case 5:  printf("\t\n SHEARING ::-\t\n");
shear();
displayorgobj();
break;
case 6:  printf("\t\n REFLECTION ::-\t\n");
refln();
displayorgobj();
break;
case 7: flag=0;
break;
default: printf("\n\nPlease select proper option !!!!!!!!!!!!!!!\n");
}
getch();
clrscr();
}
while(flag==1);
}

void matrixmul(float a[3][3], float b[3][9])
 {
int i,j,k;
float c[3][9], sum=0;
int p=0,q=0;
for(i=0; i<3; i++)
{
for(j=0; j<9; j++)
{
for(k=0; k<3; k++)
{
sum+=a[i][k]*b[k][j];
}
c[i][j]=sum;
sum=0;
}
   }
for (p=0; p<8; p++)
dda(c[q][p], c[q+1][p], c[q][p+1], c[q+1][p+1]);
dda(c[0][7], c[1][7], c[0][0], c[1][0]);
}

void trans()
 {
float tx, ty;
float txmatrix[3][3]={{1,0,0}, {0,1,0}, {0,0,1}};
printf("\n\t Enter translation parameter tx & ty :- \n");
scanf("%f %f",&tx, &ty);
txmatrix[0][2]=tx;
txmatrix[1][2]=ty;
matrixmul(txmatrix, ptmatr);
multiply(ptmatr,mat);
 }

void scal()
 {
float sx, sy, scalmatrix[3][3]={{0,0,0}, {0,0,0}, {0,0,1}};
printf("\n\tEnter scaling parameter sx & sy :- \n");
scanf("%f %f",&sx, &sy);
scalmatrix[0][0]=sx;
scalmatrix[1][1]=sy;
matrixmul(scalmatrix, ptmatr);
multiply(ptmatr,mat);
 }

void rotn()
 {
float deg,rotnmatrix[3][3]={{0,0,0}, {0,0,0}, {0,0,1}};
printf("\n\tEnter value of angle in degree :-\t");
scanf("%f",&deg);
rotnmatrix[0][0]=cos(deg*(3.142857/180));
rotnmatrix[0][1]=-sin(deg*(3.142857/180));
rotnmatrix[1][0]=sin(deg*(3.142857/180));
rotnmatrix[1][1]=cos(deg*(3.142857/180));
matrixmul(rotnmatrix, ptmatr);
 }

void refln()
{
float flag, reflnmatrix[3][3]={0,0,0,0,0,0,0,0,1};
printf("\n\tEnter '0' for reflection on X axis or \n\t'1' for reflection on Y axis or \n\t'2' for reflection about origin :-\t");
scanf("%f",&flag);
if(flag==0)
{
reflnmatrix[0][0]=1;
reflnmatrix[1][1]=-1;
}
else

{
if(flag==1)
{
reflnmatrix[0][0]=-1;
reflnmatrix[1][1]=1;
}
else
{
reflnmatrix[0][0]=-1;
reflnmatrix[1][1]=-1;
}
}
matrixmul(reflnmatrix, ptmatr);
}

void shear()
 {
float shx, shy, shearmatrix[3][3]={{1,0,0}, {0,1,0}, {0,0,1}};
printf("Enter shearing parameters shx& shy: ");
scanf("%f%f",&shx,&shy);
shearmatrix[0][1]=shx;
shearmatrix[1][0]=shy;
matrixmul(shearmatrix, ptmatr);
 }

void dda(int x1,int y1,int x2,int y2)
{
int dx,dy,steps,k,xmax,ymax,a,b,c,d;
float x,y,xincr,yincr;
xmax=getmaxx()/2;
ymax=getmaxy()/2;
a=xmax+x1;
b=ymax-y1;
c=xmax+x2;
d=ymax-y2;
line(xmax,0,xmax,getmaxy());
line(getmaxx(),ymax,0,ymax);
dx=c-a;
dy=d-b;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincr=(float)dx/steps;
yincr=(float)dy/steps;
x=a;
y=b;
putpixel(floor(x),floor(y),15);
for(k=1;k<=steps;k++)
{
x+=xincr;
y+=yincr;
putpixel(floor(x),floor(y),15);
}
 }
void displayorgobj()
{
int i;
int arr[2][9]={{100,120,120,160,160,60,60,100,100},{20,20,100,100,120,120,100,100,20}};
for(i=0; i<8; i++)
    {
dda(arr[0][i],arr[1][i],arr[0][1+i],arr[1][1+i]);
    }
 }
Output :-












Curve Generation : Bezier for n control points ..

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

void bezier (int x[4], int y[4])
{

    int i;
    double t;

    for (t = 0.0; t < 1.0; t += 0.0005)
    {
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] +
   3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];

double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] +
   3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];

putpixel (xt, yt, WHITE);
    }

    for (i=0; i<4; i++)
putpixel (x[i], y[i], YELLOW);


    return;
}

void main()
{
    int x[4], y[4],x1[4], y1[4],x2[4], y2[4];
    int i,h;

int gd = DETECT, gm;
    initgraph (&gd, &gm, "..\\bgi");

    printf ("Enter the x- and y-coordinates of the four control points.\n");
    for (i=0; i<4; i++)
scanf ("%d%d", &x[i], &y[i]);
    bezier (x, y);

    for (i=0; i<4; i++)
scanf ("%d%d", &x1[i], &y1[i]);
bezier (x1, y1);

for (i=0; i<4; i++)
scanf ("%d%d", &x2[i], &y2[i]);
bezier (x2, y2);

    getch();
    closegraph();
}
Output :-


Implementation of Circle generation algorithm..

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

void plotPoints(int cx1, int cy1, int x1, int y1)
{
putpixel(cx1+x1, cy1+y1, RED);
putpixel(cx1-x1, cy1+y1, RED);
putpixel(cx1+x1, cy1-y1, RED);
putpixel(cx1-x1, cy1-y1, RED);
putpixel(cx1+y1, cy1+x1, RED);
putpixel(cx1-y1, cy1+x1, RED);
putpixel(cx1+y1, cy1-x1, RED);
putpixel(cx1-y1, cy1-x1, RED);
}

void plotPoints1(int cx2, int cy2, int x2, int y2)
{
putpixel(cx2-x2, cy2+y2, RED);
putpixel(cx2+x2, cy2-y2, RED);
putpixel(cx2+y2, cy2+x2, RED);
    putpixel(cx2-y2, cy2-x2, RED);
}

void main()
{
int cx1,cy1,r1,cx2,cy2,r2;
int x1 = 0,y1,p1;
int x2 = 0,y2,p2;
int gd = DETECT, gm;
clrscr();

initgraph(&gd, &gm, "c:\\tc\\bgi");
cleardevice();

printf("Enter the coordinates of centre of the outer circle: ");
scanf("%d %d", &cx1, &cy1);
printf("Enter radius of Outer : ");
scanf("%d", &r1);

printf("Enter the coordinates of centre of the Inner circle: ");
scanf("%d %d", &cx2, &cy2);
printf("Enter radius of Inner : ");
scanf("%d", &r2);


y1 = r1;
p1 = 3 - 2 * r1;

while (x1 < y1)
{
plotPoints(cx1, cy1, x1, y1);
x1++;
if (p1 < 0)
p1 = p1 + 4 * x1 + 6;
else
{
y1--;
p1 = p1 + 4 * (x1 - y1) + 10;
}
plotPoints(cx1, cy1, x1, y1);
delay(200);
}

y2 = r2;
p2 = 3 - 2 * r2;

while (x2 < y2)
{
plotPoints1(cx2, cy2, x2, y2);
x2++;
if (p2 < 0)
p2 = p2 + 4 * x2 + 6;
else
{
y2--;
p2 = p2 + 4 * (x2 - y2) + 10;
}
plotPoints1(cx2, cy2, x2, y2);
delay(200);
}

getch();
}
Output :-