SlideShare a Scribd company logo
1
Program 1
WRITE A C PROGRAM TO DRAW LINE BY USING DDA ALGORITHM.
#include <graphics.h>
#include <stdio.h>
#include <math.h>
int main( )
{
float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i,gd,gm;
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 y1 : ");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,1);
2
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
OUTPUT:
3
Program 2
WRITE A C PROGRAM TO DRAW LINE BY USING BRESENHAM'S ALGORITHM.
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
printf("nntEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("nntEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:tcbgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
4
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
OUTPUT:
5
Program 3
WRITE A C PROGRAM TO DRAW A RECTANGLE.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main(){
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:TCBGI");
/* Draw rectangle on screen */
rectangle(150, 50, 400, 150);
/* Draw Bar on screen */
bar(150, 200, 400, 350);
getch();
closegraph();
return 0;
}
OUTPUT:
6
Program 4
WRITE A C PROGRAM TO DRAW A CIRCLE.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main(){
int gd = DETECT,gm;
int x ,y ,radius=80;
initgraph(&gd, &gm, "C:TCBGI");
/* Initialize center of circle with center of screen */
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(x-100, 50, "CIRCLE Using Graphics in C");
/* Draw circle on screen */
circle(x, y, radius);
getch();
closegraph();
return 0;
}
OUTPUT:
7
Program 5
WRITE A C PROGRAM TO DRAW A ELLIPSE.
#include<graphics.h>
#include<conio.h>
main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
ellipse(100, 100, 0, 360, 50, 25);
getch();
closegraph();
return 0;
}
OUTPUT:
8
Program 6
WRITE A C PROGRAM TO DRAW A TORUS.
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();
glTranslated(0.0,1.5,-6);
glRotated(-10, 1.0, 0.0, 0.0);
glutSolidTorus(0.4, 0.8, 10, 50);
glPopMatrix();
glPushMatrix();
glTranslated(0.0,-1.2,-6);
glutWireTorus(0.4, 0.8, 10, 20);
glPopMatrix();
glutSwapBuffers();
}
9
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Programming Techniques - 3D Torus");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
10
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
OUTPUT:
11
Program 7
WRITE A C PROGRAM TO SCALE AN OBJECT.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
int mx,my;
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
12
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}
OUTPUT:
13
Program 8
WRITE A C PROGRAM TO ROTATE AN OBJECT.
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2 ;
float b1,b2;
float t,deg;
initgraph(&gd,&gm,”c:tc”);
printf(“Enter the coordinates of Line n”);
scanf(“%d%d%d%d”,&x1,&y1,&x2,&y2);
setcolor(6);
line(x1,y1,x2,y2);
getch();
//cleardevice();
printf(“Enter the angle of rotation: “);
scanf(“%f”,&deg);
t=(22*deg)/(180*7);
b1=abs((x2*cos(t))-(y2*sin(t)));
b2=abs((x2*sin(t))+(y2*cos(t)));
line(x1,y1,b1,b2);
getch();
closegraph();
}
14
OUTPUT:
Program 9
WRITE A C PROGRAM TO SHEAR AN OBJECT ABOUT X-SHEAR, Y-SHEAR.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
int main()
{
int poly[30],a[9][3],b[3][3],c[9][3],poly2[30];
int x=0,y=0,p,i,j,k,xc,yc,ch;
int gd=DETECT,gm;
15
initgraph(&gd,&gm,"C:/TC/BGI");
xc=getmaxx()/2;
yc=getmaxy()/2;
setcolor(1);
setbkcolor(15);
setfillstyle(6,3);
printf("n Enter number of points : ");
scanf("%d",&p);
j=0;
for(i=0;i<p*2;i+=2)
{
printf("n Enter cordinate point x%d and y%d : ",j+1,j+1);
scanf("%d",&poly[i]);
scanf("%d",&poly[i+1]);
j++;
}
poly[p*2]=poly[0];
poly[p*2+1]=poly[1];
for(i=0;i<p*2;i+=2)
{
poly2[i]=xc+poly[i];
poly2[i+1]=yc-poly[i+1];
}
poly2[p*2]=poly2[0];
poly2[p*2+1]=poly2[1];
fillpoly(p+1,poly2);
line(0,yc,xc*2,yc);
line(xc,0,xc,yc*2);
printf("n Shearing of : n 1. x n 2. y n 3. Bothn enter choice : ");
scanf("%d",&ch);
if(ch==1)
{
printf("n Enter x shear value : ");
scanf("%d",&x);
}
if(ch==2)
{
printf("n Enter y shear value : ");
scanf("%d",&y);
}
16
if(ch==3)
{
printf("n Enter x shear value : ");
scanf("%d",&x);
printf("n Enter y shear value : ");
scanf("%d",&y);
}
j=0;
for(i=0;i<p;i++)
{
a[i][0]=poly[j];
a[i][1]=poly[++j];
a[i][2]=1;
++j;
}
if(ch==1)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
{
b[i][j]=1;
}
}
}
b[1][0]=x;
}
else if(ch==2)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
{
b[i][j]=1;
}
}
}
17
b[0][1]=y;
}
else if(ch==3)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
{
b[i][j]=1;
}
}
}
b[1][0]=x;
b[0][1]=y;
}
for(i=0;i<p;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("nnnnnt After Shearing : ");
for(i=0,j=0;i<p;i++,j+=2)
{
poly[j] =xc+c[i][0];
poly[j+1]=yc-c[i][1];
}
poly[j] =poly[0];
poly[j+1]=poly[1];
setfillstyle(9,2);
fillpoly(p+1,poly);
getch();
closegraph();
}
18
OUTPUT:
X_SHEAR:
Y-SHEAR:
19
Program 10
WRITE A PROGRAM TO DEMONSTRATE REFLECTION TRANSFORMATION ABOUT X-
AXIS, Y-AXIS AND ORIGIN.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
int main()
{
int poly[30],a[9][3],b[3][3],c[9][3],poly2[30];
int x,y,p,i,j,k,xc,yc;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:/TC/BGI");
xc=getmaxx()/2;
yc=getmaxy()/2;
setcolor(1);
setbkcolor(15);
setfillstyle(6,3);
printf("n Enter number of points : ");
scanf("%d",&p);
j=0;
for(i=0;i<p*2;i+=2)
{
printf("n Enter cordinate point x%d and y%d : ",j+1,j+1);
scanf("%d",&poly[i]);
scanf("%d",&poly[i+1]);
j++;
}
poly[p*2]=poly[0];
poly[p*2+1]=poly[1];
for(i=0;i<p*2;i+=2)
{
poly2[i]=xc+poly[i];
poly2[i+1]=yc-poly[i+1];
20
}
poly2[p*2]=poly2[0];
poly2[p*2+1]=poly2[1];
fillpoly(p+1,poly2);
line(0,yc,xc*2,yc);
line(xc,0,xc,yc*2);
printf("n Reflection about : n 1. x axisn 2. y axisn 3. originn enter choice : ");
scanf("%d",&x);
j=0;
for(i=0;i<p;i++)
{
a[i][0]=poly[j];
a[i][1]=poly[++j];
a[i][2]=1;
++j;
}
if(x==1)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
{
b[i][j]=1;
}
}
}
b[1][1]=-1;
}
else if(x==2)
{
for(i=0;i<3;i++)
{
21
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
{
b[i][j]=1;
}
}
}
b[0][0]=-1;
}
else if(x==3)
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
{
b[i][j]=-1;
}
}
}
b[2][2]=1;
}
for(i=0;i<p;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("nnnnnt Reflection : ");
22
for(i=0,j=0;i<p;i++,j+=2)
{
poly[j] =xc+c[i][0];
poly[j+1]=yc-c[i][1];
}
poly[j] =poly[0];
poly[j+1]=poly[1];
setfillstyle(9,2);
fillpoly(p+1,poly);
getch();
closegraph();
}
OUTPUT:
REFELCTION ABOUT ORIGIN:
23
REFELCTION ABOUT X-AXIS:
REFELCTION ABOUT Y-AXIS:
24

