0% found this document useful (0 votes)
54 views

Basic Graphics in C

The document describes drawing 2D and 3D transformations using matrix multiplication in C++. It includes: 1) Drawing a star and rectangle and applying translation by getting translation factors and creating a translation matrix. 2) Applying scaling by getting scaling factors, creating separate scaling matrices, and drawing the scaled shapes. 3) Applying rotation by getting an angle, converting to radians, creating a rotation matrix, and drawing the rotated shapes. 4) Drawing a prism in 3D, applying a 45 degree rotation matrix to rotate it, and applying another rotation matrix to rotate it further. Matrix multiplication is used to apply the transformations.

Uploaded by

Ananya Naithani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Basic Graphics in C

The document describes drawing 2D and 3D transformations using matrix multiplication in C++. It includes: 1) Drawing a star and rectangle and applying translation by getting translation factors and creating a translation matrix. 2) Applying scaling by getting scaling factors, creating separate scaling matrices, and drawing the scaled shapes. 3) Applying rotation by getting an angle, converting to radians, creating a rotation matrix, and drawing the rotated shapes. 4) Drawing a prism in 3D, applying a 45 degree rotation matrix to rotate it, and applying another rotation matrix to rotate it further. Matrix multiplication is used to apply the transformations.

Uploaded by

Ananya Naithani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Q5

