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

Graphics Lab Report

This document contains C code for performing 2D transformations on shapes (a star and rectangle) using translation, scaling, and rotation matrices. It allows the user to choose a transformation type and input parameters. Matrix multiplication is used to apply the transformations to the x and y coordinate arrays of the shapes. The transformed shapes are then redrawn on the screen.

Uploaded by

Anoop Jena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Graphics Lab Report

This document contains C code for performing 2D transformations on shapes (a star and rectangle) using translation, scaling, and rotation matrices. It allows the user to choose a transformation type and input parameters. Matrix multiplication is used to apply the transformations to the x and y coordinate arrays of the shapes. The transformed shapes are then redrawn on the screen.

Uploaded by

Anoop Jena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

2d TRANSFORMATION

#include
#include
#include
#include
#include

<graphics.h>
<stdlib.h>
<stdio.h>
<conio.h>
<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;
}

ELLIPSE
#include
#include
#include
#include

<graphics.h>
<stdlib.h>
<stdio.h>
<conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\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);
}
/*setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy(); */
/* draw a diagonal line
line(0, 0, xmax, ymax); */
float x,y,a,b,d;
printf("enter the value of a and b");
scanf("%f %f",&a,&b);
//a=100,b=75;
x=0;
y=b;
d=0.25*a*a+b*b-a*a*b;

putpixel(320+x,240-y,15);
while(2*b*b*(x+1)<2*a*a*(y-0.5))
{
if(d<=0)
d+=b*b*(2*x+3);
else
{
d+=b*b*(2*x+3)+2*a*a*(1-y);
y--;
}
x++;
putpixel(320+x,240-y,15);
putpixel(320+x,240+y,15);
putpixel(320-x,240-y,15);
putpixel(320-x,240+y,15);
}
d=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
while(y>=0)
{
if(d<=0)
{
d+=a*a*(3-2*y)+2*b*b*(x+1);
x++;
}
else
d+=a*a*(3-2*y);
y--;
putpixel(320+x,240-y,15);
putpixel(320+x,240+y,15);
putpixel(320-x,240-y,15);
putpixel(320-x,240+y,15);
}
/* clean up */
getch();
closegraph();
return 0;
}

SEEDFILL
#include
#include
#include
#include

<graphics.h>
<stdlib.h>
<stdio.h>
<conio.h>

/* void drawline(int , int ,int,int);*/