More Related Content

What's hot

Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
Ankit Kumar
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
Kamal Acharya
 
Graphics point clipping c program
Graphics point clipping c programGraphics point clipping c program
Graphics point clipping c program
Dr.M.Karthika parthasarathy
 
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
rohit kumar
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical File
Harjinder Singh
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
Harjinder Singh
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
sanchi29
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
Window to viewport transformation
Window to viewport transformationWindow to viewport transformation
Window to viewport transformation
Ankit Garg
 
Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.
Alamgir Hossain
 
Polygon filling
Polygon fillingPolygon filling
Polygon filling
Ankit Garg
 
Computer Graphics Practical
Computer Graphics PracticalComputer Graphics Practical
Computer Graphics PracticalNeha Sharma
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
C Programming
C ProgrammingC Programming
C Programming
Sumant Diwakar
 
Mid point circle algorithm
Mid point circle algorithmMid point circle algorithm
Mid point circle algorithm
Mani Kanth
 
Character generation
Character generationCharacter generation
Character generation
Ankit Garg
 
2D transformation (Computer Graphics)
2D transformation (Computer Graphics)2D transformation (Computer Graphics)
2D transformation (Computer Graphics)
Timbal Mayank
 

What's hot (20)

Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
 
Graphics point clipping c program
Graphics point clipping c programGraphics point clipping c program
Graphics point clipping c program
 
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Object Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical FileObject Oriented Programming Using C++ Practical File
Object Oriented Programming Using C++ Practical File
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
Window to viewport transformation
Window to viewport transformationWindow to viewport transformation
Window to viewport transformation
 
Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.
 
