0% found this document useful (0 votes)
18 views24 pages

CG Labs

The documents discuss computer graphics concepts like drawing lines, circles and ellipses using the DDA and Bresenham's line algorithm. Functions are defined to draw various shapes by calculating and plotting pixels. Transformations like rotation, scaling and translation are also covered using transformation matrices.

Uploaded by

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

CG Labs

The documents discuss computer graphics concepts like drawing lines, circles and ellipses using the DDA and Bresenham's line algorithm. Functions are defined to draw various shapes by calculating and plotting pixels. Transformations like rotation, scaling and translation are also covered using transformation matrices.

Uploaded by

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

1.

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

void main()
{
clrscr();
int gd=DETECT, gm=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
float x1,x2,y1,y2;

printf("Enter start points of line(x1,y1)\n");


scanf("%f%f",&x1,&y1);

printf("Enter end points of line(x2,y2)\n");


scanf("%f%f",&x2,&y2);

float dx,dy,x=x1,y=y1,n;
int i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>=abs(dy))
n=abs(dx);
else
n=abs(dy);

putpixel((int)x,(int)y,4);
//printf("(%d,%d)",int(x),int(y));

for(i=1; i<=n; i++)


{
x=x+dx/n;
y=y+dy/n;
putpixel((int)x,(int)y,4);
// printf("(%d,%d)\t",int(x),int(y));
}
getch();
getch();
closegraph();
}
2.BLA
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void draw(int x1,int y1,int x2,int y2)


{
float dx,dy,p;
int i,x,y,xend;
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
//printf("(%d,%d)\t",x,y);
putpixel(x,y,4);
while(x<xend)
{
if(p<0)
{
x=x+1;
printf("(%d,%d)\t",x,y);
//putpixel(x,y,4);
p=p+(2*dy);
}
else
{
x=x+1;
y=y+1;
//printf("(%d,%d)\t",x,y);
putpixel(x,y,4);
p=p+(2*dy)-(2*dx);
}
}
}

void main()
{
clrscr();
int gd=DETECT, gm=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
int x1,x2,y1,y2;

printf("Enter values of x1 and y1: ");


scanf("%d%d",&x1,&y1);
printf("Enter values of x2 and y2 : ");
scanf("%d%d",&x2,&y2);

draw(x1,y1,x2,y2);//call to function that draws the line


getch();
getch();
closegraph();
}
3.Circle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
void drawaxis(int x,int y, int r)
{
line(x,y,x+r,y);
line(x,y,x-r,y);
line(x,y,x,y+r);
line(x,y,x,y-r);
}

void drawpoints(int x,int y,int xc,int yc)


{
putpixel(x+xc,y+yc,4);
// printf("(%+3d,%+3d) ",x+xc,y+yc);
putpixel(y+xc,x+yc,4);
//printf("(%+3d,%+3d) ",y+xc,x+yc);
putpixel(-x+xc,y+yc,4);
//printf("(%+3d,%+3d) ",-x+xc,y+yc);
putpixel(-y+xc,x+yc,4);
//printf("(%+3d,%+3d) ",-y+xc,x+yc);
putpixel(-x+xc,-y+yc,4);
//printf("(%+3d,%+3d) ",-x+xc,-y+yc);
putpixel(-y+xc,-x+yc,4);
//printf("(%+3d,%+3d) ",-y+xc,-x+yc);
putpixel(x+xc,-y+yc,4);
//printf("(%+3d,%+3d) ",x+xc,-y+yc);
putpixel(y+xc,-x+yc,4);
//printf("(%+3d,%+3d)\n",y+xc,-x+yc);
}

int main()
{
clrscr();
int gd=DETECT, gm=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

int xc,yc,r,x,y,p;
printf("Enter the center coordinates:");
scanf("%d%d",&xc,&yc);
printf("Enter the radius");
scanf("%d",&r);
drawaxis(xc,yc,r);
x=0;
y=r;
drawpoints(x,y,xc,yc);
p=1-r;
while(x<y)
{
if(p<0)
{
x=x+1;
y=y;
p=p+2*x+1;
}
else
{
x=x+1;
y=y-1;
p=p+2*(x-y)+1;
}
drawpoints(x,y,xc,yc);
getch();
}
getch();
closegraph();
}
4.Ellipse
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>

void drawaxis(long x,long y, long rx,long ry)


{
line(x,y,x+rx,y);
line(x,y,x-rx,y);
line(x,y,x,y+ry);
line(x,y,x,y-ry);
}