void seedfill(int x,int y,int a, int b)
{
if( getpixel(x,y)!=a && getpixel(x,y)!=b )
{
putpixel(x,y,b);
seedfill(x+1,y,a,b);
seedfill(x,y+1,a,b);
seedfill(x-1,y,a,b);
seedfill(x,y-1,a,b);
}
}
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
// int xmax, ymax;
int p,px1[30],px2[30],py1[30],py2[30],xs,ys,bcolor=15,fcolor=12,j;
p=0;
int a[5],b[5],i;
/* 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 */
// LOOP TO DRAW POLYGON
printf("enter the number of sides of the polygon\n");
scanf("%d",&p);
printf("enter the co-ordinates\n");
/*
for(i=0;i<=p;i++)
{
scanf("%d",&a[i],&b[i]);
}
for(i=0;i<p;i++)
{
line(a[i],b[i],a[i+1],b[i+1]);
}*/
//printf("enter the color of the edges\n");
//scanf("%d",&bcolor);
for(j=0;j<p;j++)
{
printf("for line %d",j+1);
scanf("%d %d %d %d",&px1[j],&py1[j],&px2[j],&py2[j]);
line(320+px1[j],240-py1[j],320+px2[j],240-py2[j]);
}
//POLYGON FILLING BY SEEDFILL
/* printf("enter the color to be filled\n");
scanf("%d",&fcolor);*/
/*xs=50;
ys=50;*/
printf("Enter the coordinates that lie inside the polygon\n");
scanf("%d%d",&xs,&ys);
seedfill(xs,ys,15,12);
/* clean up */
getch();
closegraph();
return 0;
}
//seedfill program//
//program to print a line
/*void drawline(int x1, int y1, int x2, int y2)
{

int i=0;
int dx,dy,X=0,Y=0,d;
if(y2>y1&&x2>x1)
{
dx=x2-x1;
dy=y2-y1;
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(320+x1,240-i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(320+i,240-y1,15);
}
else if(abs(dy)>abs(dx) && abs(dx)!=0)
{
X=x1;
Y=y1;
d=dy-2*dx;
putpixel(320+X,240-Y,15);
while(Y<=y2)
{
if(d>=0)
d+=-2*dx;
else
{
d+=2*dy-2*dx;
X++;
}
Y++;
putpixel(320+X,240-Y,15);
}
}
else if(abs(dx)>abs(dy) && abs(dy)!=0)
{
X=x1;
Y=y1;
d=2*dy-dx;
putpixel(320+X,240-Y,15);
while(X<=x2)
{
if(d<=0)
d+=2*dy;
else

{
d+=2*dy-2*dx;
Y++;
}
X++;
putpixel(320+X,240-Y,15);
}
}
else
{
X=x1;
Y=y1;
putpixel(320+X,240-Y,15);
while(X<=x2)
{
X++;
Y++;
putpixel(320+X,240-Y,15);
}
}
}*/
/*negative*/
/* else
{
dx=x2-x1;
dy=y2-y1;
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(320+x1,240-i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(320+i,240-y1,15);
}
else if(abs(dy)>abs(dx) && abs(dx)!=0)
{
X=x1;
Y=y1;
d=dy-2*dx;
putpixel(320+X,240-Y,15);
while(Y>=y2)
{
if(d<0)
d+=2*dx;

else
{
d+=2*(dy+dx);
X++;
}
Y--;
putpixel(320+X,240-Y,15);
}
}
else if(abs(dx)>abs(dy) && abs(dy)!=0)
{
X=x1;
Y=y1;
d=2*dy+dx;
putpixel(320+X,240-Y,15);
while(X<=x2)
{
if(d>0)
d+=2*dy;
else
{
d+=2*dy+2*dx;
Y--;
}
X++;
putpixel(320+X,240-Y,15);
}
}
else
{
X=x1;
Y=y1;
putpixel(320+X,240-Y,15);
while(X<=x2)
{
X++;
Y++;
putpixel(320+X,240-Y,15);
}

3D TRANSFORMATION
#include
#include
#include
#include

<math.h>
<graphics.h>
<stdlib.h>
<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;
}

LINE DDA METHOD


LINE DRAWING DDA

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void put_pixel(int x, int y, int col)
{
putpixel(x+320, 240-y, col);
}
int round(float x)
{
double rem = fmod((double)x,1.0);
if(x<0.5)
return (floor((double)x));
else
return (ceil((double)x));
}
void dda(int x1, int y1, int x2, int y2)
{
int xa,ya,xb,yb;
setcolor(RED);
line(320,0,320,480);
setcolor(BLUE);
line(0,240,640,240);
setcolor(WHITE);
if(x1<x2)
{
xa=x1;ya=y1;
xb=x2;yb=y2;
}
else
{
xa=x2;ya=y2;
xb=x1;yb=y1;
}
int dx,dy;
dx=xb-xa;
dy=yb-ya;
int steps;
float x=xa,y=ya;

if (abs(dx)>abs(dy))
steps = abs(dx);
else
steps = abs(dy);
float xinc,yinc;
xinc = 1.0*dx/steps;
yinc = 1.0*dy/steps;
put_pixel(xa,ya,15);
while(x<xb)
{
x+=xinc;
y+=yinc;
put_pixel(round(x),round(y),15);
}
}
void main()
{
clrscr();
int x1,y1,x2,y2;
cout<<"Enter x1,y1 : ";
cin>>x1>>y1;
cout<<"Enter x2,y2 : ";
cin>>x2>>y2;
int gd = DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
dda(x1,y1,x2,y2);
getch();
closegraph();
}

OUTPUT

LINE DRAWING - MID POINT


#include<conio.h>
#include<iostream.h>
#include<graphics.h>
void put_pixel(int x, int y, int col)
{
putpixel(x+320, 240-y, col);
}
void mid_pt(int x1, int y1, int x2, int y2)
{
int d;
float m;
setcolor(RED);
line(320,0,320,480);
setcolor(BLUE);
line(0,240,640,240);
setcolor(WHITE);
if(x2!=x1)
m=1.0*(y2-y1)/(x2-x1);
int xa,ya,xb,yb;
if(x1<x2)
{
xa=x1;ya=y1;
xb=x2;yb=y2;
}
else
{
xa=x2;ya=y2;
xb=x1;yb=y1;
}
int dx = xb-xa;
int dy = yb-ya;
float x = xa, y = ya;
if( m>=0 && m<=1 )
{
put_pixel(xa,ya,15);
d = 2.0*dy-dx;

while(x<xb)
{
if(d>0)
{
//case ne
d+=dy-dx;
x++;
y++;
}
else
{
d+= dy;
x++;
}
put_pixel(x,y,15);
}
}
else if(m>-1 && m<0)
{
put_pixel(xa,ya,15);
d = 2.0*dy+dx;
while(x<xb)
{
if(d>0)
{
//case e
d+=dy;
x++;
}
else
{
d+= dy+dx;
x++; y--;
}
put_pixel(x,y,15);
}
}
else if(m>1)
{
put_pixel(xa,ya,15);
d = dy - 2.0*dx;
while(x<xb)
{
if(d>0)
{

//case n
d-= dx;
y++;
}
else
{
d+= dy-dx;
x++; y++;
}
put_pixel(x,y,15);
}
}
else if(m<-1)
{
put_pixel(xa,ya,15);
d = dy + 2.0*dx;
while(x<xb)
{
if(d>0)
{
//case se
d+=dy+dx;
x++;y--;
}
else
{
d+= dx;
y--;
}
put_pixel(x,y,15);
}
}
}
void main()
{
clrscr();
int x1,y1,x2,y2;
cout<<"Enter x1,y1 : ";
cin>>x1>>y1;
cout<<"Enter x2,y2 : ";
cin>>x2>>y2;
int gd = DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");

mid_pt(x1,y1,x2,y2);
getch();
closegraph();
}

PARABOLA
PARABOLA BRESENHAMS APPROACH
#include<iostream.h>
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void plotpixel(int x, int y)
{
putpixel((x+300),(240-y),15);
// putpixel((-x+300),(240-y),15);
putpixel((x+300),(240+y),15);
// putpixel((-x+300),(240+y),15);
}
void para(int cx, int cy, double a)
{
setcolor(BLUE);
line(300,0,300,479);

setcolor(RED);
line(0,240,639,240);
double x=0,y=0; /* initial coorodinates */
double d1 = 1 - (2*a);
plotpixel(x,y);
while( y<= (2*a) )
{
if(d1>0)
{
d1+= 3 + 2*y - 4*a;
plotpixel(x,y);
x++;
y++;
}
else
{
d1+= 3 + 2*y;
plotpixel(x,y);
y++;
}
//plotpixel(x,y);
}
d1 = ((y+0.5)*(y+0.5) - 4*a*(x+1));
while( y < 220 )
{
if(d1>0)
{
d1+= (-4*a);
x++;
}
else
{
d1+= 2 + 2*y - 4*a;
x++;
y++;
}
plotpixel(x,y);
}
}
void main()

