SlideShare a Scribd company logo
3
Most read
4
Most read
9
Most read
1
01. Name of the Problem: Program for the generation of Bresenham Line Drawing.
Objective: To generate basic line according to Bresenham’s Line-Drawing Algorithm.
Algorithm:
1. Input the two line endpoints and store the left endpoint in (x0, y0).
2. Set the color for frame-buffer position (x0, y0); i.e., plot the first point.
3. Calculate the constants _x, _y, 2_y, and 2_y − 2_x, and obtain the Starting value for the
decision parameter as p0 = 2_y − _x.
4. At each xk along the line, starting at k = 0, perform the following test: If pk < 0, the next point
to plot is (xk + 1, yk ) and pk+1 = pk + 2_y.Otherwise, the next point to plot is (xk + 1, yk + 1)
and pk+1 = pk + 2_y − 2_x.
5. Repeat step 4 _x − 1 more times.
Source Program:
#include<iostream>
#include<graphics.h>
using namespace std;
int main()
{
initwindow(500,600);
int x0,y0,x1,y1;
cout<<"Enter the First Line Coordinates:";
cin>>x0>>y0;
cout<<"Enter the Second Line Coordinates:";
cin>>x1>>y1;
int dx=x1-x0;
int dy=y1-y0;
int d=2*dy-dx;
int incrE=2*dy;
int incrNE=2*(dy-dx);
int x=x0;
int y=y0;
2
putpixel(x,y,5);
while(x<x1)
{
if(d<=0)
{
d+=incrE;
x++;
}
else
{
d+=incrNE;
x++;
y++;
}
putpixel(x,y,5);
}
delay(50000);
closegraph();
return 0;
}
Input and Output:
Figure: Input for the line drawing.
3
Figure: output of the line drawing.
Result and Discussion: From the sample output we can see that the Bresenham’s Line-
Drawing is very successful. For the following above Bresenham’s Line-Drawing code the output
picture is generated.
4
02. Name of the Problem: Program for the generation of Digital Differential Analyzer
(DDA) Line Drawing.
Objective: To generate basic line according to Digital Differential Analyzer (DDA) Line
Drawing Algorithm.
Algorithm:
1. Input the two line endpoints and store the left endpoint in (x0, y0).
2. Set the color for frame-buffer position (x0, y0); i.e., plot the first point.
3. Calculate the constants x, y, 2y, and 2y − 2x, and If x= 1 the next point to plot is yk+1 =yk + m
and If y= 1 the next point to plot is xk+1 =xk + 1/m
4. At each xk along the line, starting at k = 0, perform the following test: If x= -1 the next point
to plot is yk+1 =yk – m and If y= -1 the next point to plot is xk+1 =xk – 1/m and
5. Repeat step (4) x − 1 more times.
Source Program:
#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);
5
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,15);
x=x+dx;
y=y+dy;
i=i+1;
}
getch();
delay(1000);
}
6
Input and Output:
Figure: Input for the line drawing.
Figure: output of the line drawing.
Result and Discussion: From the sample output we can see that the Digital Differential
Analyzer (DDA) Line Drawing is very successful. For the following above Digital Differential
Analyzer (DDA) Line Drawing code the output picture is generated.
7
03. Name of the Problem: Program for the generation of Midpoint Circle Drawing.
Objective: To generate basic circle according to Midpoint Circle Drawing Algorithm.
Algorithm:
1. Input radius r and circle center (xc , yc ), then set the coordinates for the first point on the
circumference of a circle centered on the origin as (x0, y0) = (0, r )
2. Calculate the initial value of the decision parameter as p0 = (5/ 4) − r
3. At each xk position, starting at k = 0, perform the following test: If pk <0, the next point along
the circle centered on (0, 0) is (xk+1, yk ) and pk+1 = pk + 2xk+1 + 1 Otherwise, the next point
along the circle is (xk + 1, yk − 1) and pk+1 = pk + 2xk+1 + 1 − 2yk+1 where 2xk+1 = 2xk + 2
and 2yk+1 = 2yk − 2.
4. Determine symmetry points in the other seven octants.
5. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and plot
the coordinate values as follows: x = x + xc , y = y + yc.
6. Repeat steps 3 through 5 until x ≥ y.
Source Program:
#include<iostream>
#include<graphics.h>
using namespace std;
void drawcircle(int x0, int y0, int radius)
{
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
8
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
{
y += 1;
err += 2*y + 1;
}
if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:turboc3bgi");
cout<<"Enter radius of circle: ";
cin>>r;
cout<<"Enter co-ordinates of center(x and y): ";
cin>>x>>y;
drawcircle(x, y, r);
delay(50000);
closegraph();
return 0;
}
9
Input and Output:
Figure: Input for the circle drawing.
Figure: output of the circle drawing.
Result and Discussion: From the sample output we can see that the Midpoint Circle
Drawing was very successful. For the following above Midpoint Circle Drawing code the output
picture is generated.
10
04. Name of the Problem: Program for the generation of Midpoint Ellipse Drawing.
Objective: To generate basic ellipse according to Midpoint Ellipse Drawing Algorithm.
Algorithm:
1. Input rx, ry, and ellipse center (xc , yc ), and obtain the first point on an ellipse centered on the
origin as (x0, y0) = (0, ry)
2. Calculate the initial value of the decision parameter in region 1 as p10 = r 2
y− r 2
xry + 1 /4 r2
x
3. At each xk position in region 1, starting at k = 0, perform the following test: If p1k <0, the next
point along the ellipse centered on (0, 0) is (xk+1, yk ) and p1k+1 = p1k + 2r 2
y xk+1 + r 2
y
.Otherwise, the next point along the ellipse is (xk + 1, yk − 1) andp1k+1 = p1k + 2r 2
yxk+1 − 2r
2
x yk+1 + r 2
y with2r 2
yxk+1 = 2r 2
yxk + 2r 2
y, 2r 2
x yk+1 = 2r 2
x yk − 2r 2
x and continue until 2r
2
yx ≥ 2r x y.
4. Calculate the initial value of the decision parameter in region 2 as p20 = r 2
Y(x0 + ½)2
+ r 2
x
(y0 − 1)2 − r 2
xr 2
y where (x0, y0) is the last position calculated in region 1.
5. At each yk position in region 2, starting at k = 0, perform the following test: If p2k >0, the next
point along the ellipse centered on (0, 0) is (xk , yk − 1) and p2k+1 = p2k − 2r 2
x yk+1 + r 2
x
.Otherwise, the next point along the ellipse is (xk + 1, yk − 1) and p2k+1 = p2k + 2r 2
yxk+1 − 2r
2
x yk+1 + r 2
x using the same incremental calculations for x and y as in region 1. Continue until y
= 0.
6. For both regions, determine symmetry points in the other three quadrants.
7. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc , yc ) and
plot these coordinate values: x = x + xc , y = y + yc.
Source Program:
#include<graphics.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main()
{
int gd = DETECT, gm;
int xc,yc,x,y;float p;
long rx,ry;
initgraph(&gd, &gm, "C:TCBGI");
11
cout<<"Enter coordinates of centre : ";
cin>>xc>>yc;
cout<<"Enter x,y radius of ellipse: ";
cin>>rx>>ry;
p=ry*ry-rx*rx*ry+rx*rx/4;
x=0;y=ry;
while(2.0*ry*ry*x <= 2.0*rx*rx*y)
{ if(p < 0)
{ x++;
p = p+2*ry*ry*x+ry*ry;
} else
{ x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y-ry*ry;}
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);
}
p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;
while(y > 0)
{ if(p <= 0)
{ x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y+rx*rx;
} else
{ y--;
p = p-2*rx*rx*y+rx*rx;
}
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
12
putpixel(xc-x,yc-y,WHITE);
} delay(50000);
closegraph();
return 0;
}
Input and Output:
Figure: Input for the ellipse drawing.
Figure: output of the ellipse drawing.
Result and Discussion: From the sample output we can see that the Midpoint Ellipse
Drawing is very successful. For the following above Midpoint Ellipse Drawing code the output
picture is generated.
13
05. Name of the Problem: Program for the generation of Translating an object.
Objective: To reposition an object it along a straight-line path from on coordinate location to
another coordinate according to Translation Algorithm.
Algorithm:
1) Start
2) Initialize the graphics mode.
3) Construct a 2D object (use Drawpoly()) e.g. (x,y)
4) Get the translation value tx, ty
5) Move the 2d object with tx, ty (x’=x+tx,y’=y+ty)
6) Plot (x’,y’)
Source Program:
#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;
void translateLine ( int P[][2], int T[])
{
initwindow(600,800);
int gd = DETECT, gm, errorcode;
initgraph (&gd, &gm, "c:tcbgi");
ine(getmaxx()/2,0,getmaxx()/2,getmaxy());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
line(P[0][0], P[0][1], P[1][0], P[1][1]);
P[0][0] = P[0][0] + T[0];
P[0][1] = P[0][1] + T[1];
P[1][0] = P[1][0] + T[0];
P[1][1] = P[1][1] + T[1];
setcolor(3);
line(P[0][0], P[0][1], P[1][0], P[1][1]);
14
closegraph();
}
int main()
{
int P[2][2] = {60, 70, 30, 50}; // coordinates of point
int T[] = {10, 20}; // translation factor
translateLine (P, T);
delay(50000);
closegraph();
return 0;
}
Input and Output:
Coordinates of point: 60, 70, 30, 50
Translation factor: 10, 20
Figure: output of the ellipse drawing.
Result and Discussion: From the sample output we can see that the translating an object is
very successful. For the following above translation code the output picture is generated.
15
06. Name of the Problem: Program for the generation of Rotating an Object.
Objective: To reposition an object it along a circular path in the xy plane according to
Rotation Algorithm.
Algorithm:
1. Start
2. Initialize the graphics mode.
3. Construct a 2D object (use Drawpoly()) e.g. (x,y)
4. Get the Rotation angle
5.Rotate the object by the angle ф
x’=x cos ф - y sin ф
y’=x sin ф - y cosф
6. Plot (x’,y’)
Source Program:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y,xn,yn;
double r11,r12,r21,r22,th;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:tcbgi");
16
line(x1,y1,x2,y2);
printf("nEnter the angle:");
scanf("%lf",&th);
r11=cos((th*3.1428)/180);
r12=sin((th*3.1428)/180);
r21=(-sin((th*3.1428)/180));
r22=cos((th*3.1428)/180);
xn=((x2*r11)-(y2*r12));
yn=((x2*r12)+(y2*r11));
line(x1,y1,xn,yn);
getch();
closegraph();
}
Input and Output:
Figure: Input for the rotation of an Object.
17
Figure: output of the rotation of an Object
Result and Discussion: From the sample output we can see that the rotation of an Object is
very successful. For the following above Rotation of an object code the output picture is
generated.
18
07. Name of the Problem: Program for the generation of scaling an object.
Objective: To alter the size of an object by multiplying the coordinate values(x, y) according
to Scaling Algorithm.
Algorithm:
1. Start
2. Initialize the graphics mode.
3. Construct a 2D object (use Drawpoly()) e.g. (x,y)
4. Get the scaling value Sx,Sy
5. Resize the object with Sx,Sy (x’=x*Sx,y’=y*Sy)
6. Plot (x’,y’)
Source Program:
#include<stdio.h>
#include<graphics.h>
void findNewCoordinate(int s[][2], int p[][1])
{
int temp[2][1] = { 0 };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 1; j++)
for (int k = 0; k < 2; k++)
temp[i][j] += (s[i][k] * p[k][j]);
p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}
void scale(int x[], int y[], int sx, int sy)
{ line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
19
int s[2][2] = { sx, 0, 0, sy };
int p[2][1];
for (int i = 0; i < 3; i++)
{
p[0][0] = x[i];
p[1][0] = y[i];
findNewCoordinate(s, p);
x[i] = p[0][0];
y[i] = p[1][0];
}
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
}
int main()
{
int x[] = { 50, 100, 150 };
int y[] = { 100, 50, 100 };
int sx = 2, sy = 2;
int gd, gm;
detectgraph(&gd, &gm);
initgraph(&gd, &gm," ");
scale(x, y, sx,sy);
getch();
return 0;
}
20
Input and Output:
Drawing X axis: line (0,240,640,240)
Drawing Y axis: line (320, 0,320,480)
Original object: 500, 20
Transformed object: 500, 50
Figure: output of the scaling of an object.
Result and Discussion: From the sample output we can see that the scaling of an object is
very successful. For the following above the scaling of an object code the output picture is
generated.
Ad