void drawpoints(long x,long y,long xc,long yc)


{
putpixel(x+xc,y+yc,4);
// printf("(%+3d,%+3d) ",x+xc,y+yc);
putpixel(-x+xc,y+yc,4);
// printf("(%+3d,%+3d) ",-x+xc,y+yc);
putpixel(x+xc,-y+yc,4);
// printf("(%+3d,%+3d) \n",x+xc,-y+yc);
putpixel(-x+xc,-y+yc,4);
// printf("(%+3d,%+3d) ",-x+xc,-y+yc);
getch();
}

int main()
{
clrscr();
int gd=DETECT, gm=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
long xc,yc,rx,ry,x,y;

printf("Enter the center coordinates:");


scanf("%ld%ld",&xc,&yc);
printf("Enter the radius(rx,ry)");
scanf("%ld%ld",&rx,&ry);
drawaxis(xc,yc,rx,ry);
x=0;
y=ry;
float p1,p2,dx,dy;
p1=pow(ry,2)- pow(rx,2)*ry + pow(rx,2)/4;

dx=2*ry*ry*x;
dy=2*rx*rx*y;

//region1
while(dx<dy)
{
drawpoints(x,y,xc,yc);
if(p1<0)
{
x++;
dx=2*ry*ry*x;
//dx=dx+(2*ry*ry);
p1=p1+dx+(ry*ry);
}
else
{
x++;
y--;
dx=2*ry*ry*x;
dy=2*rx*rx*y;
//dx=dx+(2*ry*ry);
//dy=dy-(2*rx*rx);
p1=p1+dx-dy+(ry*ry);
}
}
//region2
//printf("\nregion2\n");
p2=((ry*ry)*((x+0.5)*(x+0.5)))+((rx*rx)*((y-1)*(y-1)))-(rx*rx*ry*ry);
while(y>=0)
{
drawpoints(x,y,xc,yc);

if(p2>0)
{
y--;
dy=2*rx*rx*y;
p2 = p2 + (rx * rx) - dy;
}
else
{
y--;
x++;
dx=2*ry*ry*x;
dy=2*rx*rx*y;
p2= p2 + dx - dy + (rx * rx);
}

}
getch();
getch();
closegraph();
return 0;
}
5. Rotation
#include<stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
/* //comment no need to write
void printmatrix(int a[][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
}
void printmatrixf(float a[][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%f ",a[i][j]);
}
printf("\n");
}
printf("\n");
}
*/

void multiply(float x[][3],int y[][3],float z[][3])


{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
z[i][j]=0.00;
for(int k=0;k<3;k++)
{
z[i][j]+=x[i][k]*y[k][j];
}
}
}
}

void rotation(int o[][3],float t[][3])


{
float i[3][3];
multiply(t,o,i);
int x11=floor(i[0][0]);
int y11=floor(i[1][0]);
int x21=floor(i[0][1]);
int y21=floor(i[1][1]);
int x31=floor(i[0][2]);
int y31=floor(i[1][2]);
//printmatrix(o);
// printmatrixf(t);
// printmatrixf(i);
int arrImg[]={x11,y11,x21,y21,x31,y31,x11,y11};
setfillstyle(SOLID_FILL,BLUE);
fillpoly(4,arrImg);

int main()
{
clrscr();
int gd=DETECT, gm=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

int x1=400,y1=200;
int x2=400,y2=100;
int x3=300,y3=200;
float angle=40;
int o[3][3]= {x1,x2,x3,
y1,y2,y3,
1,1,1
}; //o for object,i for image

int arrObj[]= {x1,y1,x2,y2,x3,y3,x1,y1};


setfillstyle(SOLID_FILL,RED);
fillpoly(4,arrObj);
float a=(3.1417/180)*angle;
float rotationmatrix[3][3]= {cos(a),-sin(a),0,
sin(a),cos(a),0,
0,0,1
};
rotation(o,rotationmatrix);
getch();
closegraph();
return 0;
}
6. Scaling
#include<stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
/*
void printmatrix(int a[][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
}
*/
void multiply(int x[][3],int y[][3],int z[][3])
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
z[i][j]=0;
for(int k=0; k<3; k++)
{
z[i][j]+=x[i][k]*y[k][j];
}
}
}
}

void scaling(int o[][3],int t[][3])