2D Transformation translation, scaling, rotation

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
int c,i,j,k;
float star[3][8]={0,40,60,80,120,80,60,40,60,80,120,80,60,40,0,40,1,1,1,1,1,1,1,1};
float rect[3][4]={50,70,70,50,50,50,70,70,1,1,1,1};
do
{
printf("enter your choice\n");
printf("1. Translation\n2. Scaling\n3. Rotation\n4. Exit\n");
scanf("%d",&c);
if(c==1)
{
float a,b;
printf("enter the factor by which the object has to be translated in x and y direction\n");
scanf("%f %f",&a,&b);
float t[3][3]={1,0,a,0,1,b,0,0,1};
//Matrix multiplication
float tnew1[3][8],tnew2[3][4];
for(i=0;i<3;i++)
{
for(j=0;j<8;j++)
{ tnew1[i][j]=0;
for(k=0;k<3;k++)
{
tnew1[i][j]+= star[k][j]*t[i][k];
}

}
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{ tnew2[i][j]=0;
for(k=0;k<3;k++)
{
tnew2[i][j]+= rect[k][j]*t[i][k];
}
}
}
//to draw star
line(320+tnew1[0][0],240-tnew1[1][0],320+tnew1[0][1],240-tnew1[1][1]);
line(320+tnew1[0][1],240-tnew1[1][1],320+tnew1[0][2],240-tnew1[1][2]);
line(320+tnew1[0][2],240-tnew1[1][2],320+tnew1[0][3],240-tnew1[1][3]);
line(320+tnew1[0][3],240-tnew1[1][3],320+tnew1[0][4],240-tnew1[1][4]);
line(320+tnew1[0][4],240-tnew1[1][4],320+tnew1[0][5],240-tnew1[1][5]);
line(320+tnew1[0][5],240-tnew1[1][5],320+tnew1[0][6],240-tnew1[1][6]);
line(320+tnew1[0][6],240-tnew1[1][6],320+tnew1[0][7],240-tnew1[1][7]);
line(320+tnew1[0][7],240-tnew1[1][7],320+tnew1[0][0],240-tnew1[1][0]);
//to draw rect
line(320+tnew2[0][0],240-tnew2[1][0],320+tnew2[0][1],240-tnew2[1][1]);
line(320+tnew2[0][1],240-tnew2[1][1],320+tnew2[0][2],240-tnew2[1][2]);
line(320+tnew2[0][2],240-tnew2[1][2],320+tnew2[0][3],240-tnew2[1][3]);
line(320+tnew2[0][3],240-tnew2[1][3],320+tnew2[0][0],240-tnew2[1][0]);
}
else
if(c==2)
{
float sx1,sy1,sx2,sy2;
printf("for star: Sx and Sy\n");
scanf("%f %f",&sx1,&sy1);
printf("for rectange: Sx and Sy\n");
scanf("%f %f",&sx2,&sy2);
float scale1[3][3]={sx1,0,0,0,sy1,0,0,0,1};
float scale2[3][3]={sx2,0,0,0,sy2,0,0,0,1};
float snew1[3][8],snew2[3][4];
//Matrix multiplication
for(i=0;i<3;i++)
{
for(j=0;j<8;j++)
{ snew1[i][j]=0;
for(k=0;k<3;k++)
{
snew1[i][j]=snew1[i][j]+ star[k][j]*scale1[i][k];
}

}
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{ snew2[i][j]=0;
for(k=0;k<3;k++)
{
snew2[i][j]+= rect[k][j]*scale2[i][k];
}
}
}
//to draw star
line(320+snew1[0][0],240-snew1[1][0],320+snew1[0][1],240-snew1[1][1]);
line(320+snew1[0][1],240-snew1[1][1],320+snew1[0][2],240-snew1[1][2]);
line(320+snew1[0][2],240-snew1[1][2],320+snew1[0][3],240-snew1[1][3]);
line(320+snew1[0][3],240-snew1[1][3],320+snew1[0][4],240-snew1[1][4]);
line(320+snew1[0][4],240-snew1[1][4],320+snew1[0][5],240-snew1[1][5]);
line(320+snew1[0][5],240-snew1[1][5],320+snew1[0][6],240-snew1[1][6]);
line(320+snew1[0][6],240-snew1[1][6],320+snew1[0][7],240-snew1[1][7]);
line(320+snew1[0][7],240-snew1[1][7],320+snew1[0][0],240-snew1[1][0]);
//to draw rect
line(320+snew2[0][0],240-snew2[1][0],320+snew2[0][1],240-snew2[1][1]);
line(320+snew2[0][1],240-snew2[1][1],320+snew2[0][2],240-snew2[1][2]);
line(320+snew2[0][2],240-snew2[1][2],320+snew2[0][3],240-snew2[1][3]);
line(320+snew2[0][3],240-snew2[1][3],320+snew2[0][0],240-snew2[1][0]);
}
else
if(c==3)
{
float x1,x;
printf("enter the angle by which the object has to be rotated\n");
scanf("%f",&x);
x1=((3.14)*x)/(180);
float r[3][3]={cos(x1),(-1)*sin(x1),0,sin(x1),cos(x1),0,0,0,1};
float rnew1[3][8],rnew2[4][8];
//Matrix multiplication
for(i=0;i<3;i++)
{
for(j=0;j<8;j++)
{rnew1[i][j]=0;
for(k=0;k<3;k++)
{
rnew1[i][j]+= star[k][j]*r[i][k];
}
}
}
for(i=0;i<3;i++)

{
for(j=0;j<8;j++)
{ rnew2[i][j]=0;
for(k=0;k<3;k++)
{
rnew2[i][j]+= rect[k][j]*r[i][k];
}
}
}
//to draw star
line(320+rnew1[0][0],240-rnew1[1][0],320+rnew1[0][1],240-rnew1[1][1]);
line(320+rnew1[0][1],240-rnew1[1][1],320+rnew1[0][2],240-rnew1[1][2]);
line(320+rnew1[0][2],240-rnew1[1][2],320+rnew1[0][3],240-rnew1[1][3]);
line(320+rnew1[0][3],240-rnew1[1][3],320+rnew1[0][4],240-rnew1[1][4]);
line(320+rnew1[0][4],240-rnew1[1][4],320+rnew1[0][5],240-rnew1[1][5]);
line(320+rnew1[0][5],240-rnew1[1][5],320+rnew1[0][6],240-rnew1[1][6]);
line(320+rnew1[0][6],240-rnew1[1][6],320+rnew1[0][7],240-rnew1[1][7]);
line(320+rnew1[0][7],240-rnew1[1][7],320+rnew1[0][0],240-rnew1[1][0]);
//to draw rect
line(320+rnew2[0][0],240-rnew2[1][0],320+rnew2[0][1],240-rnew2[1][1]);
line(320+rnew2[0][1],240-rnew2[1][1],320+rnew2[0][2],240-rnew2[1][2]);
line(320+rnew2[0][2],240-rnew2[1][2],320+rnew2[0][3],240-rnew2[1][3]);
line(320+rnew2[0][3],240-rnew2[1][3],320+rnew2[0][0],240-rnew2[1][0]);
}
}while(c!=4);
/* clean up */
getch();
closegraph();
return 0;