Polygon filling
Polygon fillingPolygon filling
Polygon filling
 
Computer Graphics Practical
Computer Graphics PracticalComputer Graphics Practical
Computer Graphics Practical
 
Strings in C
Strings in CStrings in C
Strings in C
 
C Programming
C ProgrammingC Programming
C Programming
 
Mid point circle algorithm
Mid point circle algorithmMid point circle algorithm
Mid point circle algorithm
 
Character generation
Character generationCharacter generation
Character generation
 
2D transformation (Computer Graphics)
2D transformation (Computer Graphics)2D transformation (Computer Graphics)
2D transformation (Computer Graphics)
 

Similar to Computer graphics lab assignment

1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf
sutharbharat59
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programsAmit Kapoor
 
Program to reflecta triangle
Program to reflecta triangleProgram to reflecta triangle
Program to reflecta triangle
Tanya Makkar
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
Ankit Kumar
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
Prianka Padmanaban
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
Prianka Padmanaban
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
Manoj Chauhan
 
Computer graphics File for Engineers
Computer graphics File for EngineersComputer graphics File for Engineers
Computer graphics File for Engineers
varun arora
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Kandarp Tiwari
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
Koshy Geoji
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
University of Potsdam
 
C basics
C basicsC basics
C basicsMSc CST
 
Cpds lab
Cpds labCpds lab
C Programming lab
C Programming labC Programming lab
C Programming lab
Vikram Nandini
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
PRATHAMESH DESHPANDE
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
kinan keshkeh
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
Syed Ahmed Zaki
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
Ashishchinu
 
C graphics programs file
C graphics programs fileC graphics programs file
C graphics programs file
shubham kanojia
 

Similar to Computer graphics lab assignment (20)

1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
 
Program to reflecta triangle
Program to reflecta triangleProgram to reflecta triangle
Program to reflecta triangle
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
 
Computer graphics File for Engineers
Computer graphics File for EngineersComputer graphics File for Engineers
Computer graphics File for Engineers
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
C basics
C basicsC basics
C basics
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
C Programming lab
C Programming labC Programming lab
C Programming lab
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
C graphics programs file
C graphics programs fileC graphics programs file
C graphics programs file
 

Recently uploaded

spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
01-GPON Fundamental fttx ftth basic .pptx
01-GPON Fundamental fttx ftth basic .pptx01-GPON Fundamental fttx ftth basic .pptx
01-GPON Fundamental fttx ftth basic .pptx
benykoy2024
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
dxobcob
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
obonagu
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 

Recently uploaded (20)

spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
01-GPON Fundamental fttx ftth basic .pptx
01-GPON Fundamental fttx ftth basic .pptx01-GPON Fundamental fttx ftth basic .pptx
01-GPON Fundamental fttx ftth basic .pptx
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 