{
int i[3][3];
multiply(t,o,i);
int x11=i[0][0];
int y11=i[1][0];
int x21=i[0][1];
int y21=i[1][1];
int x31=i[0][2];
int y31=i[1][2];
//printmatrix(o);
//printmatrix(t);
//printmatrix(i);
int arrImg[]= {x11,y11,x21,y21,x31,y31,x11,y11};
setfillstyle(SOLID_FILL,BLUE);
fillpoly(4,arrImg);
//line(x11,y11,x21,y21);
// line(x11,y11,x31,y31);
// line(x21,y21,x31,y31);

int main()
{
clrscr();
int gd=DETECT, gm=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Scaling");
int x1=50,y1=50;
int x2=200,y2=50;
int x3=150,y3=200;
int o[3][3]= {x1,x2,x3,
y1,y2,y3,
1,1,1
}; //o for object,i for image

int sx=2,sy=2;
int arrObj[]= {x1,y1,x2,y2,x3,y3,x1,y1};
setfillstyle(SOLID_FILL,RED);
fillpoly(4,arrObj);
//line(x1,y1,x2,y2);
//line(x1,y1,x3,y3);
//line(x2,y2,x3,y3);
int scalingmatrix[3][3]= {sx,0,0,
0,sy,0,
0,0,1
};
scaling(o,scalingmatrix);
getch();
closegraph();
return 0;
}
7. Translation
#include<stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
/*
void printmatrix(int a[][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
}
*/
void multiply(int x[][3],int y[][3],int z[][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
z[i][j]=0;
for(int k=0;k<3;k++)
{
z[i][j]+=x[i][k]*y[k][j];
}
}
}
}

void translation(int o[][3],int t[][3])


{
int i[3][3];
multiply(t,o,i);
int x11=i[0][0];
int y11=i[1][0];
int x21=i[0][1];
int y21=i[1][1];
int x31=i[0][2];
int y31=i[1][2];
//printmatrix(o);
//printmatrix(t);
//printmatrix(i);
int arrImg[]={x11,y11,x21,y21,x31,y31,x11,y11};
setfillstyle(SOLID_FILL,BLUE);
fillpoly(4,arrImg);
//line(x11,y11,x21,y21);
// line(x11,y11,x31,y31);
// line(x21,y21,x31,y31);

int main()
{
clrscr();
int gd=DETECT, gm=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

int x1=125,y1=225;
int x2=200,y2=100;
int x3=200,y3=400;
int o[3][3]={x1,x2,x3,
y1,y2,y3,
1,1,1}; //o for object,i for image

int tx=200,ty=0;
int arrObj[]={x1,y1,x2,y2,x3,y3,x1,y1};
setfillstyle(SOLID_FILL,RED);
fillpoly(4,arrObj);
//line(x1,y1,x2,y2);
//line(x1,y1,x3,y3);
//line(x2,y2,x3,y3);
int translationmatrix[3][3]={1,0,tx,
0,1,ty,
0,0,1};
translation(o,translationmatrix);
getch();
closegraph();
return 0;
}
8. Reflection
#include<stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>

float midx,midy;

void drawAxis()
{
float maxx=getmaxx();
float maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
line(0,midy,maxx,midy);
line(midx,0,midx,maxy);
}

/*
void printmatrix(int a[][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
}
*/
void multiply(int x[][3],int y[][3],int z[][3])
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
z[i][j]=0;
for(int k=0; k<3; k++)
{
z[i][j]+=x[i][k]*y[k][j];
}
}
}
}

void reflection(int o[][3],int t[][3])