Q6

3D Translation scaling, translation , rotation

#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,i,j,k;
float prism[4][6]={10,70,40,10,70,40,10,10,70,10,10,70,0,0,0,60,60,60,1,1,1,1,1,1};
float rot[4][4]={1,0,0,0,0,0.707,-0.707,0,0,0.707,0.707,0,0,0,0,1};
float sum, A[4][6], B[4][6];
float rot2[4][4]={1,0,0,0,0,0.707,0.707,0,0,-0.707,0.707,0,0,0,0,1};
/* struct point
{
float x[6];
float y[6];
float z[6];
}p1;*/
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/*setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();
*/
/* draw a diagonal line */
line(320+prism[0][0],240-prism[1][0],320+prism[0][1],240-prism[1][1]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][4],240-prism[1][4],320+prism[0][5],240-prism[1][5]);

line(320+prism[0][3],240-prism[1][3],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][2],240-prism[1][2],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][3],240-prism[1][3]);
clrscr();
//matrix multiplication//
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot[i][j]*prism[j][k];
}
A[i][k]=sum;
}
}
line(320+A[0][0],240-A[1][0],320+A[0][1],240-A[1][1]);
line(320+A[0][1],240-A[1][1],320+A[0][2],240-A[1][2]);
line(320+A[0][0],240-A[1][0],320+A[0][2],240-A[1][2]);
line(320+A[0][3],240-A[1][3],320+A[0][4],240-A[1][4]);
line(320+A[0][4],240-A[1][4],320+A[0][5],240-A[1][5]);
line(320+A[0][3],240-A[1][3],320+A[0][5],240-A[1][5]);
line(320+A[0][2],240-A[1][2],320+A[0][5],240-A[1][5]);
line(320+A[0][1],240-A[1][1],320+A[0][4],240-A[1][4]);
line(320+A[0][0],240-A[1][0],320+A[0][3],240-A[1][3]);
/* printf("the initial matrix is=\n");
for(i=0;i<4; i++)
{
for(j=0; j<6; j++)
{
printf("%0.2f\t", prism[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("the final matrix is(anti-clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", A[i][k]);
}
printf("\n");
}
printf("\n\n");*/

for(i=0; i<4; i++)


{
for(k=0; k<6; k++)
{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot2[i][j]*A[j][k];
}
B[i][k]=sum;
}
}
/* printf("the final matrix is(clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", B[i][k]);
}
printf("\n");
}
*/
/* clean up */
getch();
closegraph();
return 0;
}

Q1

Draw a line using bresenham approach

#include<conio.h>
#include<math.h>
#include<stdio.h>
#include<graphics.h>
#include<process.h>
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "C:\\turboc3\\bgi");

/* read result of initialization */


errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

cleardevice();

int x,y,x1,x2,y1,y2,d,dx,dy;
printf("Enter coordinates: ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x=x1;y=y1;
dx=x2-x1;dy=y2-y1;
putpixel(320+x,240-y,3);
if(abs(dx)<abs(dy)&&abs(dx)!=0)
{
d=dy-(2*dx);
while(y<=y2)
{
if(d<0)
d+=2*dx;
else
{
d+=2*(dx-dy);
x++;
}
y++;
putpixel(320+x,240-y,3);
}
}
else if(abs(dx)>abs(dy)&&abs(dy)!=0)
{
d=(2*dy)-dx;
while(x<=x2)
{
if(d<0)

d+=2*dy;
else
{
d+=2*(dy-dx);
y++;
}
x++;
putpixel(320+x,240-y,3);
}
}
else if(abs(dx)==abs(dy))
{
while(x<=x2&&y<=y2)
{
putpixel(320+x,240-y,3);
x++;y++;
}
}
else if(abs(dx)==0)
{
for(y=y1;y<=y2;y++)
putpixel(320+x,240-y,3);
}
else if(abs(dy)==0)
{
for(x=x1;x<=x2;x++)
putpixel(320+x,240-y,3);
}

getch();
}

Q2

Draw a circle using mid-point first order.

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\turboc3\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
int x,y,r,h;
cleardevice();
printf("Enter radius: ");
scanf("%d",&r);
x=r;y=0;h=1-r;
putpixel(320+x,240-y,3);
putpixel(320+y,240-x,3);
putpixel(320+y,240+x,3);
putpixel(320+x,240+y,3);
putpixel(320-x,240-y,3);
putpixel(320-y,240-x,3);
putpixel(320-x,240+y,3);
putpixel(320-y,240+x,3);
while(y<=x)
{
if(h<=0)
h+=(2*y)+3;
else
{
h+=2*(y-x)+5;
x--;

}
y++;
putpixel(320+x,240-y,3);
putpixel(320+y,240-x,3);
putpixel(320+y,240+x,3);
putpixel(320+x,240+y,3);
putpixel(320-x,240-y,3);
putpixel(320-y,240-x,3);
putpixel(320-x,240+y,3);
putpixel(320-y,240+x,3);
}
getch();
}

Q3

Draw line clipping using Cyrus-Beck.

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
struct point
{
float x,y;
};
void clip(point pol[10], point p1, point p2, int n)
{
cleardevice();
setcolor(YELLOW);
for(int i=0;i<n;i++)
{
line(pol[i].x,pol[i].y,pol[i+1].x,pol[i+1].y);
}
setcolor(WHITE);
line(p1.x,p1.y,p2.x,p2.y);
getch();
float t_enter=0,t_leave=1;
for(i=0;i<n;i++)
{
point n,pei;
pei=pol[i];
n.x=(pol[i+1].y-pol[i].y);
n.y=(pol[i+1].x-pol[i].x);
float num,den;
num = n.x*(pei.x-p1.x) - n.y*(pei.y-p1.y);
den = n.x*(p2.x-p1.x) + n.y*(p1.y-p2.y);
float t;
if(den!=0)
t= num*1.0/den;
if(t>=0 && t<=1)
{
if(den<0)
{
if(t>t_enter)
t_enter = t;
}
else if(den>0)
{
if(t<t_leave)

t_leave = t;
}
}
}
point pi,pl;
pi.x=p1.x+(p2.x-p1.x)*t_enter;
pi.y=p1.y+(p2.y-p1.y)*t_enter;
pl.x=p1.x+(p2.x-p1.x)*t_leave;
pl.y=p1.y+(p2.y-p1.y)*t_leave;
setcolor(GREEN);
line(pi.x,pi.y,pl.x,pl.y);
}
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the no. of vertices of clipping window :\n ");
int n;
scanf("%d",&n);
point pol[10];
printf("Enter the vertices in clockwise order \n");
for(int i=0;i<n;i++)
{
printf(" Enter vertex %d :\n ",i+1);
scanf("%f %f",&pol[i].x,&pol[i].y);
pol[i].x+=320;
pol[i].y=240-pol[i].y;
}
pol[i].x=pol[0].x;
pol[i].y=pol[0].y;
printf("Enter the start and end points of the line :\n ");
point p1,p2;
scanf("%f %f %f %f",&p1.x,&p1.y,&p2.x,&p2.y);
int t;
if(p1.x>p2.x)
{
t=p1.x;
p1.x=p2.x;
p2.x=t;
t=p1.y;
p1.y=p2.y;
p2.y=t;

}
p1.x+=320;p2.x+=320;
p1.y=240-p1.y;
p2.y=240-p2.y;
clip(pol,p1,p2,n);
getch();
closegraph();
}

Q7

Isometric Projections

#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,i,j,k;
float prism[4][6]={10,70,40,10,70,40,10,10,70,10,10,70,0,0,0,60,60,60,1,1,1,1,1,1};
// float rot[4][4]={1,0,0,0,0,0.707,-0.707,0,0,0.707,0.707,0,0,0,0,1};
float sum, A[4][6], B[4][6];
float rot1[4][4]={0.707,0,0.707,0,0.408,0.816,-0.408,0,0,0,0,0,0,0,0,1};
// float rot2[4][4]={1,0,0,0,0,0.707,0.707,0,0,-0.707,0.707,0,0,0,0,1};
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/* draw a diagonal line */
line(320+prism[0][0],240-prism[1][0],320+prism[0][1],240-prism[1][1]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][4],240-prism[1][4],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][2],240-prism[1][2],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][3],240-prism[1][3]);
cleardevice();
//matrix multiplication//
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)

{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot1[i][j]*prism[j][k];
}
A[i][k]=sum;
}
}
line(320+A[0][0],240-A[1][0],320+A[0][1],240-A[1][1]);
line(320+A[0][1],240-A[1][1],320+A[0][2],240-A[1][2]);
line(320+A[0][0],240-A[1][0],320+A[0][2],240-A[1][2]);
line(320+A[0][3],240-A[1][3],320+A[0][4],240-A[1][4]);
line(320+A[0][4],240-A[1][4],320+A[0][5],240-A[1][5]);
line(320+A[0][3],240-A[1][3],320+A[0][5],240-A[1][5]);
line(320+A[0][2],240-A[1][2],320+A[0][5],240-A[1][5]);
line(320+A[0][1],240-A[1][1],320+A[0][4],240-A[1][4]);
line(320+A[0][0],240-A[1][0],320+A[0][3],240-A[1][3]);
/* printf("the initial matrix is=\n");
for(i=0;i<4; i++)
{
for(j=0; j<6; j++)
{
printf("%0.2f\t", prism[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("the final matrix is(anti-clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", A[i][k]);
}
printf("\n");
}
printf("\n\n");*/
/*
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot2[i][j]*A[j][k];
}

B[i][k]=sum;
}
}
/* printf("the final matrix is(clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", B[i][k]);
}
printf("\n");
}
*/

/* clean up */
getch();
closegraph();
return 0;
}

Q8

Oblique Projections

#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,i,j,k;
float a=2,b=2,c=2;
float prism[4][6]={10,70,40,10,70,40,10,10,70,10,10,70,0,0,0,60,60,60,1,1,1,1,1,1};
// float rot[4][4]={1,0,0,0,0,0.707,-0.707,0,0,0.707,0.707,0,0,0,0,1};
float sum, A[4][6], B[4][6];
float rot1[4][4]={1,0,-a/c,0,0,1,-b/c,0,0,0,0,0,0,0,0,1};
// float rot2[4][4]={1,0,0,0,0,0.707,0.707,0,0,-0.707,0.707,0,0,0,0,1};
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/* draw a diagonal line */
line(320+prism[0][0],240-prism[1][0],320+prism[0][1],240-prism[1][1]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][4],240-prism[1][4],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][2],240-prism[1][2],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][3],240-prism[1][3]);
cleardevice();
//matrix multiplication//
for(i=0; i<4; i++)
{

for(k=0; k<6; k++)


{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot1[i][j]*prism[j][k];
}
A[i][k]=sum;
}
}
line(320+A[0][0],240-A[1][0],320+A[0][1],240-A[1][1]);
line(320+A[0][1],240-A[1][1],320+A[0][2],240-A[1][2]);
line(320+A[0][0],240-A[1][0],320+A[0][2],240-A[1][2]);
line(320+A[0][3],240-A[1][3],320+A[0][4],240-A[1][4]);
line(320+A[0][4],240-A[1][4],320+A[0][5],240-A[1][5]);
line(320+A[0][3],240-A[1][3],320+A[0][5],240-A[1][5]);
line(320+A[0][2],240-A[1][2],320+A[0][5],240-A[1][5]);
line(320+A[0][1],240-A[1][1],320+A[0][4],240-A[1][4]);
line(320+A[0][0],240-A[1][0],320+A[0][3],240-A[1][3]);
/* printf("the initial matrix is=\n");
for(i=0;i<4; i++)
{
for(j=0; j<6; j++)
{
printf("%0.2f\t", prism[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("the final matrix is(anti-clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", A[i][k]);
}
printf("\n");
}
printf("\n\n");*/
/*
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot2[i][j]*A[j][k];

}
B[i][k]=sum;
}
}
/* printf("the final matrix is(clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", B[i][k]);
}
printf("\n");
}
*/

/* clean up */
getch();
closegraph();
return 0;
}

Q9

Perspective Projection

#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,i,j,k,d=1;
float prism[4][6]={10,70,40,10,70,40,10,10,70,10,10,70,0,0,0,60,60,60,1,1,1,1,1,1};
// float rot[4][4]={1,0,0,0,0,0.707,-0.707,0,0,0.707,0.707,0,0,0,0,1};
float sum, A[4][6], B[4][6];
float rot1[4][4]={d,0,0,0,0,d,0,0,0,0,0,0,0,0,1,d};
// float rot1[4][4]={0.707,0,0.707,0,0.408,0.816,-0.408,0,0,0,0,0,0,0,0,1};
// float rot2[4][4]={1,0,0,0,0,0.707,0.707,0,0,-0.707,0.707,0,0,0,0,1};
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/* draw a diagonal line */
line(320+prism[0][0],240-prism[1][0],320+prism[0][1],240-prism[1][1]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][4],240-prism[1][4],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][2],240-prism[1][2],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][3],240-prism[1][3]);
cleardevice();
//matrix multiplication//
for(i=0; i<4; i++)
{

for(k=0; k<6; k++)


{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot1[i][j]*prism[j][k];
}
A[i][k]=sum/(A[3][k]+d);
}
}
line(320+A[0][0],240-A[1][0],320+A[0][1],240-A[1][1]);
line(320+A[0][1],240-A[1][1],320+A[0][2],240-A[1][2]);
line(320+A[0][0],240-A[1][0],320+A[0][2],240-A[1][2]);
line(320+A[0][3],240-A[1][3],320+A[0][4],240-A[1][4]);
line(320+A[0][4],240-A[1][4],320+A[0][5],240-A[1][5]);
line(320+A[0][3],240-A[1][3],320+A[0][5],240-A[1][5]);
line(320+A[0][2],240-A[1][2],320+A[0][5],240-A[1][5]);
line(320+A[0][1],240-A[1][1],320+A[0][4],240-A[1][4]);
line(320+A[0][0],240-A[1][0],320+A[0][3],240-A[1][3]);
/* printf("the initial matrix is=\n");
for(i=0;i<4; i++)
{
for(j=0; j<6; j++)
{
printf("%0.2f\t", prism[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("the final matrix is(anti-clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", A[i][k]);
}
printf("\n");
}
printf("\n\n");*/
/*
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot2[i][j]*A[j][k];

}
B[i][k]=sum;
}
}
/* printf("the final matrix is(clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", B[i][k]);
}
printf("\n");
}
*/

/* clean up */
getch();
closegraph();
return 0;
}

Q4

Polygon Clipping

#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,xmin,ymin,i,j;
float a[10],b[10],c[10],D[10],e[10],f[10],g[10],h[10],k[10],l[10];
float x1,x2,y1,y2,dx,dy,X,Y,d,t;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

/* RECTANGULAR WINDOW */
printf("Enter the maximum value of co-ordinates\n");
scanf("%d %d",&xmax,&ymax);
printf("Enter the minimum value of co-ordinates\n");
scanf("%d %d",&xmin,&ymin);
line(320+xmin,240-ymin,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmax,240-ymax);
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
printf("Enter the co-ordinates of polygon in clockwise dirn starting from left\n");
for(i=1;i<=5;i++)
{
scanf("%f %f",&a[i],&b[i]);
}

/*Sutherland Hodgman Approach */