Recommended

Computer graphics lab manual
Computer graphics lab manual
Ankit Kumar
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
Vivek Kumar Sinha
 
B. SC CSIT Computer Graphics Lab By Tekendra Nath Yogi
B. SC CSIT Computer Graphics Lab By Tekendra Nath Yogi
Tekendra Nath Yogi
 
2 d viewing computer graphics
2 d viewing computer graphics
KALESHWAR KUMAR
 
Clipping
Clipping
AMIT VIRAMGAMI
 
Bresenham circle
Bresenham circle
Taher Barodawala
 
Chapter 3 Output Primitives
Chapter 3 Output Primitives
PrathimaBaliga
 
Mid point circle algorithm
Mid point circle algorithm
Mani Kanth
 
Polygon filling algorithm
Polygon filling algorithm
Aparna Joshi
 
Illumination Models & Shading
Illumination Models & Shading
International Institute of Information Technology (I²IT)
 
Output primitives in Computer Graphics
Output primitives in Computer Graphics
Kamal Acharya
 
Code Optimization
Code Optimization
Akhil Kaushik
 
Parallel projection
Parallel projection
Prince Shahu
 
sum of subset problem using Backtracking
sum of subset problem using Backtracking
Abhishek Singh
 
Cohen-Sutherland Line Clipping Algorithm
Cohen-Sutherland Line Clipping Algorithm
Maruf Abdullah (Rion)
 
The n Queen Problem
The n Queen Problem
Sukrit Gupta
 
Dda algorithm
Dda algorithm
Mani Kanth
 
Computer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and Ellipse
2013901097
 
Computer graphics realism
Computer graphics realism
sathya dhineshkumar
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
vikas dhakane
 
Line drawing algo.
Line drawing algo.
Mohd Arif
 
Computer graphics
Computer graphics
Nanhen Verma
 
Compiler design lab programs
Compiler design lab programs
Guru Janbheshver University, Hisar
 
Raster Scan and Raster Scan Displays
Raster Scan and Raster Scan Displays
Saravana Priya
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
Swati Chauhan
 
Two pass Assembler
Two pass Assembler
Satyamevjayte Haxor
 
Bayesian networks
Bayesian networks
Massimiliano Patacchiola
 
Circle drawing algo.
Circle drawing algo.
Mohd Arif
 
Computer graphics
Computer graphics
Prianka Padmanaban
 
Computer graphics
Computer graphics
Prianka Padmanaban
 

More Related Content

What's hot (20)

Polygon filling algorithm
Polygon filling algorithm
Aparna Joshi
 
Illumination Models & Shading
Illumination Models & Shading
International Institute of Information Technology (I²IT)
 
Output primitives in Computer Graphics
Output primitives in Computer Graphics
Kamal Acharya
 