{
int i[3][3];
multiply(t,o,i);
int x11=i[0][0];
int y11=i[1][0]+2*midy;
int x21=i[0][1];
int y21=i[1][1]+2*midy;
int x31=i[0][2];
int y31=i[1][2]+2*midy;
//printmatrix(o);
// printmatrix(t);
// printmatrix(i);
int arrImg[]= {x11,y11,x21,y21,x31,y31,x11,y11};
setfillstyle(SOLID_FILL,BLUE);
fillpoly(4,arrImg);

void reflectionv(int o[][3],int t[][3])


{
int i[3][3];
multiply(t,o,i);
int x11=i[0][0]+2*midx;
int y11=i[1][0];
int x21=i[0][1]+2*midx;
int y21=i[1][1];
int x31=i[0][2]+2*midx;
int y31=i[1][2];
int arrImg[]= {x11,y11,x21,y21,x31,y31,x11,y11};
setfillstyle(SOLID_FILL,YELLOW);
fillpoly(4,arrImg);

int main()
{
clrscr();
int gd=DETECT, gm=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Reflection\n");
printf("red=object\n");
printf("blue=image reflected along horizontal axis\n");
printf("yellow=image reflected along vertical axis\n");
int x1=300,y1=150;
int x2=100,y2=200;
int x3=200,y3=100;
drawAxis();
int o[3][3]= {x1,x2,x3,
y1,y2,y3,
1,1,1
}; //o for object,i for image

int arrObj[]= {x1,y1,x2,y2,x3,y3,x1,y1};


setfillstyle(SOLID_FILL,RED);
fillpoly(4,arrObj);

int reflectionmatrixh[3][3]= {1,0,0,


0,-1,0,
0,0,1
};
reflection(o,reflectionmatrixh);

int reflectionmatrixv[3][3]= {-1,0,0,


0,1,0,
0,0,1
};
reflectionv(o,reflectionmatrixv);
getch();
getch();
closegraph();
return 0;
}
9. 3D Rotation

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int x,y,z,o,x1,x2,y1,y2;

int gd=DETECT, gm=DETECT;


initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
//co-ordinates
int a=50; //top left x
int b=50; //top left y
int c=20; //distance between bottom left and bottom right

setfillstyle(1,RED);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();

bar3d(midx+a,midy-b,midx+a+c,midy-(b-c),5,1);
printf("Enter rotating angle");
scanf("%d",&o);
x1=a*cos(o*3.14/180)-(a+c)*sin(o*3.14/180);
y1=a*sin(o*3.14/180)+(a+c)*cos(o*3.14/180);
x2=(a+c)*cos(o*3.14/180)-(b-c)*sin(o*3.14/180);
y2=(a+c)*sin(o*3.14/180)+(b-c)*cos(o*3.14/180);
//axis();

printf("After rotation about z axis=Blue\n");


setfillstyle(3,BLUE);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
//axis();
printf("After rotation about x axis=Yellow\n");
setfillstyle(4,YELLOW);
bar3d(midx+a,midy-x1,midx+(a+c),midy-x2,5,1);

//axis();
printf("After rotation about yaxis=Green\n");
setfillstyle(5,GREEN);
bar3d(midx+x1,midy-b,midx+x2,midy-(b-c),5,1);

getch();
getch();
closegraph();
}
10.3D SCALING

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int x,y,z,o,x1,x2,y1,y2;

int gd=DETECT, gm=DETECT;


initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
//co-ordinates
int a=50; //top left x
int b=50; //top left y
int c=20; //distance between bottom left and bottom right
setfillstyle(1,RED);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;

axis();

bar3d(midx+a,midy-b,midx+a+c,midy-(b-c),5,1);
printf("Enter scaling factors");
scanf("%d%d%d", &x,&y,&z);

printf("After scaling");
setfillstyle(9,BLUE);
bar3d(midx+(x*a),midy-(y*b),midx+(x*(a+c)),midy-(y*(b-c)),5*z,1);

getch();
getch();
closegraph();
}
11. 3D Translation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int x,y,z,o,x1,x2,y1,y2;
int gd=DETECT, gm=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
//co-ordinates
int a=50; //top left x
int b=50; //top left y
int c=20; //distance between bottom left and bottom right
setfillstyle(1,RED);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;

axis();

bar3d(midx+a,midy-b,midx+a+c,midy-(b-c),10,1);

printf("Enter translation factor");


scanf("%d%d",&x,&y);
printf("After translation:");
setfillstyle(9,BLUE);
bar3d(midx+x+a,midy-(y+b),midx+x+(a+c),midy-(y+(b-c)),10,1);
getch();
getch();
closegraph();
}
12. GL draw a line
#include <windows.h>
#include <GL/glu.h>
#include <GL/freeglut.h>

void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
glBegin(GL_LINES);
glColor3f(1, 0, 0);
glVertex3f(-0.75, 0,0 );
glColor3f(0, 1, 0);
glVertex3f(0.9, 0.9, 0);
//glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
glFlush();
}

int main(int argc, char** argv)


{
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(400, 400); // Set the window's initial width and height
glutInitWindowPosition(100, 100); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
13.
//triangle
void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
glBegin(GL_POLYGON);
glColor3f(1, 1, 0);
glVertex3f(-0.6, -0.75, 0.5);
glColor3f(1, 0, 0);
glVertex3f(0.6, 0.0, 0);
glColor3f(1, 0, 0);
glVertex3f(-0.6, 0.75, 0);
glEnd();
glFlush(); // Flush drawing command buffer to make drawing happen as soon as possible.
}

14. Square

void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 0.5f, 1.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Flush drawing command buffer to make drawing happen as soon as possible.
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy