CGM updated
CGM updated
CGM updated
LAB MANUAL
Prepared By:
Curve Generation
10 58
13 VIVA Questions 86
14 Practice Exercises 94
Hardware and Software Requirements
1. Software Requirement:
Hardware Requirements:
Intel® Pentium® 4, Intel Centrino®, Intel Xeon®, or Intel Core™ Duo (or compatible)
processor
Microsoft® Windows® 7 (64 bit) or Windows 8 (64 bit)
4GB of RAM
2.5GB of available hard-disk space for installation; additional free space required
during installation (cannot install on removable flash storage devices)
1024x768 display (1280x800 recommended)
QuickTime 10.x software recommended
Basic Structure of a C-graphics program:
#include<stdio.h>
#include<conio.h>
void main()
getch();
}
Experiment – 1
Digital Differential Analyzer Algorithm
1 :Digital differential analyzer (DDA) is used for linear interpolation of variables over an interval
between given start, end points and for rasterization of lines, triangles and polygons. Using DDA
Algorithm, Write a C-Program to draw a line segment between two given points?
Aim: To implement DDA Algorithm for drawing a line segment between two given end points A
(x1, y1) and B(x2, y2).
#include<stdio.h>
#include<graphics.
h>
#include<math.h>
float round(float a);
void main()
{
int gd=DETECT,gm;
// gd=graphics driver (detects best graphics driver and assigns it as default, gm=graphics
mode
. int x1,y1,x2,y2,steps,k;
float
xincr,yincr,x,y,dx,dy;
printf("enter x1,y1");
scanf("%d%d",&x1,&y1);
printf("enter x2,y2");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"c:\\turboc3\\BGI");//initializes the
graph dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(d
y))
steps=abs(dx);
else
steps=abs(dy);
xincr=dx/step
s;
yincr=dy/step
s; x=x1;
y=y1;
for(k=1;k<=steps;k+
+)
{
delay(100);//for seeing the line drawing process
slowly. x+=xincr;
y+=yincr;
putpixel(round(x),round(y),WHIT
E);
}
outtextxy(200,20,"DDA"); // for printing text at desired screen
location. outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)
"); getch();
closegraph(); // closes the graph and comes back to previous graphic mode.
}
float round(float a)
{
int
b=a+0.5;
return b;
}
2
MODI INSTITUTE OF TECHNOLOGY CSE Department
Output:
2: Bresenham’s line algorithm is an algorithm which determines which order to form a close
approximation to a straight line between two given points. Write a C program for determining
pixel activation list between two given points in order to draw line segment using
bresenham’s Line drawing algorithm?
Aim: To implement Bresenham’s line drawing algorithm for drawing a line segment between two
given endpoints A (x1, y2) and B(x2, y2).
Description:
Basic Concept:
Move across the x axis in unit intervals and at each step choose
between two different y coordinates
For example, from position (2, 3) we have to choose between (3, 3) and (3,
4). We would like the point that is closer to the original line
1. Input the two line end-points, storing the left end-point in (x0, y0)
3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the
decision parameter as:
p0 2y 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:
p k 1 p k 2 y
p k 1 p k 2 y 2 x
5. Repeat step 4 (Δx – 1) times
NOTE: The algorithm and derivation above assumes slopes are less than 1. For other
slopes we need to adjust the algorithm slightly
#include<stdio.h>
#include<conio.h>
#include<graphics.
h> void main()
{
int
x,y,x1,y1,x2,y2,p,dx,dy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\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);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second point ::");
scanf("%d",&y2);
x=x1;
y=y1;
dx=x2-
x1;
dy=y2-
y1;
putpixel(x,y,2);
p=(2*dy-
dx);
while(x<=x
2)
{
if(p<0
)
{ x=x+1;
p=p+2*dy
;
}
else
{
x=x+1
;
y=y+1
;
p=p+(2*dy)-(2*dx);
}
putpixel(x,y,7);
}
getch();
closegraph(
);
}
6
MODI INSTITUTE OF TECHNOLOGY CSE Department
Output:
3: Using Midpoint circle generation algorithm which is a variant of Bresenham's line algorithm, write a
C- Program to generate pixel activation list for drawing a circle with a given centre of circle P(x,
y) and a radius r?
Aim: To implement midpoint circle generation algorithm or bresenham’s circle algorithm for drawing a circle
of given center (x, y) and radius r.
Description:
Circles have the property of being highly symmetrical, which is handy when it comes
to drawing them on a display screen.
We know that there are 360 degrees in a circle. First we see that a circle is symmetrical about
the x axis, so only the first 180 degrees need to be calculated.
Next we see that it's also symmetrical about the y axis, so now we only need to calculate the
first 90 degrees.
Finally we see that the circle is also symmetrical about the 45 degree diagonal axis, so we
only need to calculate the first 45 degrees.
We only need to calculate the values on the border of the circle in the first octant. The other
values may be determined by symmetry.
Bresenham's circle algorithm calculates the locations of the pixels in the first 45 degrees. It
assumes that the circle is centered on the origin. So for every pixel (x, y) it calculates, we draw a
pixel in each of the eight octants of the circle. This is done till when the value of the y coordinate
equals the x coordinate. The pixel positions for determining symmetry are given in the below
algorithm.
So we use decision
parameter here to
8 decide.
1. Input radius r and circle centre (xc, yc), then set the coordinates for the first point on
the circumference of a circle centred on the origin as:
( x 0 , y 0 ) ( 0 , r )
2. Calculate the initial value of the decision parameter as:
p 5 r
0
4
3. Starting with k = 0 at each position xk, perform the following test. If pk < 0, the next
point along the circle centred on (0, 0) is (xk+1, yk) and:
p k 1 p k 2 x k 1 1
Otherwise the next point along the circle is (xk+1, yk-1) and:
p k 1 p k 2 x k 1 1 2 y k 1
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x, y) onto the circular path centred at (xc, yc) to
plot the coordinate values:
6. Repeat steps 3 to 5 until x >= y
x x xc yyy c
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.
h>
void draw_circle(int,int,int);
void
symmetry(int,int,int,int);
void main()
{
int xc,yc,R;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI
"); printf("Enter the center of the
circle:\n"); printf("Xc =");
scanf("%d",&xc);
printf("Yc =");
scanf("%d",&yc);
printf("Enter the radius of the circle
:"); scanf("%d",&R);
draw_circle(xc,yc,
R); getch();
closegraph();
}
10
Output:
11
4: Using Midpoint ellipse generation algorithm which is a variant of Bresenham's line algorithm, write
a C- Program to generate pixel activation list for drawing a ellipse?
Aim: To implement the Ellipse Generation Algorithm for drawing an ellipse of given center(x, y) and radius rx
and ry.
Description:
Thus, we must calculate pixel positions along the elliptical arc through one quadrant and then we
obtain positions in the remaining 3 quadrants by symmetry
The next pixel is chosen based on the decision parameter. The required conditions are given in
following algorithm.
12
1. Input rx, ry, and ellipse center (xc, yc), and obtain the first point on an ellipse centered
on the origin as
p1 r 2 r 2 r 1
r2
0 y x y 4 x
3. At each xi position, starting at i = 0, if p1i < 0, the next point along the ellipse centered on (0,
0) is (xi + 1, yi)
p1 p1 2 r 2 x r 2
and
i 1 i y i 1 y
p1 p1 2 r 2 x 2 r2 y r 2
i 1 i y i 1 x i 1 y
and continue 2r 2
x 2r 2
y
until
y x
4. (x0, y0) is the last position calculated in region 1. Calculate the initial parameter in region 2 as
p 2 0 r y2 ( x0 12 ) 2 rx2 ( y0 1) 2 rx2 ry 2
5. At each yi position, starting at i = 0, if p2i > 0, the next point along the ellipse centered on (0,
0) is (xi, yi – 1) and
p2 p 2 2 r2 y r2
i 1 i x i 1 x
p 2 i 1 p 2 i 2 ry x 2 rx y r2
2 2
Otherwise, the next point is (xi + 1, yi – 1) i 1 i 1
and x
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 the coordinate values
x = x + xc , y = y + yc
13
else
{ (2.0*rx*rx*y); x--;
14
Output:
15
MODI INSTITUTE OF TECHNOLOGY CSE Department
Experiment- 5
Creating various types of texts and fonts
5: Using different graphics functions available for text formatting in C-Language, Write a C
program for displaying text in different sizes, different colors, different font styles?
Aim: To write a C-program for displaying text in different sizes, different colors and different font
styles by using graphics functions.
Description:
Settextstyle():Settextstyle function is used to change the way in which text appears, using it we can
modify the size of text, change direction of text and change the font of text.
Eg: settextstyle(font,direction,charsize);--settextstyle(TRIPLEX_FONT,HORIZ_DIR,2);
Font –font style- it may be font name or integer value from 0- 9.
DEFAULT_FONT,
TRIPLEX_FONT, 1. Horizontal direction(HORIZ_DIR or
SMALL_FONT, 0)
SANS_SERIF_FO 2. Vertical direction(VERT_DIR or 1)
NT,
GOTHIC_FONT,
SCRIPT_FONT,
SIMPLEX_FONT,
TRIPLEX_SCR_F
ONT,
COMPLEX_FONT
,
EUROPEAN_FON
T, BOLD_FONT
16
17
MODI INSTITUTE OF TECHNOLOGY CSE Department
Experiment- 6
Creating two dimensional objects
6: Using certain graphic functions available in C-language for drawing lines, rectangles & circles,
Write a C- Program which generates pixel activation list for drawing the following simple two
dimensional objects (Circle, Ellipse…..).
Aim: To write a C-program for creating simple two dimensional shape of house, car, fish, man
using lines, circles etc.
Description:
The following graphics functions are available for creating two dimensional shapes in C.
line
circle
ellipse
rectangl
e
drawpol
y
line: line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and
(x2,y2) are end points of the line.
Declaration: - line(x1, y1, x2, y2); ---line (100,200,300,400);
Circle: Circle function is used to draw a circle with center (x, y) and third parameter specifies
the radius of the circle.
Declaration: circle(x, y, r)—circle (100, 200, 25) ;( 25 is radius of circle, (100,100) is
center of circle).
Ellipse: Ellipse is used to draw an ellipse (x, y) are coordinates of center of the ellipse,
startangle is the starting angle, endangle is the ending angle, and fifth and sixth
parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse
strangles and end angle should be 0 and 360 respectively.
Usage: ellipse(x, y, startangle, endangle, xradius, yradius);--
ellipse(100,200,0,360,25,45);((100,200) is center of ellipse, 0 is start angle, 360 is end
angle, 25 is x-axis radius, 45 is radius circle).
Rectangle: rectangle function is used to draw a rectangle. Coordinates of left top and right
bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left
corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of
right bottom corner, bottom specifies the Y-coordinate of right bottom corner.
Syntax: rectangle(left,top,right,bottom);--rectangle(100,200,300,400);
18
num+1. Example: we will draw a triangle using drawpoly, consider for example the array :-
int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
points array contains coordinates of triangle which are (320, 150), (420, 300) and (250,
300). Note that last point(320, 150) in array is same as first.
Number of vertices are denoted by num. for any polygon, number of vertices are
(num+1). For triangle, number of vertices are 4.
#include<stdio.h>
#include<conio.h>
#include<graphics.
h> void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BG
I"); setcolor(5);
rectangle(60,80,150,200);
rectangle(95,140,120,200);
line(60,80,100,15);
line(100,15,150,80);
circle(100,60,10
); getch();
closegraph();
}
Output:
19
#include<stdio.h>
#include<conio.h>
#include<graphics.
h>
#include<dos.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,
"C:\\TurboC3\\BGI"); cleardevice();
line( 150, 100, 242, 100);
ellipse(242, 105, 0, 90, 10, 5);
line(150, 100, 120, 150);
line(252, 105, 280, 150);
line(100, 150, 320, 150);
line(100, 150, 100, 200);
line(320, 150, 320, 200);
line(100, 200, 110, 200);
line( 320, 200, 310, 200);
arc(130, 200, 0, 180, 20);
arc( 290, 200, 0, 180, 20);
line( 270, 200, 150, 200);
circle(130, 200, 17);
circle(290, 200, 17);
getch();
}
Output:
20
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<graphics.
h>
#include<ctype.h>
void
main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI
"); cleardevice();
ellipse(520,200,30,330,90,30);
circle(450,193,3);
line(430,200,450,200);
line(597,185,630,170);
line(597,215,630,227);
line(630,170,630,227);
line(597,200,630,200);
line(597,192,630,187);
line(597,207,630,213);
line(500,190,540,150);
line(530,190,540,150);
getch();
}
Output:
21
#include<stdio.h>
#include<graphics.
h>
#include<conio.h>
void main()
{ int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI
"); setcolor(9);
circle(150,150,35);
line(150,185,150,300);
line(150,200,120,230);
line(150,200,180,230);
line(150,300,120,330);
line(150,300,180,330);
outtextxy(230,350,"HI, This is Computer Graphics");
getch();
Output:
22
Description:
Using drawpoly (), we can draw any polygon of any number of vertices.
Syntax: drawpoly (num, points);--num indicates number of vertices of polygon. Num = num+1.
Example: we will draw a triangle using drawpoly, consider for example the
array:- int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
1) Points array contains coordinates of triangle which are (320, 150), (420, 300) and (250,
300). Note that last point (320, 150) in array is same as first.
2) Number of vertices is denoted by num. for any polygon, numbers of vertices are
(num+1). For triangle, number of vertices are 4.
Program:
#include
<graphics.h>
#include <conio.h>
main(
)
{ int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
drawpoly(4, points);
getch();
closegraph(
); return 0;
}
Output:
23
7: Write a C-program for performing the basic 2D transformations such as translation, Scaling,
Rotation, shearing and reflection for a given 2D object?
Aim: To apply the basic 2D transformations such as translation, Scaling, Rotation, shearing and
reflection for a given 2D object.
1. Translation
2. Scaling
3. Rotation
4. Reflection
5. Shear
1. Translation: Translation is defined as moving the object from one position to another position
along straight line path.
We can move the objects based on translation distances along x and y axis. tx denotes translation
distance along x-axis and ty denotes translation distance along y axis.
Translation Distance: It is nothing but by how much units we should shift the object from one location
to another along x, y-axis.
Consider (x,y) are old coordinates of a point. Then the new coordinates of that same point (x’,y’) can be
obtained as follows:
X’=x+tx
Y’=y+ty
We denote translation transformation as P. we express above equations in matrix form as:
x,y---old coordinates
x’,y’—new coordinates
after translation
tx,ty—translation distances, T is
24
If (x, y) are old coordinates of object, then new coordinates of object after applying
scaling transformation are obtained as:
x’=x*sx
y’=y*sy.
sx and sy are scaling factors along x-axis and y-axis. we express the above equations in matrix form as:
x sx 0 x
y 0 s y
y
Scaling Matrix
3. Rotation: A rotation repositions all points in an object along a circular path in the plane
centered at the pivot point. We rotate an object by an angle theta.
25
5. Shear:
1. Shear is the translation along an axis by an amount that increases linearly with another
axis (Y). It produces shape distortions as if objects were composed of layers that are
caused to slide over each other.
2. Shear transformations are very useful in creating italic letters and slanted letters from
regular letters.
3. Shear transformation changes the shape of the object to a slant position.
x’=x+shx*y
y’=y
b.Y-shear: changing y coordinates value and keeping x constant
x’=x
y’=y+shy*x
shx and shy are shear factors along x and y-axis.
26
#include<stdio.h>
#include<conio.h>
#include<graphics.
h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,tx,ty,x3,y3,x4,y4;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the starting point of line
segment:"); scanf("%d %d",&x1,&y1);
printf("Enter the ending point of line
segment:"); scanf("%d %d",&x2,&y2);
printf("Enter translation distances
tx,ty:\n"); scanf("%d%d",&tx,&ty);
setcolor(5);
line(x1,y1,x2,y2
);
outtextxy(x2+2,y2+2,"Original
line"); x3=x1+tx;
y3=y1+ty;
x4=x2+tx;
y4=y2+ty;
setcolor(7);
line(x3,y3,x4,y4
);
outtextxy(x4+2,y4+2,"Line after translation");
getch();
}
Output:
27
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after
scaling"); getch();
}
Output:
28
#include<stdio.h>
#include<conio.h>
#include<graphics.
h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float x1,y1,x2,y2,x3,y3,x4,y4,a,t;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter coordinates of starting point:\n");
scanf("%f%f",&x1,&y1);
printf("Enter coordinates of ending point\n");
scanf("%f%f",&x2,&y2);
printf("Enter angle for
rotation\n"); scanf("%f",&a);
setcolor(5);
line(x1,y1,x2,y2
);
outtextxy(x2+2,y2+2,"Original
line"); t=a*(3.14/180);
x3=(x1*cos(t))-(y1*sin(t));
y3=(x1*sin(t))+(y1*cos(t));
x4=(x2*cos(t))-(y2*sin(t));
y4=(x2*sin(t))+(y2*cos(t
)); setcolor(7);
line(x3,y3,x4,y4);
outtextxy(x3+2,y3+2,"Line after rotation");
} getch();
Output:
29
# include
<stdio.h> #
include <conio.h>
# include
<graphics.h> #
include <math.h>
char IncFlag;
int PolygonPoints[3][2] ={{10,100},{110,100},{110,200}};
void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,24
0);
line(320,0,320,48
0);
for (iCnt=0; iCnt<3; iCnt++)
{line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%3][1]);
}
}
void Reflect()
{float Angle; int iCnt;
int Tx,Ty;
printf("endl");;
for (iCnt=0; iCnt<3; iCnt++)
PolygonPoints[iCnt][1] = (480 - PolygonPoints[iCnt][1]);
}
void main()
{int gDriver = DETECT, gMode; int iCnt;
initgraph(&gDriver, &gMode,
"C:\\TurboC3\\BGI"); for (iCnt=0; iCnt<3;
iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine(
); getch();
Reflect();
PolyLine(
); getch();
30
Output:
Object before reflection about x-axis
31
# include
<stdio.h> #
include <conio.h>
# include
<graphics.h> #
include <math.h>
char IncFlag;
int PolygonPoints[3][2] =
{{10,100},{110,100},{110,200}};
void PolyLine()
{
int iCnt;
cleardevice();
line(0,240,640,24
0);
line(320,0,320,48
0);
for (iCnt=0; iCnt<3; iCnt++)
{
line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1],
PolygonPoints[(iCnt+1)%3][0],PolygonPoints[(iCnt+1)%
3][1]);
}
}
void Reflect()
{
float
Angle; int
iCnt;
int Tx,Ty;
void main()
{
int gd = DETECT,
gm; int iCnt;
initgraph(&gd, &gm,
"C:\\TurboC3\\BGI"); for (iCnt=0;
iCnt<3; iCnt++)
{
PolygonPoints[iCnt][0] += 320;
PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1];
}
PolyLine(
); getch();
Reflect();
PolyLine(
); getch();
}
32
MODI INSTITUTE OF TECHNOLOGY CSE Department
Output:
Object before reflection:
33
void main()
{
int
gd=DETECT,gm;
float shx,shy;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter shear factor shx along x-axis
:"); scanf("%f",&shx);
line(100,0,200,0);
line(200,0,200,200);
line(200,200,100,200);
line(100,200,100,0);
printf("X-
shear");
setcolor(12);
line((100+(0*shx)),0,(200+(0*shx)),0);
line((200+(0*shx)),0,(200+(200*shx)),200);
line((200+(200*shx)),200,(100+(200*shx)),200);
line((100+(200*shx)),200,(100+(0*shx)),0);
getch();
}
Output:
In above output, red lined rectangle denotes object after x-shear transformation.
34
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.
h> void main()
{
int gd=DETECT,gm;
float shx,shy;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter shear factor shy along y-axis
:"); scanf("%f",­);
line(100,10,200,10);
line(200,10,200,200);
line(200,200,100,200);
line(100,200,100,10);
printf("Y-
shear");
setcolor(12);
line(100,10+(shy*100),200,10+(shy*200));
line(200,10+(shy*200),200,200+(shy*200));
line(200,200+(shy*200),100,200+(shy*100));
line(100,200+(shy*100),100,10+(shy*100));
getch();
} closegraph(
Output: );
35
Note: For filling a given picture or object with color’s, we can do it in two ways in C programming. The
two ways are given below:
i. Using filling algorithms such as Floodfill algorithm, Boundary fill algorithm and scanline
polygon fill algorithm, we can color the objects.
ii. Using inbuilt graphics functions such as floodfill(),setfillstyle() we can fill the object with color’s directly
without using any filling algorithm.
8-i: By using the concept of flood fill algorithm, Write a C- program for filling a given rectangle object
with color?
Aim: To implement flood fill algorithm for filling a rectangle with given color.
Description:
Sometimes we want to fill in (recolor) an area that is not defined within a single color boundary.
We paint such areas by replacing a specified interior color instead of searching for a boundary
color value. This approach is called a flood-fill algorithm.
1. We start from a specified interior pixel (x, y) and reassign all pixel values that are currently
set to a given interior color with the desired fill color.
2. If the area has more than one interior color, we can first reassign pixel values so that all
interior pixels have the same color.
3. Using either 4-connected or 8-connected approach, we then step through pixel positions
until all interior pixels have been repainted.
36
4- connected approach:
2. First all the pixels should be reassigned to common color. here common color is black.
4. If above condition is satisfied, then following 4 steps are followed in filling the object.
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_co
l); flood(x-
1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_co
l); flood(x,y-
1,fill_col,old_col);
8- connected approach:
2. First all the pixels should be reassigned to common color. here common color is black.
4. If above condition is satisfied, then following 8 steps are followed in filling the object.
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);
flood(x + 1, y - 1, fill_col,
old_col); flood(x + 1, y + 1,
fill_col, old_col); flood(x - 1, y -
1, fill_col, old_col); flood(x - 1,
y + 1, fill_col, old_col);
37
#include<stdio.h>
#include<conio.h>
#include<graphics>
#include<dos.h>
void
flood(int,int,int,int);
void main()
{int gd,gm=DETECT; clrscr(); detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI
"); rectangle(50,50,100,100);
flood(55,55,9,0);
getch();
}
void flood(int x,int y, int fill_col, int old_col)
{
if(getpixel(x,y)==old_col)
{
delay(10);
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_co
l); flood(x-
1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_co
} l); flood(x,y-
} 1,fill_col,old_col);
Output:
38
#include<stdio.h>
#include<conio.h>
#include<graphics.
h>
#include<dos.h>
void flood(int,int,int,int);
void main()
{
int gd,gm=DETECT;
clrscr();
detectgraph(&gd,&gm
);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI
"); rectangle(50,50,100,100);
flood(55,55,12,0);
getch();
}
void flood(int x,int y, int fill_col, int old_col)
{
if(getpixel(x,y)==old_col)
{
delay(10);
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_co
l); flood(x-
1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_co
l); flood(x,y-
1,fill_col,old_col);
flood(x + 1, y - 1, fill_col,
old_col); flood(x + 1, y + 1,
} fill_col, old_col); flood(x - 1, y -
} 1, fill_col, old_col); flood(x - 1,
Output: y + 1, fill_col, old_col);
39
Aim: To implement Boundary fill algorithm for filling a rectangle with given color.
Description:
Start at a point inside a region and paint the interior outward toward the boundary.
If the boundary is specified in a single color, the fill algorithm processed
outward pixel by pixel until the boundary color is encountered.
A boundary-fill procedure accepts as input the coordinate of the interior point
(x, y), a fill color, and a boundary color.
Algorithm:
The following steps illustrate the idea of the recursive boundary-fill algorithm:
1. Start from an interior point.
2. If the current pixel is not already filled and if it is not an edge point, then set the pixel with the
fill color, and store its neighboring pixels (4 or 8-connected). Store only neighboring pixel that is
not already filled and is not an edge point.
3. Select the next pixel from the stack, and continue with step 2.
40
#include<stdio.h>
#include<conio.h>
#include<graphics.
h>
#include<dos.h>
void boundary_fill(int x, int y, int fcolor, int bcolor)
{
if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor))
{ delay(10);
putpixel(x, y, fcolor);
boundary_fill(x + 1, y, fcolor,
bcolor); boundary_fill(x - 1, y,
fcolor, bcolor); boundary_fill(x, y
+ 1, fcolor, bcolor);
boundary_fill(x, y - 1, fcolor,
bcolor);
}
}
void main()
{
int x, y, fcolor,
bcolor; int
gd=DETECT,gm;
initgraph(&gd, &gm,
"C:\\TurboC3\\BGI"); printf("Enter the
seed point (x,y) : "); scanf("%d%d", &x,
&y);
printf("Enter boundary color :
"); scanf("%d", &bcolor);
printf("Enter new color : ");
scanf("%d", &fcolor);
circle(100,200,45);
} boundary_fill(x,y,fcolor,bcolor)
Output: ; getch();
41
}
}
void main()
{
int x, y, fcolor,
bcolor; int
gd=DETECT,gm;
initgraph(&gd, &gm,
"C:\\TurboC3\\BGI"); printf("Enter the
seed point (x,y) : "); scanf("%d%d", &x,
&y);
printf("Enter boundary color :
"); scanf("%d", &bcolor);
printf("Enter new color : ");
scanf("%d", &fcolor);
rectangle(50,50,100,100);
boundary_fill(x,y,fcolor,bcolor)
; getch();
}
Output:
42
MODI INSTITUTE OF TECHNOLOGY CSE Department
8- i: By using the concept of Scan line polygon fill algorithm, write a C- program for filling a given object
with color?
Aim: To implement the Scan line polygon fill algorithm for coloring a given object.
Description:
Find the intersections of the scan line with all edges of the polygon
Sort the intersections by increasing x coordinate
Fill in all pixels between pairs of intersections that lie interior to the polygon
Process involved:
The scan-line polygon-filling algorithm involves
• the horizontal scanning of the polygon from its lowermost to its topmost vertex,
• identifying which edges intersect the scan-line,
• and finally drawing the interior horizontal lines with the specified fill color process.
Algorithm Steps:
1. the horizontal scanning of the polygon from its lowermost to its topmost vertex
2. identify the edge intersections of scan line with polygon
3. Build the edge table
a. Each entry in the table for a particular scan line contains the maximum y
value for that edge, the x-intercept value (at the lower vertex) for the edge,
and the inverse slope of the edge.
4. Determine whether any edges need to be splitted or not. If there is need to split,
split the edges.
5. Add new edges and build modified edge table.
6. Build Active edge table for each scan line and fill the polygon based on
intersection of scanline with polygon edges.
43
44
MODI INSTITUTE OF TECHNOLOGY CSE Department
{
if( ((a[i][1]<=y)&&(a[i+1][1]>y))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-
a[i][1])); k++;
}
}
for(j=0;j<k-1;j++) /*- Arrange x-intersections in
order -*/ for(i=0;i<k-1;i++)
{
if(xi[i]>xi[i+1])
{
temp=xi[i];
xi[i]=xi[i+1];
xi[i+1]=temp
;
}
}
setcolor(3);
for(i=0;i<k;i+=
2)
{
line(xi[i],y,xi[i+1]+1,y
); getch();
}
}
}
Output:
Filled
polygo
n
45
Aim: To fill a given object with colors by using inbuilt graphics functions.
Description:
For filling a given object with colors, we have the following graphics functions.
1. Fillpoly
2. Floodfill
3. Setfillstyle
4. Setcolor
5. fillellipse
Fillpoly: Fillpoly function is used to draw a polygon of any number of vertices and fills that polygon
with colors. It requires same arguments as drawpoly function. fillpoly fills the polygon by using
current fill pattern and color which can be changed using setfillstyle function.
46
#include<graphics.
h>
#include<conio.h>
main(
)
{ int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
setcolor(RED);
circle(100,100,50);
floodfill(100,100,RE
D); getch();
closegraph(
); return 0;
}
Output:
#include<graphics.
h>
#include<conio.h>
main(
)
{ int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
circle(100,100,50); /* drawn in white color
*/ setcolor(GREEN);
circle(200,200,50); /* drawn in green color */
getch();
closegraph(
); return 0;
Output:
49
#include<graphics.
h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
fillellipse(100, 100, 50, 25);
getch();
closegraph(
); return 0;
}
Output:
50
9: Write a C-program for performing the basic transformations such as translation, Scaling,
Rotation for a given 3D object?
Aim: To apply the basic transformations such as translation, Scaling, Rotation for a given 3D object.
51
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h
>
#include<graphics.
h>
int
x1,x2,y1,y2,mx,my,depth;
void draw();
void
trans();
void main()
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n\t\t3D Translation\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-
x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice(
); trans();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void trans()
{
int a1,a2,b1,b2,dep,x,y;
printf("\n Enter the Translation
Distances:"); scanf("%d%d",&x,&y);
a1=x1+x;
a2=x2+x;
b1=y1+y;
b2=y2+y;
dep=(a2-
a1)/4;
bar3d(a1,b1,a2,b2,dep,
1); setcolor(5);
draw();
}
52
MODI INSTITUTE OF TECHNOLOGY CSE Department
Output for Translation:
53
int gd=DETECT,gm,c;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI")
; printf("\n\t\t3D Scaling\n\n");
printf("\nEnter 1st top value(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter right bottom value(x2,y2):");
scanf("%d%d",&x2,&y2);
depth=(x2-x1)/4;
mx=(x1+x2)/2;
my=(y1+y2)/2;
draw();
getch();
cleardevice(
); scale();
getch();
}
void draw()
{
bar3d(x1,y1,x2,y2,depth,1);
}
void scale()
{
int x,y,a1,a2,b1,b2,dep;
printf("\n\n Enter scaling
Factors:"); scanf("%d%d",&x,&y);
a1=mx+(x1-mx)*x;
a2=mx+(x2-mx)*x;
b1=my+(y1-my)*y;
b2=my+(y2-my)*y;
dep=(a2-a1)/4;
bar3d(a1,b1,a2,b2,dep,
1); setcolor(5);
draw();
}
54
55
56
MODI INSTITUTE OF TECHNOLOGY CSE Department
bar3d(a1,b1,a2,b2,dep,1); setcolor(5);
}
Output:
57
10: Write a C-program for generating a curve for a given set of control points?
Aim: To generate a smooth curve by using Bezier curve technique for a given set of control points.
Description:
A Bézier curve is a parametric curve frequently used in computer graphics and related fields.
Generalizations of Bézier curves to higher dimensions are called Bézier surfaces, of which the
In vector graphics, Bézier curves are used to model smooth curves that can be scaled indefinitely.
"Paths," as they are commonly referred to in image manipulation programs are combinations of
linked Bézier curves. Paths are not bound by the limits of rasterized images and are intuitive to
modify. Bézier curves are also used in animation as a tool to control motion
Four points P0, P1, P2 and P3 in the plane or in higher-dimensional space define a cubic Bézier
curve. The curve starts at P0 going toward P1 and arrives at P3 coming from the direction of P2.
Usually, it will not pass through P1 or P2; these points are only there to provide directional
information. The distance between P0 and P1 determines "how long" the curve moves into direction
58
#include <stdio.h>
#include <stdlib.h>
#include
<graphics.h>
#include <math.h>
void bezier (int x[4], int y[4])
{
int gd = DETECT,
gm; int i;
double t;
initgraph (&gd, &gm,
"C:\\TurboC3\\BGI"); for (t = 0.0; t <
1.0; t += 0.0005)
{
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) *
x[1] + 3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3)
* x[3];
void main()
{
int x[4], y[4];
int i;
printf ("Enter the x- and y-coordinates of the four control
points.\n"); for (i=0; i<4; i++)
scanf ("%d%d", &x[i],
&y[i]); bezier (x, y);
}
59
60
Aim: To develop programs for making simple animations using transformations like rotation,
scaling and translation. The simple animations are given below:
Suppose if we want to move a circle from left to right means, we have to shift the
The below programs illustrate the movement of objects by using for loop and also using
61
getch();
}
62
63
64
MODI INSTITUTE OF TECHNOLOGY CSE Department
getch();
}
output for man moving:
65
66
67
void
main()
{ int gd = DETECT, gm = DETECT, c = -200, i = 0, x = 40, l = 15, h = 15, ht = 0;
initgraph(&gd, &gm,
"C:\\TurboC3\\BGI"); cleardevice();
setcolor(BROWN);
line(0, 201, 600,
201);
cont:
while (!kbhit())
{
setcolor(4);
ellipse(x, 100, 0, 180, 50, 30);
line(x - 50, 100, x + 50, 100);
line(x, 100, x, 150);
circle(x - 20, 115, 15);
line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);
line(x - 20, 140, x, 150);
line(x - 20, 140, x - 20 - h, 160);
setcolor(0)
;
delay(50);
ellipse(x, 100, 0, 180, 50, 30);
line(x - 50, 100, x + 50, 100);
line(x, 100, x, 150);
circle(x - 20, 115, 15);
line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);
line(x - 20, 140, x, 150);
line(x - 20, 140, x - 20 - h, 160);
line(x + 50, 100, x + 50,
200); x++;
l--;
if (l == -
15) l =
15;
if (ht ==
1) h++;
else
h--;
if (h ==
15) ht =
0;
else if (h == -
15) ht = 1;
68
MODI INSTITUTE OF TECHNOLOGY CSE Department
}
if (getch() == ' ') {
while
(!kbhit());
getch();
goto cont;
}
}
Output:
69
70
Output:
71
Adobe Flash (formerly called "Macromedia Flash") is a multimedia and software platform used for
authoring of vector graphics, animation, games and Rich Internet Applications (RIAs) which can
be viewed, played and executed in Adobe Flash Player. Flash is frequently used to add
streamed video or audio players, advertisement and interactive multimedia content to web
pages, although usage of Flash on websites is declining.
FLASH- a scripting language….it is a action script..it is used to add interactivity to animation& to build web
app’s which uses information created in flash..
Flash Workspace:
75
*To specify the Stage size in pixels, enter values in the Width and Height boxes. The
minimum size is 1 x 1 pixels; the maximum is 2880 x 2880 pixels.
*To minimize document size, click the Contents button to the right of Match (but first create
all of the objects on the Stage).
*To set the Stage size to the maximum available print area, click Printer
Tools Panel:
76
*Click the panel menu to view additional options for the current panel.
*You can hide or show panels by using the options on the Window menu
You can organize and control the content of a movie over timeline.
77
Frames: Like films, Flash movies divide lengths of time into frames, which are organized on
the Timeline.
Keyframes: Frames that define a change in what is displayed in a movie or include frame
actions to modify a movie. When you open a new blank movie document, it contains one
layer with one blank keyframe.
78
*Layers are like multiple film strips stacked on top of each other, each with a different
element that appears on the Stage.
*Graphics
*Animations
*Text
*Sounds
*Buttons
*Frame actions
79
*Symbols are elements you reuse within a movie to reduce file size.
*Types of symbols include graphics, buttons, movie clips, sound files, and text.
*When you drag a symbol from a library to the Stage, you create an instance of the symbol.
*Easy editing: If you change the symbol in the library, all instances of the symbol are
updated automatically.
*Smaller file sizes: Symbols are downloaded only once, regardless of the number of instances you’ve
included in the movie. This reduces the size of your published movies and decreases download times
80
*You can set the beginning and ending frames and have Flash automatically create the
frames in between.
Motion tweening.
In Flash, a shape is a vector-based object. You create a shape by using the drawing tools or
Use shape tweening to animate one shape into another. You cannot shape-tween grouped
objects, bitmaps, text that has not been broken apart, or symbols.
ACTION SCRIPT:
Frames: ActionScript attached to a frame is triggered when the movie plays that frame.
Objects: ActionScript attached to an object is triggered when the viewer interacts with the
81
The following steps show how to create a shape tween from frame 1 to frame 30 of the
Timeline. However, you can create tweens in any part of the Timeline that you choose.
82
1. Create a new Flash document and name it something like Motion Tween
Bounce. Flash creates a document with one layer and a keyframe at
Frame 1 by default.
2. In the Timeline, select Frame 1.
3. In the Toolbar, select the oval tool; set the Line Color to None.
4. Near the top of the Stage, draw a circle.
This circle will be the ball. Make it fairly large.
5. With Frame 1 still selected, from the Insert menu, choose Create Motion Tween
(Choose Insert > Create Motion Tween.
Flash creates a symbol from the objects on the Stage. Flash gives the symbol a
default name based on the number of tweening objects already created in the movie.
In this case, Flash turns the ball into a symbol named Tween 1
The Create Motion Tween command turns an editable object on the Stage in the
selected frame into a symbol and names the symbol Tween 1, Tween 2, and so on.
6. In the Timeline, select Frame 5.
7. Choose Insert > Frame.
Flash adds frames containing a dotted line. The dotted line indicates that these
frames are set to contain a motion tween but something is wrong and Flash cannot
complete the tween. In this case, the keyframe that describes where the ball should
be at the end of this animation sequence is missing.
Adding frames to the motion tween results in a temporarily broken tween (this
dashed line in the Timeline indicates this).
8. In Frame 5, move the circle to the bottom of the Stage to create the downward
bounce of the ball.
Flash creates a keyframe in Frame 5 with the symbol located at the bottom of the
Stage. Flash then updates the Timeline to give you information about the tween. In
the in-between frames that contain the motion tween, Flash replaces the dotted line
with an arrow, indicating that tweening takes place in these frames. These in-
between frames are still empty, but they no longer display the content of the previous
keyframe—they display the incrementally changed content Flash creates.
Once you have created a motion tween over a range of frames, repositioning the
content of a frame causes Flash to create a new keyframe in the current frame and
complete the tween.
9. In the Timeline, select Frame 10.
10. Choose Insert > Frame.
Flash extends the motion-tween tinting to Frame 10. A dotted line indicating an
incomplete tween appears in frames 6 through 10.
11. In Frame 10, move the circle to the top of the Stage to create the upward bounce of
the ball. Flash creates a new keyframe to contain the changed content and puts the
tweening arrow over the in-between frames.
Adding frames to the end of a motion tween extends the tween. Repositioning the
ball in the last frame of the tween completes the tween. Flash creates a new
keyframe for the repositioned ball.
12. From the Control menu, choose Play to preview the animation.
You've created another version of the simple bouncing ball. As in the frame-by-frame
animation you created in Chapter 8, you created new content for just three frames,
yet this tweened animation is much smoother than the three-keyframe animation you
created with the frame-by-frame technique. That's because you've actually created a
ten-frame animation; you're just letting Flash do the work of repositioning the ball on
the in-between frames.
83
1. Create a new Flash document and make sure to select ActionScript 3.0. The Bone tool will only
work with AS 3.0 documents
2. Draw an object on the Stage. For this example, I kept it simple and used the Rectangle tool to
create a basic shape.
3. Once you are done creating your shape, convert it to a Movie Clip or a Graphic symbol.
4. Since you'll need more than one object to create a chain of linked objects, duplicate the symbol
by holding down the Alt (Windows) or Option (Mac OS) key and dragging the symbol to a new
location. Flash will duplicate the instance every time you click and drag it. Repeat this procedure a
few more times to create multiple instances of the same symbol
5. Link all of these objects together to create your armature. Select the Bone tool (X) from the Tools panel
Bone tool
6. Decide what will be your parent or root symbol instance in the armature. This is the symbol
instance to which you will apply the first bone segment. Then drag to the next symbol instance to
link them together. When you release the mouse, a solid bone segment will appear between the
symbol instances
7. Repeat this procedure to link the second symbol instance to the third instance. Continue
dragging from one symbol to the next until you have linked all symbol instances with bones
8. The next step is the fun part. Select the Selection tool from the Tools panel (V) and drag the last
bone in your chain. The entire armature can now be manipulated in real time as you drag the last
bone around the Stage
84
85
Define pixel.
Pixel is shortened forms of picture element. Each screen point is referred to as pixel or pel.
DDA algorithm?
Digital differential analyzer is a scan conversion line algorithm based on calculating either dx or
dy at unit intervals in one coordinate and determine corresponding integer values nearest to the
line path for the other coordinate.
Merit:
1. It is a faster method for calculating pixel positions than direct use.
2. It eliminates multiplication by making use of raster characteristics.
De-merits:
1. It is time consuming
2. It is difficult to perform rounding and arithmetic operations.
86
Properties of circle
1. Circle type
2. Width
3. Color
4. dot-dash patterns
5. Pen or brush options
Attribute primitive
The parameter that affects the way a primitive is to be displayed is referred to as attribute
parameters. Eg. Color, size that determine fundamental characteristics of a primitive.
Attributes of a line
1. Line type
2. Line width
3. Pen and brush options
4. Line color
What is PHIGS?
PHIGS is Programmers hierarchical interactive graphics standard. It is the second software
standard to be developed and approved by standard organization. It is an extension of graphical
kernel system (GKS). It increases capabilities for object modeling, color specifications, surface
rendering and picture manipulations.
How will you specify the line width for lines with |m| < 1?
For lines with slope magnitude less than 1, we can modify a line drawing routine to display thick
lines by plotting a vertical span of pixels at each x position along the line. The no. of pixels in each
span is set equal to the integer magnitude of parameter lw.
87
What is Transformation?
Transformation is the process of introducing changes in the shape size and orientation of the object
using scaling rotation reflection shearing & translation etc.
What is translation?
Translation is the process of changing the position of an object in a straight-line path from one coordinate
location to another.
What is rotation?
A 2-D rotation is done by repositioning the coordinates along a circular path, in the x-y plane by making
an angle with the axes.
What is scaling?
The scaling transformations changes the shape of an object and can be carried out by multiplying each
vertex (x,y) by scaling factor Sx,Sy where Sx is the scaling factor of x and Sy is the scaling factor of y.
What is shearing?
The shearing transformation actually slants the object along the X direction or the Y direction as
required.ie; this transformation slants the shape of an object along a required plane.
What is reflection?
The reflection is actually the transformation that produces a mirror image of an object. For this use
some angles and lines of reflection.
88
Define clipping.
Clipping is the method of cutting a graphics display to neatly fit a predefined graphics region or the view port.
What are the various representation schemes used in three dimensional objects?
Boundary representation (B-res) – describe the 3 dimensional object as a set of surfaces that separate
the object interior from the environment.
Space- portioning representation – describe interior properties, by partitioning the spatial region
containing an object into a set of small, no overlapping, contiguous solids.
89
Define Octrees.
Hierarchical tree structures called octrees, are used to represent solid objects in some graphics systems.
Medical imaging and other applications that require displays of object cross sections commonly use
octree representation.
Define projection.
The process of converting the description of objects from world coordinates to viewing coordinates is
known as projection
90
91
What is a spline?
To produce a smooth curve through a designed set of points, a flexible strip called spline is used.
Such a spline curve can be mathematically described with a piecewise cubic polynomial function
whose first and second derivatives are continuous across various curve section.
Define frame.
One of the shape photographs that a film or video is made of is known as frame.
92
93
2. Design our national flag using set of lines generated by DDA or Bresenhams line
drawing algorithm?
3. Design a solar planet system using a set of circles generated by midpoint circle
generation algorithm?
4. Design a sky consisting of clouds using set of ellipses or circles generated by mid
point ellipse generation algorithm?
5. Draw 3 clouds by using graphics functions and fill the 3 clouds with saphron,
white and green colors by using flood fill Algorithm?
6. Draw our national flag by using graphics functions and fill appropriate colors by
using boundary fill algorithm?
9. Using shape tweening and motion tweening, generate any animation sequence?
10. Using bone tool in adobe flash professional, generate an animation sequence of
cartoon character walking?
94
FUNCTIONS OF GRAPHICS.H
C graphics using graphics.h functions can be used to draw different shapes, display
text in different fonts, change colors and many more. Using functions of graphics.h in
turbo c compiler you can make graphics programs, animations, projects and games.
You can draw circles, lines, rectangles, bars and many other geometrical figures.
You can change their colors using the available functions and fill them. Following is a
list of functions of graphics.h header file. Every function is discussed with the
arguments
errors whileitusing
needs, itsfunction
that description,
and possible
a sample c graphics program with its
output.
95
arc function in c
Declaration :- void arc(int x, int y, int stangle, int endangle, int radius);
arc function is used to draw an arc with center (x,y) and stangle specifies starting
angle, endangle specifies the end angle and last parameter specifies the radius of
the arc. arc function can also be used to draw a circle but for that starting angle
and end angle should be 0 and 360 respectively.
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,
135, 50);
getch();
closegraph(
); return 0;
}
96
bar function in c
Declaration :- void bar(int left, int top, int right, int bottom);
C programming code
#include
<graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;
200);
getch();
closegraph(
); return 0;
}
97
bar3d function in c
Declaration :- void bar3d(int left, int top, int right, int bottom, int depth, int topflag);
C program of bar3d
#include<graphics.
h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
getch();
closegraph(
); return 0;
}
98
circle function in c
circle function is used to draw a circle with center (x,y) and third parameter specifies
the radius of the circle. The code given below draws a circle.
main()
{
int gd = DETECT, gm;
getch();
closegraph(
); return 0;
}
99
cleardevice function in c
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\ TurboC3\\BGI");
getch();
closegraph(
); return 0;
}
100
closegraph function in c
closegraph function closes the graphics mode, deallocates all memory allocated by
graphics system and restores the screen to the mode it was in before you called
initgraph.
C code of closegraph
#include<graphics.
h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
mode...");
getch();
closegraph(
); return 0;
}
101
drawpoly function in c
num indicates (n+1) number of points where n is the number of vertices in a polygon,
polypoints points to a sequence of (n*2) integers . Each pair of integers gives x and y
coordinates of a point on the polygon. We specify (n+1) points as first point
coordinates should be equal to (n+1)th to draw a complete figure.
To understand more clearly we will draw a triangle using drawpoly, consider for
example the array :-
int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
points array contains coordinates of triangle which are (320, 150), (420, 300) and
(250, 300). Note that last point(320, 150) in array is same as first. See the program
below and then its output, it will further clear your understanding.
main()
{
int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
getch();
closegraph(
); return 0;
}
102
ellipse function in c
Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse,
stangle is the starting angle, end angle is the ending angle, and fifth and sixth
parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse
strangles and end angle should be 0 and 360 respectively.
main()
{
int gd = DETECT, gm;
getch();
closegraph(
); return 0;
}
103
fillellipse function in c
main()
{
int gd = DETECT, gm;
25);
getch();
closegraph(
); return 0;
}
104
fillpoly function in c
C programming code
#include
<graphics.h>
#include <conio.h>
main()
{
int gd=DETECT,gm,points[]={320,150,440,340,230,340,320,150};
getch();
closegraph(
); return 0;
}
105
floodfill function
floodfill function is used to fill an enclosed area. Current fill pattern and fill color is
used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then
inside will be filled otherwise outside will be filled,border specifies the color of
boundary of area. To change fill pattern and fill color use setfillstyle. Code given
below draws a circle and then fills it.
C programming code
#include<graphics.
h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
TurboC3\\BGI"); setcolor(RED);
circle(100,100,50);
floodfill(100,100,RE
D);
getch();
closegraph(
); return 0;
}
106
getarcoords function in c
getarccoords function is used to get coordinates of arc which is drawn most recently.
arccoordstype is a predefined structure which is defined as follows:
struct arccoordstype
{
int x, y; /* center point of arc */
int xstart, ystart; /* start position */
int xend, yend; /* end position */
};
C program of getarccoords
#include<graphics.
h>
#include<conio.h>
#include<stdio.h>
main()
{
int gd = DETECT,
gm; struct
arccoordstype a;
char arr[100];
initgraph(&gd, &gm,"C:\\
TurboC3\\BGI");
arc(250,200,0,90,100);
getarccoords(&a);
sprintf(arr,"(%d,
%d)",a.xstart,a.ystart);
outtextxy(360,195,arr);
sprintf(arr,"(%d,
%d)",a.xend,a.yend);
outtextxy(245,85,arr);
getch();
closegraph(
); return 0;
}
107
getbkcolor function in c
main()
{
int gd = DETECT, gm,
bkcolor; char a[100];
initgraph(&gd,&gm,"C:\\
TurboC3\\BGI"); bkcolor =
getbkcolor();
getch();
closegraph(
); return 0;
}
108
getcolor function
main()
{
int gd = DETECT, gm,
drawing_color; char a[100];
initgraph(&gd,&gm,"C:\\
TurboC3\\BGI"); drawing_color =
getcolor();
getch();
closegraph(
); return 0;
}
109
getdrivername function
main()
{
int gd = DETECT,
gm; char
*drivername;
drivername = getdrivername();
outtextxy(200, 200,
drivername);
getch();
closegraph(
); return 0;
}
110
getimage function in c
getimage function saves a bit image of specified region into memory, region can be
any rectangle.
Declaration:- void getimage(int left, int top, int right, int bottom, void *bitmap);
getimage copies an image from screen to memory. Left, top, right, and bottom define
the area of the screen from which the rectangle is to be copied, bitmap points to the
area in memory where the bit image is stored.
111
getmaxcolor function
getmaxcolor function returns maximum color value for current graphics mode and
driver. Total number of colors available for current graphics mode and driver are (
getmaxcolor() + 1 ) as color numbering starts from zero.
C program of getmaxcolor
#include<graphics.
h>
#include<conio.h>
main()
{
int gd = DETECT, gm,
max_colors; char a[100];
initgraph(&gd,&gm,"C:\\
TurboC3\\BGI"); max_colors =
getmaxcolor();
getch();
closegraph(
); return 0;
}
112
getmaxx function in c
getmaxx function returns the maximum X coordinate for current graphics mode and
main()
{
int gd = DETECT, gm,
max_x; char array[100];
initgraph(&gd,&gm,"C:\\
getch();
closegraph(
); return 0;
}
113
getmaxy function in c
getmaxy function returns the maximum Y coordinate for current graphics mode and
main()
{
int gd = DETECT, gm,
max_y; char array[100];
initgraph(&gd,&gm,"C:\\
getch();
closegraph(
); return 0;
}
114
getpixel function in c
main()
{
int gd = DETECT, gm,
color; char array[50];
initgraph(&gd,&gm,"C:\\
getch();
closegraph(
); return 0;
}
115
getx function in c
C program of getx
#include
<graphics.h>
#include <conio.h>
main()
{
int gd = DETECT,
gm; char
array[100];
%d",getx()); outtext(array);
getch();
closegraph(
); return 0;
}
116
gety function in c
main()
{
int gd = DETECT,
gm, y; char
array[100];
TurboC3\\BGI"); y = gety();
y); outtext(array);
getch();
closegraph(
); return 0;
}
117
graphdefaults function in c
main()
{
int gd = DETECT, gm;
TurboC3\\BGI"); setcolor(RED);
setbkcolor(YELLOW);
getch();
graphdefaults(
);
getch();
closegraph(
); return 0;
}
118
grapherrormsg function in c
main()
{
int gd, gm, errorcode;
TurboC3\\BGI"); errorcode =
graphresult();
if(errorcode != grOk)
{
printf("Graphics error: %s\n",
grapherrormsg(errorcode)); printf("Press any key to
exit.");
getch();
exit(1);
}
getch();
closegraph(
); return 0;
}
119
imagesize function in c
imagesize function returns the number of bytes required to store a bitimage. This
function is used when we are using getimage.
Declaration:- unsigned int imagesize(int left, int top, int right, int bottom);
main()
{
int gd = DETECT, gm,
bytes; char array[100];
getch();
closegraph(
); return 0;
}
120
line function in c
line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and
(x2,y2) are end points of the line.The code given below draws a line.
Declaration :- void line(int x1, int y1, int x2, int y2);
main()
{
int gd = DETECT, gm;
200);
getch();
closegraph(
); return 0;
}
121
lineto function in c
lineto function draws a line from current position(CP) to the point(x,y), you can get
current position using getx and gety function.
main()
{
int gd = DETECT, gm;
getch();
closegraph(
); return 0;
}
122
linerel function in c
Linerel function draws a line from the current position(CP) to a point that is a relative
distance (x, y) from the CP, then advances the CP by (x, y). You can use getx and
gety to find the current position.
main()
{
int gd = DETECT, gm;
getch();
closegraph(
); return 0;
}
123
moveto function in c
main()
{
int gd = DETECT,
gm; char
msg[100];
Y = %d",getx(),gety()); outtext(msg);
moveto(50, 50);
gety()); outtext(msg);
getch();
closegraph(
); return 0;
}
124
moverel function in c
main()
{
int gd = DETECT, gm,
x, y; char
message[100];
x = getx();
y = gety();
getch();
closegraph(
); return 0;
}
125
outtext function
main()
{
int gd = DETECT, gm;
getch();
closegraph(
); return 0;
}
126
outtextxy function in c
main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\ TurboC3\\BGI");
function");
getch();
closegraph(
); return 0;
}
127
pieslice function in c
#include<graphics.
h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
TurboC3\\BGI");
pieslice(200,200,0,135,100);
getch();
closegraph(
); return 0;
}
128
putimage function in c
op);
putimage puts the bit image previously saved with getimage back onto the screen,
with the upper left corner of the image placed at (left, top). ptr points to the area in
memory where the source image is stored. The op argument specifies a operator
that controls how the color for each destination pixel on screen is computed, based
on pixel already on screen and the corresponding source pixel in memory.
129
putpixel function in c
For example if we want to draw a GREEN color pixel at (35, 45) then we will
write putpixel(35, 35, GREEN); in our c program, putpixel function can be used to
draw circles, lines and ellipses using various algorithms.
main()
{
int gd = DETECT, gm;
getch();
closegraph(
); return 0;
}
130
rectangle function in c
Declaration :- void rectangle(int left, int top, int right, int bottom);
rectangle function is used to draw a rectangle. Coordinates of left top and right
bottom corner are required to draw the rectangle. left specifies the X-coordinate of
top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-
coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom
corner. The code given below draws a rectangle.
main()
{
int gd = DETECT, gm;
TurboC3\\BGI");
rectangle(100,100,200,200);
getch();
closegraph(
); return 0;
}
131
MODI INSTITUTE OF TECHNOLOGY CSE Department
37) sector
sector function in c
Declaration :- void sector( int x, int y, int stangle, int endangle, int xradius, int yradius);
main()
{
int gd = DETECT, gm;
getch();
closegraph(
); return 0;
}
132
setbkcolor function in c
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\ TurboC3\\BGI");
setbkcolor(GREEN);
getch();
closegraph(
); return 0;
}
133
setcolor function in c
In Turbo Graphics each color is assigned a number. Total 16 colors are available.
Strictly speaking number of available colors depends on current graphics mode and
driver.For Example :- BLACK is assigned 0, RED is assigned 4 etc. setcolor function
is used to change the current drawing color.e.g. setcolor(RED) or setcolor(4)
changes the current drawing color to RED. Remember that default drawing color is
WHITE.
main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\
TurboC3\\BGI");
getch();
closegraph(
); return 0;
}
134
setfillstyle function in c
enum fill_styles
{
EMPTY_FILL,
SOLID_FILL,
LINE_FILL,
LTSLASH_FILL,
SLASH_FILL,
BKSLASH_FILL
,
LTBKSLASH_FI
LL,
HATCH_FILL,
XHATCH_FILL,
INTERLEAVE_
FILL,
WIDE_DOT_FIL
L,
CLOSE_DOT_F
ILL, USER_FILL
};
C programming source code for setfillstyle
#include<graphics.
h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
TurboC3\\BGI");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
closegraph(
); return 0;
}
135
MODI INSTITUTE OF TECHNOLOGY CSE Department
•41) setlinestyle
setlinestyle in c
Declaration:
void setlinestyle( int linestyle, unsigned upattern, int
enum line_styles
{
SOLID_LINE
,
DOTTED_LI
NE,
CENTER_LI
NE,
DASHED_LI
NE,
USERBIT_LI
NE
};
C programming code
#include <graphics.h>
main()
{
int gd = DETECT, gm, c , x = 100, y =
TurboC3\\BGI");
line(x, y, x+200,
y); y = y + 25;
}
getch();
closegraph(
); return 0;
}
136
MODI INSTITUTE OF TECHNOLOGY CSE Department
•42) settextstyle
settextstyle function in c
Settextstyle function is used to change the way in which text appears, using it we
can modify the size of text, change direction of text and change the font of text.
Different fonts
enum font_names
{
DEFAULT_FONT,
TRIPLEX_FONT,
SMALL_FONT,
SANS_SERIF_FO
NT,
GOTHIC_FONT,
SCRIPT_FONT,
SIMPLEX_FONT,
TRIPLEX_SCR_F
ONT,
COMPLEX_FONT
,
EUROPEAN_FON
T, BOLD_FONT
};
C programming source code for settextstyle
#include
<graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm, x = 25, y = 25,
font = 0; initgraph(&gd,&gm,"C:\\
TurboC3\\BGI");
getch();
closegraph(
); return 0;}
137
setviewport function in c
Declaration :- void setviewport(int left, int top, int right, int bottom, int
clip);
main()
{
int gd = DETECT, gm, midx, midy;
TurboC3\\BGI");
midx =
getmaxx()/2; midy
= getmaxy()/2;
getch();
closegraph(
); return 0;
}
138
textheight function in c
main()
{
int gd = DETECT, gm,
height; char array[100];
sprintf(array,"Textheight =
%d",height); outtext(array);
getch();
closegraph(
); return 0;
}
139
textwidth function in c
main()
{
int gd = DETECT, gm,
width; char array[100];
sprintf(array,"Textwidth =
%d",width); outtext(array);
getch();
closegraph(
); return 0;
}
140