Code Optimization
Code Optimization
Akhil Kaushik
 
Parallel projection
Parallel projection
Prince Shahu
 
sum of subset problem using Backtracking
sum of subset problem using Backtracking
Abhishek Singh
 
Cohen-Sutherland Line Clipping Algorithm
Cohen-Sutherland Line Clipping Algorithm
Maruf Abdullah (Rion)
 
The n Queen Problem
The n Queen Problem
Sukrit Gupta
 
Dda algorithm
Dda algorithm
Mani Kanth
 
Computer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and Ellipse
2013901097
 
Computer graphics realism
Computer graphics realism
sathya dhineshkumar
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
vikas dhakane
 
Line drawing algo.
Line drawing algo.
Mohd Arif
 
Computer graphics
Computer graphics
Nanhen Verma
 
Compiler design lab programs
Compiler design lab programs
Guru Janbheshver University, Hisar
 
Raster Scan and Raster Scan Displays
Raster Scan and Raster Scan Displays
Saravana Priya
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
Swati Chauhan
 
Two pass Assembler
Two pass Assembler
Satyamevjayte Haxor
 
Bayesian networks
Bayesian networks
Massimiliano Patacchiola
 
Circle drawing algo.
Circle drawing algo.
Mohd Arif
 

Similar to Computer graphics lab report with code in cpp (20)

Computer graphics
Computer graphics
Prianka Padmanaban
 
Computer graphics
Computer graphics
Prianka Padmanaban
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Saikrishna Tanguturu
 
Computer graphics
Computer graphics
AAlha PaiKra
 
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
RajJain516913
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4
Roziq Bahtiar
 
CG-Lecture3.pptx
CG-Lecture3.pptx
lakshitasarika2014
 
Graphics practical lab manual
Graphics practical lab manual
Vivek Kumar Sinha
 
Computer Graphics Unit 1
Computer Graphics Unit 1
aravindangc
 
Computer Graphics
Computer Graphics
Sneha Chopra
 
Cs580
Cs580
Chellamuthu K
 
2D_line_circle.ppt
2D_line_circle.ppt
PuneetMathur39
 
module 1.pdf
module 1.pdf
KimTaehyung188352
 
Computer Graphics Unit 2
Computer Graphics Unit 2
SanthiNivas
 