Computer graphics lab assignment

  • 1. 1 Program 1 WRITE A C PROGRAM TO DRAW LINE BY USING DDA ALGORITHM. #include <graphics.h> #include <stdio.h> #include <math.h> int main( ) { float x,y,x1,y1,x2,y2,dx,dy,pixel; int i,gd,gm; 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 y1 : "); scanf("%f",&y2); detectgraph(&gd,&gm); initgraph(&gd,&gm,""); dx=abs(x2-x1); dy=abs(y2-y1); if(dx>=dy) pixel=dx; else pixel=dy; dx=dx/pixel; dy=dy/pixel; x=x1; y=y1; i=1; while(i<=pixel) { putpixel(x,y,1);
  • 3. 3 Program 2 WRITE A C PROGRAM TO DRAW LINE BY USING BRESENHAM'S ALGORITHM. # include <stdio.h> # include <conio.h> # include <graphics.h> void main() { int dx,dy,x,y,p,x1,y1,x2,y2; int gd,gm; printf("nntEnter the co-ordinates of first point : "); scanf("%d %d",&x1,&y1); printf("nntEnter the co-ordinates of second point : "); scanf("%d %d",&x2,&y2); dx = (x2 - x1); dy = (y2 - y1); p = 2 * (dy) - (dx); x = x1; y = y1; detectgraph(&gd,&gm); initgraph(&gd,&gm,"e:tcbgi"); putpixel(x,y,WHITE); while(x <= x2) { if(p < 0) { x=x+1; y=y; p = p + 2 * (dy); } else {
  • 4. 4 x=x+1; y=y+1; p = p + 2 * (dy - dx); } putpixel(x,y,WHITE); } getch(); closegraph(); } OUTPUT:
  • 5. 5 Program 3 WRITE A C PROGRAM TO DRAW A RECTANGLE. #include<stdio.h> #include<graphics.h> #include<conio.h> int main(){ int gd = DETECT,gm; initgraph(&gd, &gm, "C:TCBGI"); /* Draw rectangle on screen */ rectangle(150, 50, 400, 150); /* Draw Bar on screen */ bar(150, 200, 400, 350); getch(); closegraph(); return 0; } OUTPUT:
  • 6. 6 Program 4 WRITE A C PROGRAM TO DRAW A CIRCLE. #include<stdio.h> #include<graphics.h> #include<conio.h> int main(){ int gd = DETECT,gm; int x ,y ,radius=80; initgraph(&gd, &gm, "C:TCBGI"); /* Initialize center of circle with center of screen */ x = getmaxx()/2; y = getmaxy()/2; outtextxy(x-100, 50, "CIRCLE Using Graphics in C"); /* Draw circle on screen */ circle(x, y, radius); getch(); closegraph(); return 0; } OUTPUT:
  • 7. 7 Program 5 WRITE A C PROGRAM TO DRAW A ELLIPSE. #include<graphics.h> #include<conio.h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); ellipse(100, 100, 0, 360, 50, 25); getch(); closegraph(); return 0; } OUTPUT:
  • 8. 8 Program 6 WRITE A C PROGRAM TO DRAW A TORUS. #include <windows.h> #include <GL/glut.h> #include <stdlib.h> static void resize(int width, int height) { const float ar = (float) width / (float) height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity() ; } static void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3d(1,0,0); glPushMatrix(); glTranslated(0.0,1.5,-6); glRotated(-10, 1.0, 0.0, 0.0); glutSolidTorus(0.4, 0.8, 10, 50); glPopMatrix(); glPushMatrix(); glTranslated(0.0,-1.2,-6); glutWireTorus(0.4, 0.8, 10, 20); glPopMatrix(); glutSwapBuffers(); }
  • 9. 9 const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f }; const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f }; const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat high_shininess[] = { 100.0f }; /* Program entry point */ int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowSize(640,480); glutInitWindowPosition(10,10); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Programming Techniques - 3D Torus"); glutReshapeFunc(resize); glutDisplayFunc(display); glClearColor(1,1,1,1); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  • 11. 11 Program 7 WRITE A C PROGRAM TO SCALE AN OBJECT. #include<stdio.h> #include<conio.h> #include<graphics.h> #include<process.h> #include<math.h> int x1,y1,x2,y2,x3,y3,mx,my; void draw(); void scale(); void main() { int gd=DETECT,gm; int c; initgraph(&gd,&gm," "); printf("Enter the 1st point for the triangle:"); scanf("%d%d",&x1,&y1); printf("Enter the 2nd point for the triangle:"); scanf("%d%d",&x2,&y2); printf("Enter the 3rd point for the triangle:"); scanf("%d%d",&x3,&y3); draw(); scale(); } void draw() { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x1,y1); } void scale() { int x,y,a1,a2,a3,b1,b2,b3; int mx,my; printf("Enter the scalling coordinates"); scanf("%d%d",&x,&y); mx=(x1+x2+x3)/3;
  • 13. 13 Program 8 WRITE A C PROGRAM TO ROTATE AN OBJECT. #include<stdio.h> #include<graphics.h> void main() { int gd=DETECT,gm; int x1,y1,x2,y2 ; float b1,b2; float t,deg; initgraph(&gd,&gm,”c:tc”); printf(“Enter the coordinates of Line n”); scanf(“%d%d%d%d”,&x1,&y1,&x2,&y2); setcolor(6); line(x1,y1,x2,y2); getch(); //cleardevice(); printf(“Enter the angle of rotation: “); scanf(“%f”,&deg); t=(22*deg)/(180*7); b1=abs((x2*cos(t))-(y2*sin(t))); b2=abs((x2*sin(t))+(y2*cos(t))); line(x1,y1,b1,b2); getch(); closegraph(); }
  • 14. 14 OUTPUT: Program 9 WRITE A C PROGRAM TO SHEAR AN OBJECT ABOUT X-SHEAR, Y-SHEAR. #include<stdio.h> #include<conio.h> #include<dos.h> #include<graphics.h> int main() { int poly[30],a[9][3],b[3][3],c[9][3],poly2[30]; int x=0,y=0,p,i,j,k,xc,yc,ch; int gd=DETECT,gm;
  • 15. 15 initgraph(&gd,&gm,"C:/TC/BGI"); xc=getmaxx()/2; yc=getmaxy()/2; setcolor(1); setbkcolor(15); setfillstyle(6,3); printf("n Enter number of points : "); scanf("%d",&p); j=0; for(i=0;i<p*2;i+=2) { printf("n Enter cordinate point x%d and y%d : ",j+1,j+1); scanf("%d",&poly[i]); scanf("%d",&poly[i+1]); j++; } poly[p*2]=poly[0]; poly[p*2+1]=poly[1]; for(i=0;i<p*2;i+=2) { poly2[i]=xc+poly[i]; poly2[i+1]=yc-poly[i+1]; } poly2[p*2]=poly2[0]; poly2[p*2+1]=poly2[1]; fillpoly(p+1,poly2); line(0,yc,xc*2,yc); line(xc,0,xc,yc*2); printf("n Shearing of : n 1. x n 2. y n 3. Bothn enter choice : "); scanf("%d",&ch); if(ch==1) { printf("n Enter x shear value : "); scanf("%d",&x); } if(ch==2) { printf("n Enter y shear value : "); scanf("%d",&y); }
  • 16. 16 if(ch==3) { printf("n Enter x shear value : "); scanf("%d",&x); printf("n Enter y shear value : "); scanf("%d",&y); } j=0; for(i=0;i<p;i++) { a[i][0]=poly[j]; a[i][1]=poly[++j]; a[i][2]=1; ++j; } if(ch==1) { for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; if(i==j) { b[i][j]=1; } } } b[1][0]=x; } else if(ch==2) { for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; if(i==j) { b[i][j]=1; } } }
  • 17. 17 b[0][1]=y; } else if(ch==3) { for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; if(i==j) { b[i][j]=1; } } } b[1][0]=x; b[0][1]=y; } for(i=0;i<p;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } printf("nnnnnt After Shearing : "); for(i=0,j=0;i<p;i++,j+=2) { poly[j] =xc+c[i][0]; poly[j+1]=yc-c[i][1]; } poly[j] =poly[0]; poly[j+1]=poly[1]; setfillstyle(9,2); fillpoly(p+1,poly); getch(); closegraph(); }
  • 19. 19 Program 10 WRITE A PROGRAM TO DEMONSTRATE REFLECTION TRANSFORMATION ABOUT X- AXIS, Y-AXIS AND ORIGIN. #include<stdio.h> #include<conio.h> #include<dos.h> #include<graphics.h> int main() { int poly[30],a[9][3],b[3][3],c[9][3],poly2[30]; int x,y,p,i,j,k,xc,yc; int gd=DETECT,gm; initgraph(&gd,&gm,"C:/TC/BGI"); xc=getmaxx()/2; yc=getmaxy()/2; setcolor(1); setbkcolor(15); setfillstyle(6,3); printf("n Enter number of points : "); scanf("%d",&p); j=0; for(i=0;i<p*2;i+=2) { printf("n Enter cordinate point x%d and y%d : ",j+1,j+1); scanf("%d",&poly[i]); scanf("%d",&poly[i+1]); j++; } poly[p*2]=poly[0]; poly[p*2+1]=poly[1]; for(i=0;i<p*2;i+=2) { poly2[i]=xc+poly[i]; poly2[i+1]=yc-poly[i+1];
  • 20. 20 } poly2[p*2]=poly2[0]; poly2[p*2+1]=poly2[1]; fillpoly(p+1,poly2); line(0,yc,xc*2,yc); line(xc,0,xc,yc*2); printf("n Reflection about : n 1. x axisn 2. y axisn 3. originn enter choice : "); scanf("%d",&x); j=0; for(i=0;i<p;i++) { a[i][0]=poly[j]; a[i][1]=poly[++j]; a[i][2]=1; ++j; } if(x==1) { for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=0; if(i==j) { b[i][j]=1; } } } b[1][1]=-1; } else if(x==2) { for(i=0;i<3;i++) {
  • 24. 24