{
clrscr();
double a;
cout<<"Enter a : ";
cin>>a;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
para(0,0,a);
getch();
closegraph();
}

OUTPUT

LINE CLIPPING
#include
#include
#include
#include
#include

<graphics.h>
<stdlib.h>
<math.h>
<stdio.h>
<conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
// int xmax, ymax;
/* 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(0, 0, xmax, ymax);
int xmin,ymin,xmax,ymax,xw1,xw2,yw1,yw2;
printf("Enter xmin and ymin\n");
scanf("%d%d",&xmin,&ymin);
printf("Enter xmax and ymax\n");
scanf("%d%d",&xmax,&ymax);
int x1,y1,x2,y2,dx,dy,d,x,y,i,temp,temp1;
printf("Enter the start coordinates of line");
scanf("%d%d",&xw1,&yw1);
printf("Enter the end coordinates of line");
scanf("%d%d",&xw2,&yw2);
// clrscr();
/* float tL,tB,tR,tT;
tL=(xmin-xw1)/(xw2-xw1);

tB=(ymin-yw1)/(yw2-yw1);
tR=(xw1-xmax)/(xw1-xw2);
tT=(yw1-ymax)/(yw1-yw2);
printf("%f\n",tl); */
// printf("%f",1/14);
x1=xmin;
y1=ymin;
x2=xmax;
y2=ymin;
if(x2<x1 && y2<y1)
{
temp=x2;
x2=x1;
x1=temp;
temp1=y2;
y2=y1;
y1=temp1;
}
// clrscr();
dx=x2-x1;
dy=y2-y1;
// printf("%d %d",dx,dy);
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(x1+320,240-i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(i+320,240-y1,15);
}
else if(abs(dy)>abs(dx))
{
d=dy-2*dx;
x=x1;
y=y1;
putpixel(x+320,240-y,15);
while(y<=y2)

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

x1=xmax;
y1=ymin;
x2=xmax;
y2=ymax;
if(x2<x1 && y2<y1)
{
temp=x2;
x2=x1;
x1=temp;
temp1=y2;
y2=y1;
y1=temp1;
}

// clrscr();
dx=x2-x1;
dy=y2-y1;
// printf("%d %d",dx,dy);
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(x1+320,240-i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(i+320,240-y1,15);
}
else if(abs(dy)>abs(dx))
{
d=dy-2*dx;
x=x1;
y=y1;
putpixel(x+320,240-y,15);
while(y<=y2)
{
if(d<0)
d-=2*dx;
else
{
d+=dy-2*dx;
x++;
}
y++;
putpixel(x+320,240-y,15);
}
}
else if(abs(dy)<=abs(dx))
{
d=2*dy-dx;
x=x1;
y=y1;
putpixel(x+320,240-y,15);
while(x<=x2)
{
if(d<0)
d+=2*dy;
else
{

d+=2*(dy-dx);
y++;
}
x++;
putpixel(x+320,240-y,15);
}
}

x2=xmax;
y2=ymax;
x1=xmin;
y1=ymax;
if(x2<x1 && y2<y1)
{
temp=x2;
x2=x1;
x1=temp;
temp1=y2;
y2=y1;
y1=temp1;
}
// clrscr();
dx=x2-x1;
dy=y2-y1;
// printf("%d %d",dx,dy);
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(x1+320,240-i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(i+320,240-y1,15);
}
else if(abs(dy)>abs(dx))
{
d=dy-2*dx;
x=x1;
y=y1;
putpixel(x+320,240-y,15);

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

x2=xmin;
y2=ymax;
x1=xmin;
y1=ymin;
if(x2<x1 && y2<y1)
{
temp=x2;
x2=x1;
x1=temp;
temp1=y2;
y2=y1;

y1=temp1;
}
// clrscr();
dx=x2-x1;
dy=y2-y1;
//printf("%d %d",dx,dy);
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(x1+320,240-i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(i+320,240-y1,15);
}
else if(abs(dy)>abs(dx))
{
d=dy-2*dx;
x=x1;
y=y1;
putpixel(x+320,240-y,15);
while(y<=y2)
{
if(d<0)
d-=2*dx;
else
{
d+=dy-2*dx;
x++;
}
y++;
putpixel(x+320,240-y,15);
}
}
else if(abs(dy)<=abs(dx))
{
d=2*dy-dx;
x=x1;
y=y1;
putpixel(x+320,240-y,15);
while(x<=x2)
{
if(d<0)
d+=2*dy;

else
{
d+=2*(dy-dx);
y++;
}
x++;
putpixel(x+320,240-y,15);
}
}

x1=xw1;
y1=yw1;
x2=xw2;
y2=yw2;
if(x2<x1 && y2<y1)
{
temp=x2;
x2=x1;
x1=temp;
temp1=y2;
y2=y1;
y1=temp1;
}
// clrscr();
dx=x2-x1;
dy=y2-y1;
// printf("%d %d",dx,dy);
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(x1+320,240-i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(i+320,240-y1,15);
}
else if(abs(dy)>abs(dx))
{
d=dy-2*dx;

x=x1;
y=y1;
putpixel(x+320,240-y,15);
while(y<=y2)
{
if(d<0)
d-=2*dx;
else
{
d+=dy-2*dx;
x++;
}
y++;
putpixel(x+320,240-y,15);
}
}
else if(abs(dy)<=abs(dx))
{
d=2*dy-dx;
x=x1;
y=y1;
putpixel(x+320,240-y,15);
while(x<=x2)
{
if(d<0)
d+=2*dy;
else
{
d+=2*(dy-dx);
y++;
}
x++;
putpixel(x+320,240-y,15);
}
}
//clipping
float tL,tB,tR,tT;
tL=(float)(xmin-xw1)/(float)(xw2-xw1);
tB=(float)(ymin-yw1)/(float)(yw2-yw1);
tR=(float)(xw1-xmax)/(float)(xw1-xw2);
tT=(float)(yw1-ymax)/(float)(yw1-yw2);
/* printf("%f\n",tL);
printf("%f\n",tR);
printf("%f\n",tB);
printf("%f\n",tT);

clrscr();
*/
// int f1,f2,f3,f4;
float p1x,p1y,p2x,p2y,t1,t2;
if(0<tB && tB<1)
t1=tB;
else if(0<tL && tL<1)
t1=tL;
else if(0<tB && tB<1 && 0<tL && tL<1)
{
if(tB>tL)
t1=tB;
else
t1=tL;
}
else
t1=0;
if(0<tR && tR<1)
t2=tR;
else if(0<tT && tT<1)
t2=tT;
else if(0<tT && tT<1 && 0<tR && tR<1)
{
if(tR<tT)
t2=tR;
else
t2=tT;
}
else
t2=0;

p1x=xw1+(xw2-xw1)*t1;
p1y=yw1+(yw2-yw1)*t1;
p2x=xw1+(xw2-xw1)*t2;
p2y=yw1+(yw2-yw1)*t2;
//clipped line
x1=p1x;
y1=p1y;
x2=p2x;
y2=p2y;

if(x2<x1 && y2<y1)


{
temp=x2;
x2=x1;
x1=temp;
temp1=y2;
y2=y1;
y1=temp1;
}
// clrscr();
dx=x2-x1;
dy=y2-y1;
// printf("%d %d",dx,dy);
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(x1+320,240-i,3);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(i+320,240-y1,3);
}
else if(abs(dy)>abs(dx))
{
d=dy-2*dx;
x=x1;
y=y1;
putpixel(x+320,240-y,3);
while(y<=y2)
{
if(d<0)
d-=2*dx;
else
{
d+=dy-2*dx;
x++;
}
y++;
putpixel(x+320,240-y,3);
}
}
else if(abs(dy)<=abs(dx))
{
d=2*dy-dx;

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

/* float tl;
tl=(xmin-xw1)/(xw2-xw1);
printf("%f\n",tl);
printf("%f",1/14);
*/
/* clean up */
getch();
closegraph();
return 0;
}

HERMITE CURVE
#include<graphics.h>
#include<iostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
struct
{
float
float
};
struct

pt
x,y;
mx,my;
pt g1,g2;

void main()
{
float outx,outy;
float u;
float gx[4],gy[4],tempx[4],tempy[4];
cout<<"Enter the
cin>>g1.x;
cout<<"\n\nEnter
cin>>g1.y;
cout<<"\n\nEnter
cin>>g2.x;
cout<<"\n\nEnter
cin>>g2.y;
cout<<"Enter
cin>>g1.mx;
cout<<"Enter
cin>>g1.my;
cout<<"Enter
cin>>g2.mx;
cout<<"Enter
cin>>g2.my;

x-co-ord of first point: ";


the y-co-ord of first point: ";
the x-co-ord of second point: ";
the y-co-ord of the second point: ";

the x-co-ord of the tangent vector at first end point";


the tangent vector's y co-ord at first end point: ";
the tangent vector's x-co-ord at second end point: ";
the tangent vector's y-co-ord at second end point: ";

int gdriver = DETECT, gmode, errorcode;


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

gx[0]=g1.x;
gx[1]=g2.x;
gx[2]=g1.mx;
gx[3]=g2.mx;
gy[0]=g1.y;
gy[1]=g2.y;
gy[2]=g1.my;
gy[3]=g2.my;
tempx[0]
tempx[1]
tempx[2]
tempx[3]

=
=
=
=

2*(gx[0]-gx[1]) + gx[2] + gx[3];


-3*(gx[0]-gx[1]) - 2*gx[2] - gx[3];
gx[2];
gx[0];

tempy[0]
tempy[1]
tempy[2]
tempy[3]

=
=
=
=

2*(gy[0]-gy[1]) + gy[2] + gy[3];


-3*(gy[0]-gy[1]) - 2*gy[2] - gy[3];
gy[2];
gy[0];

setcolor(RED);
line(0,240,640,240);
setcolor(BLUE);
line(320,0,320,480);
setcolor(WHITE);
for(u=0;u<=1;u+=0.0001)
{
outx=u*u*u*tempx[0]+u*u*tempx[1]+u*tempx[2]+tempx[3];
outy=u*u*u*tempy[0]+u*u*tempy[1]+u*tempy[2]+tempy[3];
putpixel(320+outx,240-outy,15);
}
getch();
}

GRAPHICS LAB REPORT


NAME:ANOOP JENA
ROLL NO:423/IC/12
ICE-1

You might also like