Cgm Lab Manual
Cgm Lab Manual
Oriental College of Technology,Bhopal
 
Line circle draw
Line circle draw
Praveen Kumar
 
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi
54MahakBansal
 
Computer graphics notes 2 tutorials duniya
Computer graphics notes 2 tutorials duniya
TutorialsDuniya.com
 
Unit 3
Unit 3
Siddhant Goyal
 
Graphic Design Lab File.docx
Graphic Design Lab File.docx
PayalJindal19
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Saikrishna Tanguturu
 
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
RajJain516913
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4
Roziq Bahtiar
 
Graphics practical lab manual
Graphics practical lab manual
Vivek Kumar Sinha
 
Computer Graphics Unit 1
Computer Graphics Unit 1
aravindangc
 
Computer Graphics Unit 2
Computer Graphics Unit 2
SanthiNivas
 
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi
chapter 3 , foley.pptxhuujjjjjjjkjmmmm. Ibibhvucufucuvivihohi
54MahakBansal
 
Computer graphics notes 2 tutorials duniya
Computer graphics notes 2 tutorials duniya
TutorialsDuniya.com
 
Graphic Design Lab File.docx
Graphic Design Lab File.docx
PayalJindal19
 
Ad

More from Alamgir Hossain (13)

How to write a project proposal for software engineering course
How to write a project proposal for software engineering course
Alamgir Hossain
 
Malware Detection Approaches using Data Mining Techniques.pptx
Malware Detection Approaches using Data Mining Techniques.pptx
Alamgir Hossain
 
5 nested if in c with proper example
5 nested if in c with proper example
Alamgir Hossain
 
4. decision making and some basic problem
4. decision making and some basic problem
Alamgir Hossain
 
3. user input and some basic problem
3. user input and some basic problem
Alamgir Hossain
 
2. introduction of a c program
2. introduction of a c program
Alamgir Hossain
 
1. importance of c
1. importance of c
Alamgir Hossain
 
Report on student-faculty document sharing android project
Report on student-faculty document sharing android project
Alamgir Hossain
 
A lab report on modeling and simulation with python code
A lab report on modeling and simulation with python code
Alamgir Hossain
 
Lab report on to plot efficiency of pure and slotted aloha in matlab a data c...
Lab report on to plot efficiency of pure and slotted aloha in matlab a data c...
Alamgir Hossain
 
Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.
Alamgir Hossain
 
Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report
Alamgir Hossain
 
Microsoft Teams
Microsoft Teams
Alamgir Hossain
 
How to write a project proposal for software engineering course
How to write a project proposal for software engineering course
Alamgir Hossain
 
Malware Detection Approaches using Data Mining Techniques.pptx
Malware Detection Approaches using Data Mining Techniques.pptx
Alamgir Hossain
 
5 nested if in c with proper example
5 nested if in c with proper example
Alamgir Hossain
 
4. decision making and some basic problem
4. decision making and some basic problem
Alamgir Hossain
 
3. user input and some basic problem
3. user input and some basic problem
Alamgir Hossain
 
2. introduction of a c program
2. introduction of a c program
Alamgir Hossain
 
Report on student-faculty document sharing android project
Report on student-faculty document sharing android project
Alamgir Hossain
 
A lab report on modeling and simulation with python code
A lab report on modeling and simulation with python code
Alamgir Hossain
 
Lab report on to plot efficiency of pure and slotted aloha in matlab a data c...
Lab report on to plot efficiency of pure and slotted aloha in matlab a data c...
Alamgir Hossain
 
Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.
Alamgir Hossain
 
Digital signal Processing all matlab code with Lab report
Digital signal Processing all matlab code with Lab report
Alamgir Hossain
 
Ad

Recently uploaded (20)

Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
ICT-8-Module-REVISED-K-10-CURRICULUM.pdf
penafloridaarlyn
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Overview of Off Boarding in Odoo 18 Employees
Overview of Off Boarding in Odoo 18 Employees
Celine George
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Measuring, learning and applying multiplication facts.
Measuring, learning and applying multiplication facts.
cgilmore6
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
ABCs of Bookkeeping for Nonprofits TechSoup.pdf
TechSoup
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 