/* Left edge*/
j=1;
for(i=1;i<=4;i++)
{
if(a[i+1]>xmin)
{
if(a[i]>xmin)
{
c[j]=a[i+1];
D[j]=b[i+1];
}
else
{
t=-(a[i]-xmin)/(a[i+1]-a[i]);
c[j]=a[i]+(a[i+1]-a[i])*t;
D[j]=b[i]+(b[i+1]-b[i])*t;
j=j+1;
c[j]=a[1+i];
D[j]=b[1+i];
}
}
else
{
if(a[i]>xmin)
{
t=-(a[i]-xmin)/(a[i+1]-a[i]);
c[j]=a[i]+(a[i+1]-a[i])*t;
D[j]=b[i]+(b[i+1]-b[i])*t;
}

}
j++;
}
/* Top edge */
c[6]=c[1];
D[6]=D[1];
j=1;
for(i=1;i<=5;i++)
{
if(D[i+1]<ymax)
{
if(D[i]<ymax)
{
e[j]=c[i+1];

f[j]=D[i+1];
}
else
{
t=(D[i]-ymax)/(-D[i+1]+D[i]);
e[j]=c[i]+(c[i+1]-c[i])*t;
f[j]=D[i]+(D[i+1]-D[i])*t;
j=j+1;
e[j]=c[1+i];
f[j]=D[1+i];
}
}
else
{
if(D[i]<ymax)
{
t=(D[i]-ymax)/(-D[i+1]+D[i]);
e[j]=c[i]+(c[i+1]-c[i])*t;
f[j]=D[i]+(D[i+1]-D[i])*t;
}

}
j++;
}
/* Right edge*/
e[7]=e[1];
f[7]=f[1];
j=1;
for(i=1;i<=6;i++)
{
if(e[i+1]<xmax)
{
if(e[i]<xmax)
{
g[j]=e[i+1];
h[j]=f[i+1];
}
else
{
t=(e[i]-xmax)/(-e[i+1]+e[i]);
g[j]=e[i]+(e[i+1]-e[i])*t;
h[j]=f[i]+(f[i+1]-f[i])*t;
j=j+1;
g[j]=e[1+i];

h[j]=f[1+i];
}
}
else
{
if(e[i]<xmax)
{
t=(e[i]-xmax)/(-e[i+1]+e[i]);
g[j]=e[i]+(e[i+1]-e[i])*t;
h[j]=f[i]+(f[i+1]-f[i])*t;
}

}
j++;
}
/* Bottom Edge*/
g[8]=g[1];
h[8]=h[1];
j=1;
for(i=1;i<=7;i++)
{
if(h[i+1]>ymin)
{
if(h[i]>ymin)
{
k[j]=g[i+1];
l[j]=h[i+1];
}
else
{
t=-(h[i]-ymin)/(h[i+1]-h[i]);
k[j]=g[i]+(g[i+1]-g[i])*t;
l[j]=h[i]+(h[i+1]-h[i])*t;
j=j+1;
k[j]=e[1+i];
l[j]=f[1+i];
}
}
else
{
if(h[i]>ymin)
{
t=-(h[i]-ymin)/(h[i+1]-h[i]);
k[j]=g[i]+(g[i+1]-g[i])*t;

l[j]=h[i]+(h[i+1]-h[i])*t;
}

}
j++;
}
k[9]=k[1];
l[9]=l[1];
// Printing the final co-ordinates
/* for(i=1;i<=9;i++)
{
printf("%f\t %f\n",k[i],l[i]);
}
*/
for(i=1;i<=8;i++)
{
line(320+k[i],240-l[i],320+k[i+1],240-l[i+1]);
}

/* clean up */
getch();
closegraph();
}

Q10 Curves
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
struct point
{
int x;
int y;
int z;
}P,P0,P1,dP0,dP1,a,b,c,d,e,f,g;
float t;
void drawcurve(point P0,point P1,point dP0,point dP1)
{
for(t=0.0;t<=1.0;t+=0.0001)
{
P.z=(2*(t*t*t)-3*(t*t)+1)*P0.z+(-2*(t*t*t)+3*t*t)*P1.z+(t*t*t-2*t*t+t)*dP0.z+(t*t*t-t*t)*dP1.z;
P.x=(2*(t*t*t)-3*(t*t)+1)*P0.x+(-2*(t*t*t)+3*t*t)*P1.x+(t*t*t-2*t*t+t)*dP0.x+(t*t*t-t*t)*dP1.x;
P.y=(2*(t*t*t)-3*(t*t)+1)*P0.y+(-2*(t*t*t)+3*t*t)*P1.y+(t*t*t-2*t*t+t)*dP0.y+(t*t*t-t*t)*dP1.y;
putpixel(320+P.x,240-P.y,3);
}
}
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
//int i,xmax, ymax;
int poly[8];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\turboc3\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
a.x=50;
a.y=60;
a.z=0;

b.x=55;
b.y=35;
b.z=0;
c.x=50;
c.y=50;
c.z=0;
d.x=-40;
d.y=-30;
d.z=0;
e.x=20;
e.y=20;
e.z=0;
f.x=40;
f.y=-100;
f.z=0;
g.x=-50;
g.y=20;
g.z=0;
drawcurve(a,b,c,d);
drawcurve(b,e,f,g);
getch();
cleardevice();
}

You might also like