CG Lab
CG Lab
LAB MANUAL
1
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
INDEX
S.NO CONTENTS CO PAGE
NO.
1 VISION/MISION 4
2. PEO 4
3. PO’s 5
7. SYLLABUS 8
8. BOOKS 8
9. INSTRUCTIONAL METHODS 9
14. EXPERIMENTS 11
Exp: - 2 Objectives: - To draw pixel, line, ellipse, heart, smiley face and circle 1 14
using function
2
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Exp: -4 1 22
Objectives: - To Draw A Line Using DDA Algorithm
algorithm
Exp:-8 Objectives: - To rotate an object About Origin. 2 34
3
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
• To impart outcome based education for emerging technologies in the field of computer
science and engineering.
• To provide opportunities for interaction between academia and industry.
• To provide platform for lifelong learning by accepting the change in technologies.
• To develop aptitude of fulfilling social responsibilities.
2. PEO
4
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
3. PROGRAM OUTCOMES
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex
engineering problems.
2. Problem analysis: Identify, formulate, research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified needs
with appropriate consideration for the public health and safety, and the cultural,
societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data,
and synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge
to assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member
or leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with
the engineering community and with society at large, such as, being able to
comprehend and write effective reports and design documentation, make effective
presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary
environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
5
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
I 3 1 0 0 0 0 0 0 0 0 0 3
II 2 3 2 3 0 3 0 0 0 0 1 3
III 1 3 2 3 1 0 0 0 0 0 2 0
IV 0 0 0 1 2 0 3 2 3 0 2 0
V 0 0 0 0 0 0 0 0 2 2 0 0
5. COURSE OUTCOMES
Graduates would be able:
1. Apply basic concepts and different types of graphics drawing algorithm.
2. Analyze different transformations and animation.
MAPPING OF CO & PO
CO-PO Mapping
CGMT LAB 5CS4-21
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1: Apply basic
concepts and
different types of 3 3 3 3 2 2 1 1 1 1 2 3
graphics drawing
algorithm
CO2: Analyse
different
3 3 3 2 2 1 1 1 1 1 1 3
transformations and
animation
6
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
PSO: Ability to interpret and analyze network specific and cyber security issues, automation
in real word environment.
PSO2: Ability to Design and Develop Mobile and Web-based applications under realistic
constraints.
CO-PSO Mapping
CO’s PSO1 PSO2
7
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
7. SYLLABUS
Class: VI Sem. B.Tech. Evaluation
Branch: Computer Engg. Examination Time =Three (3) Hours
Schedule per Week Maximum Marks = 50
Practical Hrs.: 2 [Sessional/Mid-term (30) & Endterm (20)]
8. BOOKS: -
8.1 Text books:-
1. Hearn and Baker: Computer Graphics, PHI
2. Multimedia Systems Design, Prabhat Andleigh and Thakkar, PHI..
8.2 Reference Books :-
1. J. Foley, A. Van Dam, S. Feiner, J. Hughes: Computer Graphics- Principles and Practice, Pearson
8
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
9. INSTRUCTIONAL METHODS:-
9.1. Direct Instructions:
I. White board presentation
9.2. Interactive Instruction:
I. Algorithms
9.3. Indirect Instructions:
I. Problem solving
10. LEARNING MATERIALS: -
1. Text/Lab Manual
2. https://www.jecrcfoundation.com/student-corner/lab-videos
9
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
DON’TS
1. No one is allowed to bring storage devices like Pan Drive /Floppy etc. in the
lab.
2. Don’t mishandle the system.
3. Don’t leave the system on standing for long
4. Don’t bring any external material in the lab.
5. Don’t make noise in the lab.
6. Don’t bring the mobile in the lab. If extremely necessary then keep ringers off.
7. Don’t enter in the lab without permission of lab Incharge.
8. Don’t litter in the lab.
9. Don’t delete or make any modification in system files.
10. Don’t carry any lab equipments outside the lab.
We need your full support and cooperation for smooth functioning of the Lab
• All the students are supposed to prepare the theory regarding the next program.
• Students are supposed to bring the practical file and the lab copy.
• Previous programs should be written in the practical file.
• Any student not following these instructions will be denied entry in the lab.
10
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
14. EXPERIMENTS
Experiment No. 1
Aim:
Description
1) INITGRAPH
• Initializes the graphics system.
Declaration
• Void far initgraph(int far *graphdriver)
Remarks
• To start the graphic system, you must first call initgraph.
• Initgraph initializes the graphic system by loading a graphics driver from disk (or validating a
registered driver) then putting the system into graphics mode.
• Initgraph also resets all graphics settings (color, palette, current position, viewport, etc) to their defaults
then resets graph.
2) GETPIXEL, PUTPIXEL
• Getpixel gets the color of a specified pixel.
• Putpixel places a pixel at a specified point.
Declaration
• Unsigned far getpixel(int x, int y)
• Void far putpixel(int x, int y, int color)
Remarks
• Getpixel gets the color of the pixel located at (x,y);
• Putpixel plots a point in the color defined at (x, y).
Return value
• Getpixel returns the color of the given pixel.
• Putpixel does not return.
3) CLOSE GRAPH
• Shuts down the graphic system.
Declaration
• Void far closegraph(void);
11
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Remarks
• Close graph deallocates all memory allocated by the graphic system.
• It then restores the screen to the mode it was in before you called initgraph.
Return value
• None.
4) ARC, CIRCLE, PIESLICE
• arc draws a circular arc.
• Circle draws a circle
• Pieslice draws and fills a circular pieslice
Declaration
• Void far arc(int x, int y, int stangle, int endangle, int radius);
• Void far circle(int x, int y, int radius);
• Void far pieslice(int x, int y, int stangle, int endangle, int radius);
Remarks
• Arc draws a circular arc in the current drawing color
• Circle draws a circle in the current drawing color
• Pieslice draws a pieslice in the current drawing color, then fills it using the current fill pattern and fill
color.
Declaration
• Void far ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius)
• Void far fillellipse(int x, int y, int xradius, int yradius)
• Void farsectoe(int x, int y, int stangle, int endangle, int xradius, int yradius)
Remarks
• Ellipse draws an elliptical arc in the current drawing color.
• Fillellipse draws an elliptical arc in the current drawing color and than fills it with fill color and fill pattern.
• Sector draws an elliptical pie slice in the current drawing color and than fills it using the pattern and color
defined by setfillstyle or setfillpattern.
6) FLOODFILL
• Flood-fills a bounded region.
Declaration
• Void far floodfill(int x, int y, int border)
12
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Remarks
• Floodfills an enclosed area on bitmap device.
• The area bounded by the color border is flooded with the current fill pattern and fill color.
• (x,y) is a “seed point”
➢ If the seed is within an enclosed area, the inside will be filled.
➢ If the seed is outside the enclosed area, the exterior will be filled.
• Use fillpoly instead of floodfill wherever possible so you can maintain code compatibility with future
versions.
• Floodfill doesnot work with the IBM-8514 driver.
Return value
• If an error occurs while flooding a region, graph result returns ‘1’.
7) GETCOLOR, SETCOLOR
• Getcolor returns the current drawing color.
• Setcolor returns the current drawing color.
Declaration
• Int far getcolor(void);
• Void far setcolor(int color)
Remarks
• Getcolor returns the current drawing color.
• Setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
• To set a drawing color with setcolor , you can pass either the color number or the equivalent
color name.
8) LINE,LINEREL,LINETO
• Line draws a line between two specified pints.
• Onerel draws a line relative distance from current position(CP).
• Linrto draws a line from the current position (CP) to(x,y).
Declaration
• Void far lineto(int x, int y)
Remarks
• Line draws a line from (x1, y1) to (x2, y2) using the current color, line style and thickness. It does not
update the current position (CP).
• Linerel draws a line from the CP to a point that is relative distance (dx, dy) from the CP, then advances
the CP by (dx, dy).
• Lineto draws a line from the CP to (x, y), then moves the CP to (x,y).
Return value
• None
9) RECTANGLE
13
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Return value
• None.
Experiment No. 2
Aim:
To draw pixel, line, ellipse, heart, smiley face and circle using function
Source Code:
/* String */
// driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm, color;
// putpixel function
putpixel(85, 35, GREEN);
putpixel(30, 40, RED);
putpixel(115, 50, YELLOW);
putpixel(135, 50, CYAN);
putpixel(45, 60, BLUE);
putpixel(20, 100, WHITE);
putpixel(200, 100, LIGHTBLUE);
14
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
getch();
return 0;
}
Output
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
//init graphics
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
/*
if you are using turboc2 use below line to init graphics:
initgraph(&gd, &gm, "C:/TC/BGI");
// driver code
int main()
{
// gm is Graphics mode which is a computer display
// mode that generates image using pixels.
// DETECT is a macro defined in "graphics.h" header file
int gd = DETECT, gm;
15
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
getch();
Output
/* Ellipse*/
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* Initialize center of ellipse with center of screen */
x = getmaxx()/2;
y = getmaxy()/2;
getch();
closegraph();
return 0;
}
16
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Output
/*Heart*/
#include<constream.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
for(int l=1;l<=50;l++)
{
int b=1;
while(!kbhit())
{
for(int i=1;i<=20;i++)
settextstyle(3,0,5);
outtextxy(270,230,"Ankit");
arc(280,250,400,2000,50);
arc(355,250,700,500,50);
line(320,350,235,270);
line(320,350,400,270);
b++;
delay(8);
cleardevice();
if(b==70)
{
break;
}}}}
Output
17
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
/* Smiley Face*/
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
//for head
circle(200,200,30);
//for left eye
circle(190,190,5);
arc(190,190,50,130,10);
//for right eye
circle(210,190,5);
arc(210,190,50,130,10);
//for smiley lips
arc(200,210,180,360,10);
line(187,210,193,210);
line(207,210,213,210);
//for nose
line(198,195,195,200);
line(202,195,205,200);
line(195,200,200,205);
line(205,200,200,205);
getch();
closegraph();
}
OUTPUT:
18
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
//driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
// circle fuction
circle(250, 200, 50);
getch();
return 0;
}
Output
Viva Question:
19
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Experiment No. 3
Aim:
Algorithm:
1. Start.
2. Declare variables x,y,x1,y1,x2,y2,p,dx,dy and also declare gdriver=DETECT,gmode.
3. Initialize the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left end-points in (x1,y1).
5. Load (x1,y1) into the frame buffer; that is, plot the first point put x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1,and obtain the initial value of decision parameter p as:
a. p=(2dy-dx).
7. Starting from first point (x,y) perform the following test:
8. Repeat step 9 while(x<=x2).
9. If p<0,next point is (x+1,y) and p=(p+2dy).
10. Otherwise, the next point to plot is (x+1,y+1) and p=(p+2dy-2dx).
11. Place pixels using putpixel at points (x,y) in specified colour.
12. Close Graph.
13. Stop.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy; int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI:"); printf("\nEnter the x-coordinate of the first point ::");
scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point ::");
scanf("%d",&y1);
20
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
while(x<=x2)
{
if(p<0)
{
x=x+1;
p=2*x-dx;
}
else
{
x=x+1; y=y+1;
p=p+2*dy;
putpixel(x,y,7);
getch();
closegraph();
OutPut:
21
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Viva Questions:
Experiment No. 4
1. Start.
2. declare gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left end-points in (x1,y1).
5. Load (x1,y1) into the frame buffer;that is,plot the first point.put x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1.
7. If abs(dx) > abs(dy), do s=abs(dx).
8. Otherwise s= abs(dy).
9. Then xi=dx/s and yi=dy/s.
10. Start from k=0 and continuing till k<s,the points will be
i. x=x+xi.
ii. y=y+yi.
11. Place pixels using putpixel at points (x,y) in specified colour.
12. Close Graph.
22
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
13. Stop.
14. Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:");
printf("enter first point");
scanf("%d%d",&x1,&y1);
printf("enter second point");
scanf("%d%d",&x2,&y2); x=x1;
y=y1;
putpixel(x,y,7);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
xi=dx/s;
yi=dy/s;
x=x1;
y=y1;
putpixel(x,y,7);
for(k=0;k<s;k++)
{
x=x+xi;
y=y+yi;
putpixel(x,y,7);
23
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
}
getch();
closegraph();
}
Output:
Viva questions:
Experiment No.5
Algorithm
1. Start.
2. Declare variables x,y,p and also declare gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC folder.
4. Input the radius of the circle r.
5. Load x-0,y=r,initial decision parameter p=1-r.so the first point is (0,r).
6. Repeat Step 7 while (x<y) and increment x-value simultaneously. 7. If
24
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
(p>0),do p=p+2*(x-y)+1.
8. Otherwise p=p+2*x+1 and y is decremented simultaneously.
9. Then calculate the value of the function circlepoints() with p.arameters (x,y).
10. Place pixels using putpixel at points (x+300,y+300) in specified colour in circlepoints() function shifting
the origin to 300 on both x-axis and y-axis.
11. Close Graph.
12. Stop.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circlepoints(int,int);
void main()
{
int x,y,p,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:"); clrscr();
printf("enter the radius");
scanf("%d",&r); x=0;y=r;p=1-r; while(x<y)
{
x++;
if(p>0)
{
}
else
p=p+2*(x-y)+1; y--;
p=p+2*x+1;
circlepoints(x,y);
}
getch(); closegraph();
}
void circlepoints(int x,int y)
{
putpixel(x+300,y+300,8);
putpixel(x+300,-y+300,8);
putpixel(-x+300,y+300,8);
putpixel(-x+300,-y+300,8);
putpixel(y+300,x+300,8);
putpixel(y+300,-x+300,8);
putpixel(-y+300,x+300,8);
putpixel(-y+300,-x+300,8);
25
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
}
{
putpixel(x+300,y+300,8); putpixel(x+300,-y+300,8); putpixel(-
x+300,y+300,8); putpixel(-x+300,-y+300,8); putpixel(y+300,x+300,8);
putpixel(y+300,-x+300,8); putpixel(-y+300,x+300,8); putpixel(-y+300,-
x+300,8);
}
Output:
Viva Questions:
26
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Experiment No. 6
Aim:
Algorithm
1. Start.
2. Initialize the graphic system using initgraph function.
3. Get the input of radius of major and minor arc from the user.
4. Store the values of major and minor arc in an another variable.
5. Square the values of major and minor arc.
6. Calculate decision parameter P = (square of minor axis – (square of major axis*minor axis)
+ (0.25* square of major axis).
27
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void ellips(int x,int y);
void completellipse(int r,int g,int u,int v)
{
float s,k,e,f,x; double p1,p2; s=r;k=g; e=(pow((s+.5),2));
f=(pow((k-1),2));
p2=((u*e)+(v*f)-(u*v)); ellips(s,k);
while(k>=0)
{
if(p2>0)
p2=(p2+v-(2*v*s));
else
{
}
k--;
p2=(p2+(2*u*(s+1))-(2*v*(k-1))+v); s++;
ellips(s,k);
}
}
void main()
{
28
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Output:
Viva Questions:
29
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
Experiment No. 7
Algorithm
1. Start.
2. Initialize the graphic system using initgraph function.
3. Get the input of window co ordinates from the user and draw a window.
4. Get the input of line co ordinates from user and draw the line.
5. Calculate the region code of each end point of line using relation given in steps 6 to step
6. Let (x,y) be the co ordinates of end point of line and (xmin,ymin), (xmax,ymax) be co ordinates of
world window
7. If y –ymax = +ve
8. MSB region code = 1.
9. Else MSB region code = 0.
10. If ymin – y = +ve
11. Region code = 1.
12. Else Region code = 0.
13. If x – xmax = +ve
14. Region code = 1.
15. Else Region code = 0.
16. If xmin – x = +ve
17. LSB Region code = 1.
18. Else LSB Region code = 0.
19. Calculate region code of both end points.
20. Logically and both region code.
21. If Logically anded result is = 0
22. Line is not a clipping candidate.
23. Else.
24. Line is a clipping candidate.
25. Calculate slope of line using formula slope=(y2-y1)/(x2-x1).
30
Jaipur Engineering college and research
centre, Shri Ram ki Nangal, via Sitapura Academic year-
2019-20
RIICO Jaipur- 302 022.
35. Stop.
.
Source code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void storepoints(int,int,int,int,int,int,int[]); void main()
{
int gdriver=DETECT,gmode;
int x1,x2,y1,y2,xmax,ymax,xmin,ymin,a[10],b[10],xi1,xi2,yi1,yi2,flag=0; float m;
int i; clrscr();
printf("output"); printf("\n");
printf("enter the value of x1,y1,x2,y2: >"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); printf("enter
the value of xmax,ymax,xmin,ymin:"); scanf("%d%d%d%d",&xmax,&ymax,&xmin,&ymin);
storepoints(x2,y2,ymin,ymax,xmax,xmin,b); for(i=1;i<=4;i++)
{
if(a[i]*b[i]==0)
flag=1;
else
}
flag=0;
if(flag==1)
{
m=(y2-y1)/(x2-x1); xi1=x1;
yi1=y1;
}
if(a[1]==1)
{
}
31
else
{
yi1=ymax; xi1=x1+((1/m)*(yi1-y1));
if(a[2]==1)
{
yi1=ymin; xi1=x1+((1/m)*(yi1-y1));
}
}
if(a[3]==1)
{
xi1=xmax; yi1=y1+(m*(xi1-x1));
}
if(a[4]==1)
{
}
else
xi1=xmin; yi1=y1+(m*(xi1-x1));
if(b[1]==1)
{
}
else
yi2=ymax; xi2=x2+((1/m)*(yi2-y2));
if(b[2]==1)
{
}
else
yi2=ymin; xi2=x2+((1/m)*(yi2-y2));
if(b[3]==1)
{
clrscr();
}
else
xi2=xmax; yi2=y2+((1/m)*(xi2-x2));
if(b[4]==1)
{
xi2=xmin; yi2=y2+(m*(xi2-x2));
}
initgraph(&gdriver,&gmode,"c://tc//bgi:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
delay(5000);
closegraph();
clrscr();
initgraph(&gdriver,&gmode,"c://tc//bgi:");
line(xi1,yi1,xi2,yi2);
rectangle(xmin,ymin,xmax,ymax); if(flag==0)
{
printf("\n no clipping is required");
}
32
getch(); closegraph();
}
void storepoints(int x1,int y1,int ymax,int xmax,int xmin,int ymin,int c[10])
{
if((y1-ymax)>0)
c[1]=1;
else
c[1]=0;
if((ymin-y1)>0)
c[2]=1;
else
c[2]=0;
if((x1-xmax)>0)
c[3]=1;
else
c[3]=0;
if((xmin-x1)>0)
c[4]=1;
else
}
c[4]=0;
}
Output:
enter the value of
x1,y1,x2,y2: >10 10
100
100
enter the value of
xmax,ymax,xmin,ymin50 50
0
0
Viva Questions:
33
Experiment No. 8
Aim:
To rotate an object about origin
Source code:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h> void main()
{
clrscr();
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,a[3][3];
double b[3][3],c[3][3];
cout<<"\n Enter Ist coordinates of triangle:";
cin>>a[0][0]>>a[1][0];
cout<<"\n Enter 2nd coordinates of triangle:";
cin>>a[0][1]>>a[1][1];
cout<<"\n Enter 3rd coordinates of triangle:";
cin>>a[0][2]>>a[1][2];
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
getch(); cleardevice();
cout<<"\n Enter angle of rotation:\n"; cin>>x;
b[0][0]=b[1][1]=cos((x*3.14)/180);
b[0][1]=-sin((x*3.14)/180);
b[1][0]=sin((x*3.14)/180); b[2][2]=1; b[2][0]=b[2][1]=b[0][2]=b[1][2]= 0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{ c[i][j]=0;
for (int k=0; k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
} x1=(c[i][j]+0.5);
a[i][j]=x1;
}
}
cout<<"\n Triangle after rotation is:\n" ;
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
getch(); closegraph();
34
}
Output:
Viva Questions:
35
Experiment No.9
Aim:
To scale an object with scaling factors along X and Y directions
Source Program:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm; initgraph(&gd, &gm,"");
cleardevice();
int x1,y1,x2,y2,x3,y3,x4,y4;
float sx,sy;
cout<<"Enter the first coordinates of triangle\n";
cin>>x1>>y1;
cout<<"Enter the second coordinates of triangle\n";
cin>>x2>>y2;
cout<<"Enter the third coordinates of triangle\n";
cin>>x3>>y3;
int poly[8]={x1,y1,x2,y2,x3,y3,x1,y1};
cleardevice();
drawpoly(4,poly); getch();
cout<<"Enter the scaling factors\n";
cin>>sx>>sy;
x4=sx*x1-x1; y4=sy*y1-y1;
x1=sx*x1-x4; y1=sy*y1-y4; x2=sx*x2-x4;
y2=sy*y2-y4; x3=sx*x3-x4; y3=sy*y3-y4;
poly[0]=x1; poly[1]=y1; poly[2]=x2; poly[3]=y2;
poly[4]=x3; poly[5]=y3; poly[6]=x1; poly[7]=y1;
getch();
cleardevice();
drawpoly(4,poly);
getch();
closegraph();
}
Output:
36
Result:
Viva Questions:
1. What is scaling
2. What are the different transformation
37
Experiment No. 10
Aim:
To translate an object with translation parameters in X and Y directions
Description:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
clrscr();
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,y1,x2,y2,x3,y3;
cout<<"\n Enter Ist coordinates of triangle:"; cin>>x1>>y1;
cout<<"\n Enter 2nd coordinates of triangle:"; cin>>x2>>y2;
cout<<"\n Enter 3rd coordinates of triangle:"; cin>>x3>>y3;
cleardevice(); line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3); getch(); cleardevice();
cout<<"\n Enter translatio factors :\n"; cin>>x>>y;
x1-=x;
y1-=y;
x2-=x;
y2-=y;
x3-=x;
y3-=y;
cleardevice(); line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
getch();
closegraph();
}
Output:
38
Result:
Hence translation of an object is implemented
Viva Questions:
1. What is basic transformation
2. What is translation
Experiment No. 11
Source Code
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h> void main()
{
clrscr();
int gm,gd=DETECT; initgraph(&gd,&gm,"");
int h,k,x1,y1,x2,y2,x3,y3; float t;
cout<<" OUTPUT"<<endl;
cout<<"Enter the coordinates of point"<<endl; cin>>x2>>y2;
putpixel(x2,y2,2);
cout<<"Enter the coordinates of point around which rotation is done"<<endl; cin>>h>>k;
putpixel(h,k,2);
cout<<"Enter the angle for rotation"<<endl; cin>>t;
cleardevice(); x1=(h*cos(t))-(k*sin(t));
y1=(h*sin(t))+(k*cos(t)); x3=x1+x2-h;
y3=y1+y2-k;
cout<<"Point after rotation is:";
39
putpixel(x3,y3,2);
getch();
closegraph();
}
Output:
Viva Question:
1. What is rotation
2. What is basic transformation
40
Experiment No. 12
Source Code
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h> void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
int h,k,x1,y1,x2,y2,x3,y3;
float t;
cout<<" OUTPUT"<<endl;
cout<<"Enter the coordinates of point"<<endl; cin>>x2>>y2;
putpixel(x2,y2,2);
cout<<"Enter the angle for rotation"<<endl; cin>>t;
cleardevice();
x1=int(x2*cos(t*3.14/180))-(y2*sin(t*3.14/180));
y1=int(x2*sin(t*3.14/180))+(y2*cos(t*3.14/180));
cout<<"Point after rotation is:";
putpixel(x1,y1,2);
getch();
closegraph();
}
Output:
Viva Question:
1. What is transformation
41
Experiment No. 13
Source Code
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h> void main()
{
clrscr();
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,a[3][3];
double b[3][3],c[3][3];
cout<<"\n Enter Ist coordinates of triangle:"; cin>>a[0][0]>>a[1][0];
cout<<"\n Enter 2nd coordinates of triangle:"; cin>>a[0][1]>>a[1][1];
cout<<"\n Enter 3rd coordinates of triangle:"; cin>>a[0][2]>>a[1][2];
cout<<"\n Enter 1. for reflection in x-axis:\n"; cout<<"\n Enter 2. for reflection in y-axis:\n";
cout<<"\n Enter 3. for reflection in both the axis:\n"; cin>>x;
cleardevice(); line(320,0,320,479); line(0,240,639,240);
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
switch(x)
{
case 1:b[0][0]=640-a[0][0];
b[0][1]=640-a[0][1];
b[0][2]=640-a[0][2];
b[1][0]=a[1][0];
b[1][1]=a[1][1];
b[1][2]=a[1][2]; line(320,0,320,479); line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch(); break;
case 2:b[1][0]=480-a[1][0];
b[1][1]=480-a[1][1];
b[1][2]=480-a[1][2];
b[0][0]=a[0][0];
b[0][1]=a[0][1];
b[0][2]=a[0][2]; line(320,0,320,479); line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch(); break;
case 3: b[0][0]=640-a[0][0];
b[0][1]=640-a[0][1];
42
b[0][2]=640-a[0][2];
b[1][0]=a[1][0];
b[1][1]=a[1][1];
b[1][2]=a[1][2]; line(320,0,320,479); line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]); b[1][0]=480-a[1][0];
b[1][1]=480-a[1][1];
b[1][2]=480-a[1][2];
b[0][0]=a[0][0];
b[0][1]=a[0][1];
b[0][2]=a[0][2]; line(320,0,320,479); line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch(); break;
}
getch(); closegraph();
}
Output:
43
Result: Hence Reflection a Triangle is implemented
Viva Question:
1. What is Shearing
2. What is reflection
Experiment No. 14
Source Code
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h> void main()
{
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
line(100,100,150,50);
line(150,50,200,100); line(100,100,200,100); line(150,50,350,50); line(200,100,350,100);
line(350,50,350,100);
circle(150,75,10); rectangle(100,100,200,300);
rectangle(200,100,350,300); rectangle(250,175,300,225);
line(250,175,300,225); line(300,175,250,225); line(125,300,125,225); line(175,300,175,225);
arc(150,225,0,180,25);
44
getch(); closegraph();
}
Output:
Viva Question:
1. What is graphics function
45
Experiment No. 15
Source Code
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h> void main()
{
int graphdriver=DETECT,graphmode; initgraph(&graphdriver,&graphmode,"...\\bgi");
int p=1,x;
int a[12]={100,100,150,150,200,100,200,200,100,200,100,100};
drawpoly(6,a);
for(int i=100;i<200;i++)
{
p=1;
for(int j=100;j<=200;j++)
{
x=getpixel(j,i);
for(int d=0;d<11;d++)
{
if(j==a[d]&&i==a[d+1] ) break;
else
{
if(x>0&&d==10) p++;
if(p%2==0)
putpixel(j,i,4);
}
}
}
}
getch();
closegraph();
}
46
Output:
Viva Question:
1. What is graphics function
Experiment No. 16
Source Code
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void bird();
void handup();
void handdown();
void heli();
void para();
int a,b,c,d,e,k;
void main()
{
47
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
//setcolor(GREEN);
b=0;
c=0;
d=0;
for(a=0;a<80;a++)
{
heli();
delay(100);
cleardevice();
k=k+3;
}
heli();
for(a=0;a<90;a++)
{
para();
heli();
if(a>20){k=k+6;}
delay(100);
cleardevice();
if(a<50){
b=b+2;
}
}
getch();
}
void heli(){
ellipse(100+k,200,270,90,20,15);
line(100+k,185,80+k,185);
line(100+k,215,80+k,215);
ellipse(80+k,210,160,270,15,5);
ellipse(80+k,190,90,170,15,5);
line(65+k,190,5+k,195);
line(65+k,210,5+k,200);
//circle(5+k,197,15);
if(a%2==0){
pieslice(5+k,197,30,60,15);
pieslice(5+k,197,210,240,15);
}
else{
48
pieslice(5+k,197,120,150,15);
pieslice(5+k,197,300,330,15);
}
bar(90+k,185,92+k,170);
bar(70+k,168,110+k,170);
line(94+k,190,94+k,196);
line(96+k,200,110+k,200);
ellipse(102+k,195,160,220,8,8);
line(90+k,215,85+k,230);
line(95+k,215,100+k,230);
bar(80+k,230,108+k,232);
}
void para()
{
arc(340,260+b,0,180,20);
arc(340,263+b,0,180,20);
line(320,263+b,330,280+b);
line(360,263+b,350,280+b);
line(330,280+b,331,290+b);
line(328,280+b,330,290+b);
line(350,280+b,348,290+b);
line(349,280+b,347,290+b);
ellipse(340,280+b,0,360,4,6);
rectangle(336,285+b,344,302+b);
bar(330,288+b,336,289+b);
bar(338,302+b,339,310+b);
bar(342,302+b,343,310+b);
line(336,302+b,339,312+b);
ellipse(344,312+b,0,360,2,1);
ellipse(338,312+b,0,360,2,1);
Output:
49
Viva Question:
1. What is graphics function
2. What is rectangle function
Experiment No. 17
Source Code
#include<graphics.h> //for graphics
#include<stdio.h> //standard input/output
#include<string.h> //string function
#include<conio.h> //console input output
#include<math.h> //math calculation
#define PI 3.14 //value constant pi=3.14
char speed[4]; //array declaration for speed
int theta=0,change=1,temp=0,ch;
float x,y,r=100;
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi"); //path where ur BGI file is stored
strcpy(speed,"on 1");
outtextxy(200,20,"Working Fan");
outtextxy(150,50,"Use number 0 1 2 3 to change fan speed");
outtextxy(475,375,"Fan Speed");
fan:
do
{
cleardevice();
outtextxy(200,20,"Working Fan");
outtextxy(150,50,"Use number 0 1 2 3 to change fan speed");
outtextxy(475,375,"Fan Speed");
if(kbhit())
50
{
ch=getch();
if(ch=='0')
{
rectangle(495,395,535,410);
floodfill(515,405,15);
temp=1;
}
if(ch=='1')
{
strcpy(speed,"on 1");
change=1;
}
else if(ch=='2')
{
strcpy(speed,"on 2");
change=3;
}
else if(ch=='3')
{
strcpy(speed,"on 3");
change=18;
}
}
outtextxy(500,400,speed);
circle(320,240,(r/12));
circle(320,240,(r/6));
circle(320,240,(r/4));
x=r*(cos((PI * theta)/180));
y=r*sin((PI * theta)/180);
line(320+(x/4),240+(y/4),320+(1.6*x),230+(1.6*y)); /*draw the line for fan*/
line(320+(x/4),240+(y/4),320+(1.6*x),250+(1.6*y));
line(320+(1.6*x),230+(1.6*y),320+(1.6*x),250+(1.6*y));
x=r*cos((PI *(theta+120))/180);
y=r*sin((PI *(theta+120))/180);
51
line(320+(x/4),240+(y/4),320+(1.6*x),230+(1.6*y));
line(320+(x/4),240+(y/4),320+(1.6*x),250+(1.6*y));
line(320+(1.6*x),230+(1.6*y),320+(1.6*x),250+(1.6*y));
x=r*cos((PI * (theta+240))/180);
y=r*sin((PI * (theta+240))/180);
line(320+(x/4),240+(y/4),320+(1.6*x),230+(1.6*y));
line(320+(x/4),240+(y/4),320+(1.6*x),250+(1.6*y));
line(320+(1.6*x),230+(1.6*y),320+(1.6*x),250+(1.6*y));
if(temp!=1) delay(36/change);
else
{
ch=getche();
if(ch=='\r') exit(0);
if(ch=='0') temp=1;
if(ch=='1') //for speed
{
strcpy(speed,"on 1");
temp=0;
change=1;
}
else if(ch=='2')
{
strcpy(speed,"on 2");
temp=0;
change=3;
}
else if(ch=='3')
{
strcpy(speed,"on 3");
temp=0;
change=18;
}
else
{
strcpy(speed,"off!");
52
temp=1;
}
}
if(theta==360) theta=0;
theta++;
}
while(temp==0);
goto fan;
}
Output:
Viva Question:
1. What is graphics function
2. What is rectangle function
53
Experiment No. 18
Source Code
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main() {
int gd = DETECT, gm;
int i, maxx, midy;
/* Draw Car */
setcolor(YELLOW);
setfillstyle(SOLID_FILL, RED);
54
floodfill(5 + i, midy + 22, YELLOW);
setcolor(BLUE);
setfillstyle(SOLID_FILL, DARKGRAY);
/* Draw Wheels */
circle(30 + i, midy + 25, 9);
circle(90 + i, midy + 25, 9);
floodfill(30 + i, midy + 25, BLUE);
floodfill(90 + i, midy + 25, BLUE);
/* Add delay of 0.1 milli seconds */
delay(100);
}
getch();
closegraph();
return 0;
}
Output
Viva Question:
1. What is graphics function
2. What is rectangle function
55
Experiment No. 19
Source Code
#include<time.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x=10,y=200,x1=675,y1=380;
int stangle=35,endangle=140,radius=90;
initgraph(&gd,&gm,"C:/TC/BGI");
while(!kbhit())
{
cleardevice();
setbkcolor(BLACK);
if(x<640)
{
x+=5;
y+=1;
arc(x,y,stangle,endangle+35,radius);
arc(x,y-110,190,323,radius+2);
circle(x+40,y-60,5);
line(x-90,y-90,x-90,y-8);
}
else
{
x1-=5;
y1-=1;
arc(x1,y1,stangle-30,endangle+4,radius);
arc(x1,y1-110,217,350,radius+2);
circle(x1-40,y1-60,5);
line(x1+90,y1-90,x1+90,y1-10);
}
setcolor(YELLOW);
delay(90);
}
closegraph();
}
56
Result: Hence a moving fish is drawn
Viva Question:
1. What is graphics function
2. What is rectangle function
57