Computer graphics lab report with code in cpp

  • 1. 1 01. Name of the Problem: Program for the generation of Bresenham Line Drawing. Objective: To generate basic line according to Bresenham’s Line-Drawing Algorithm. Algorithm: 1. Input the two line endpoints and store the left endpoint in (x0, y0). 2. Set the color for frame-buffer position (x0, y0); i.e., plot the first point. 3. Calculate the constants _x, _y, 2_y, and 2_y − 2_x, and obtain the Starting value for the decision parameter as p0 = 2_y − _x. 4. At each xk along the line, starting at k = 0, perform the following test: If pk < 0, the next point to plot is (xk + 1, yk ) and pk+1 = pk + 2_y.Otherwise, the next point to plot is (xk + 1, yk + 1) and pk+1 = pk + 2_y − 2_x. 5. Repeat step 4 _x − 1 more times. Source Program: #include<iostream> #include<graphics.h> using namespace std; int main() { initwindow(500,600); int x0,y0,x1,y1; cout<<"Enter the First Line Coordinates:"; cin>>x0>>y0; cout<<"Enter the Second Line Coordinates:"; cin>>x1>>y1; int dx=x1-x0; int dy=y1-y0; int d=2*dy-dx; int incrE=2*dy; int incrNE=2*(dy-dx); int x=x0; int y=y0;
  • 3. 3 Figure: output of the line drawing. Result and Discussion: From the sample output we can see that the Bresenham’s Line- Drawing is very successful. For the following above Bresenham’s Line-Drawing code the output picture is generated.
  • 4. 4 02. Name of the Problem: Program for the generation of Digital Differential Analyzer (DDA) Line Drawing. Objective: To generate basic line according to Digital Differential Analyzer (DDA) Line Drawing Algorithm. Algorithm: 1. Input the two line endpoints and store the left endpoint in (x0, y0). 2. Set the color for frame-buffer position (x0, y0); i.e., plot the first point. 3. Calculate the constants x, y, 2y, and 2y − 2x, and If x= 1 the next point to plot is yk+1 =yk + m and If y= 1 the next point to plot is xk+1 =xk + 1/m 4. At each xk along the line, starting at k = 0, perform the following test: If x= -1 the next point to plot is yk+1 =yk – m and If y= -1 the next point to plot is xk+1 =xk – 1/m and 5. Repeat step (4) x − 1 more times. Source Program: #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);
  • 6. 6 Input and Output: Figure: Input for the line drawing. Figure: output of the line drawing. Result and Discussion: From the sample output we can see that the Digital Differential Analyzer (DDA) Line Drawing is very successful. For the following above Digital Differential Analyzer (DDA) Line Drawing code the output picture is generated.
  • 7. 7 03. Name of the Problem: Program for the generation of Midpoint Circle Drawing. Objective: To generate basic circle according to Midpoint Circle Drawing Algorithm. Algorithm: 1. Input radius r and circle center (xc , yc ), then set the coordinates for the first point on the circumference of a circle centered on the origin as (x0, y0) = (0, r ) 2. Calculate the initial value of the decision parameter as p0 = (5/ 4) − r 3. At each xk position, starting at k = 0, perform the following test: If pk <0, the next point along the circle centered on (0, 0) is (xk+1, yk ) and pk+1 = pk + 2xk+1 + 1 Otherwise, the next point along the circle is (xk + 1, yk − 1) and pk+1 = pk + 2xk+1 + 1 − 2yk+1 where 2xk+1 = 2xk + 2 and 2yk+1 = 2yk − 2. 4. Determine symmetry points in the other seven octants. 5. Move each calculated pixel position (x, y) onto the circular path centered at (xc , yc ) and plot the coordinate values as follows: x = x + xc , y = y + yc. 6. Repeat steps 3 through 5 until x ≥ y. Source Program: #include<iostream> #include<graphics.h> using namespace std; void drawcircle(int x0, int y0, int radius) { int x = radius; int y = 0; int err = 0; while (x >= y) { putpixel(x0 + x, y0 + y, 7); putpixel(x0 + y, y0 + x, 7); putpixel(x0 - y, y0 + x, 7); putpixel(x0 - x, y0 + y, 7); putpixel(x0 - x, y0 - y, 7);
  • 8. 8 putpixel(x0 - y, y0 - x, 7); putpixel(x0 + y, y0 - x, 7); putpixel(x0 + x, y0 - y, 7); if (err <= 0) { y += 1; err += 2*y + 1; } if (err > 0) { x -= 1; err -= 2*x + 1; } } } int main() { int gdriver=DETECT, gmode, error, x, y, r; initgraph(&gdriver, &gmode, "c:turboc3bgi"); cout<<"Enter radius of circle: "; cin>>r; cout<<"Enter co-ordinates of center(x and y): "; cin>>x>>y; drawcircle(x, y, r); delay(50000); closegraph(); return 0; }
  • 9. 9 Input and Output: Figure: Input for the circle drawing. Figure: output of the circle drawing. Result and Discussion: From the sample output we can see that the Midpoint Circle Drawing was very successful. For the following above Midpoint Circle Drawing code the output picture is generated.
  • 10. 10 04. Name of the Problem: Program for the generation of Midpoint Ellipse Drawing. Objective: To generate basic ellipse according to Midpoint Ellipse Drawing Algorithm. Algorithm: 1. Input rx, ry, and ellipse center (xc , yc ), and obtain the first point on an ellipse centered on the origin as (x0, y0) = (0, ry) 2. Calculate the initial value of the decision parameter in region 1 as p10 = r 2 y− r 2 xry + 1 /4 r2 x 3. At each xk position in region 1, starting at k = 0, perform the following test: If p1k <0, the next point along the ellipse centered on (0, 0) is (xk+1, yk ) and p1k+1 = p1k + 2r 2 y xk+1 + r 2 y .Otherwise, the next point along the ellipse is (xk + 1, yk − 1) andp1k+1 = p1k + 2r 2 yxk+1 − 2r 2 x yk+1 + r 2 y with2r 2 yxk+1 = 2r 2 yxk + 2r 2 y, 2r 2 x yk+1 = 2r 2 x yk − 2r 2 x and continue until 2r 2 yx ≥ 2r x y. 4. Calculate the initial value of the decision parameter in region 2 as p20 = r 2 Y(x0 + ½)2 + r 2 x (y0 − 1)2 − r 2 xr 2 y where (x0, y0) is the last position calculated in region 1. 5. At each yk position in region 2, starting at k = 0, perform the following test: If p2k >0, the next point along the ellipse centered on (0, 0) is (xk , yk − 1) and p2k+1 = p2k − 2r 2 x yk+1 + r 2 x .Otherwise, the next point along the ellipse is (xk + 1, yk − 1) and p2k+1 = p2k + 2r 2 yxk+1 − 2r 2 x yk+1 + r 2 x using the same incremental calculations for x and y as in region 1. Continue until y = 0. 6. For both regions, determine symmetry points in the other three quadrants. 7. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc , yc ) and plot these coordinate values: x = x + xc , y = y + yc. Source Program: #include<graphics.h> #include<stdlib.h> #include<iostream> using namespace std; int main() { int gd = DETECT, gm; int xc,yc,x,y;float p; long rx,ry; initgraph(&gd, &gm, "C:TCBGI");
  • 11. 11 cout<<"Enter coordinates of centre : "; cin>>xc>>yc; cout<<"Enter x,y radius of ellipse: "; cin>>rx>>ry; p=ry*ry-rx*rx*ry+rx*rx/4; x=0;y=ry; while(2.0*ry*ry*x <= 2.0*rx*rx*y) { if(p < 0) { x++; p = p+2*ry*ry*x+ry*ry; } else { x++;y--; p = p+2*ry*ry*x-2*rx*rx*y-ry*ry;} putpixel(xc+x,yc+y,WHITE); putpixel(xc+x,yc-y,WHITE); putpixel(xc-x,yc+y,WHITE); putpixel(xc-x,yc-y,WHITE); } p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry; while(y > 0) { if(p <= 0) { x++;y--; p = p+2*ry*ry*x-2*rx*rx*y+rx*rx; } else { y--; p = p-2*rx*rx*y+rx*rx; } putpixel(xc+x,yc+y,WHITE); putpixel(xc+x,yc-y,WHITE); putpixel(xc-x,yc+y,WHITE);
  • 12. 12 putpixel(xc-x,yc-y,WHITE); } delay(50000); closegraph(); return 0; } Input and Output: Figure: Input for the ellipse drawing. Figure: output of the ellipse drawing. Result and Discussion: From the sample output we can see that the Midpoint Ellipse Drawing is very successful. For the following above Midpoint Ellipse Drawing code the output picture is generated.
  • 13. 13 05. Name of the Problem: Program for the generation of Translating an object. Objective: To reposition an object it along a straight-line path from on coordinate location to another coordinate according to Translation Algorithm. Algorithm: 1) Start 2) Initialize the graphics mode. 3) Construct a 2D object (use Drawpoly()) e.g. (x,y) 4) Get the translation value tx, ty 5) Move the 2d object with tx, ty (x’=x+tx,y’=y+ty) 6) Plot (x’,y’) Source Program: #include<bits/stdc++.h> #include<graphics.h> using namespace std; void translateLine ( int P[][2], int T[]) { initwindow(600,800); int gd = DETECT, gm, errorcode; initgraph (&gd, &gm, "c:tcbgi"); ine(getmaxx()/2,0,getmaxx()/2,getmaxy()); line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(P[0][0], P[0][1], P[1][0], P[1][1]); P[0][0] = P[0][0] + T[0]; P[0][1] = P[0][1] + T[1]; P[1][0] = P[1][0] + T[0]; P[1][1] = P[1][1] + T[1]; setcolor(3); line(P[0][0], P[0][1], P[1][0], P[1][1]);
  • 14. 14 closegraph(); } int main() { int P[2][2] = {60, 70, 30, 50}; // coordinates of point int T[] = {10, 20}; // translation factor translateLine (P, T); delay(50000); closegraph(); return 0; } Input and Output: Coordinates of point: 60, 70, 30, 50 Translation factor: 10, 20 Figure: output of the ellipse drawing. Result and Discussion: From the sample output we can see that the translating an object is very successful. For the following above translation code the output picture is generated.
  • 15. 15 06. Name of the Problem: Program for the generation of Rotating an Object. Objective: To reposition an object it along a circular path in the xy plane according to Rotation Algorithm. Algorithm: 1. Start 2. Initialize the graphics mode. 3. Construct a 2D object (use Drawpoly()) e.g. (x,y) 4. Get the Rotation angle 5.Rotate the object by the angle ф x’=x cos ф - y sin ф y’=x sin ф - y cosф 6. Plot (x’,y’) Source Program: #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<math.h> int main() { int graphdriver=DETECT,graphmode,errorcode; int i; int x2,y2,x1,y1,x,y,xn,yn; double r11,r12,r21,r22,th; printf("Enter the 2 line end points:"); printf("x1,y1,x2,y2:"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); initgraph(&graphdriver,&graphmode,"c:tcbgi");
  • 17. 17 Figure: output of the rotation of an Object Result and Discussion: From the sample output we can see that the rotation of an Object is very successful. For the following above Rotation of an object code the output picture is generated.
  • 18. 18 07. Name of the Problem: Program for the generation of scaling an object. Objective: To alter the size of an object by multiplying the coordinate values(x, y) according to Scaling Algorithm. Algorithm: 1. Start 2. Initialize the graphics mode. 3. Construct a 2D object (use Drawpoly()) e.g. (x,y) 4. Get the scaling value Sx,Sy 5. Resize the object with Sx,Sy (x’=x*Sx,y’=y*Sy) 6. Plot (x’,y’) Source Program: #include<stdio.h> #include<graphics.h> void findNewCoordinate(int s[][2], int p[][1]) { int temp[2][1] = { 0 }; for (int i = 0; i < 2; i++) for (int j = 0; j < 1; j++) for (int k = 0; k < 2; k++) temp[i][j] += (s[i][k] * p[k][j]); p[0][0] = temp[0][0]; p[1][0] = temp[1][0]; } void scale(int x[], int y[], int sx, int sy) { line(x[0], y[0], x[1], y[1]); line(x[1], y[1], x[2], y[2]); line(x[2], y[2], x[0], y[0]);
  • 19. 19 int s[2][2] = { sx, 0, 0, sy }; int p[2][1]; for (int i = 0; i < 3; i++) { p[0][0] = x[i]; p[1][0] = y[i]; findNewCoordinate(s, p); x[i] = p[0][0]; y[i] = p[1][0]; } line(x[0], y[0], x[1], y[1]); line(x[1], y[1], x[2], y[2]); line(x[2], y[2], x[0], y[0]); } int main() { int x[] = { 50, 100, 150 }; int y[] = { 100, 50, 100 }; int sx = 2, sy = 2; int gd, gm; detectgraph(&gd, &gm); initgraph(&gd, &gm," "); scale(x, y, sx,sy); getch(); return 0; }
  • 20. 20 Input and Output: Drawing X axis: line (0,240,640,240) Drawing Y axis: line (320, 0,320,480) Original object: 500, 20 Transformed object: 500, 50 Figure: output of the scaling of an object. Result and Discussion: From the sample output we can see that the scaling of an object is very successful. For the following above the scaling of an object code the output picture is generated.