You are on page 1of 25

2

Table of contents

S.No. Name of Experiment Page No.

1 Write a program to implement the basic primitive functions of graphics. 6

2 Write a program to implement setlinestyle() function 8

write a C-program for displaying text in different sizes, different colors


3 10
and different font styles by using graphics functions.

4 Write a program to design a logo/poster using primitive functions. 12

Write a program To implement DDA Algorithm for drawing a line


5 14
segment between two given end points A (x1, y1) and B(x2, y2).

Write a program To implement Bresenham’s line drawing algorithm for


6 drawing a line segment between two given endpoints A (x1, y2) and 15
B(x2, y2).

Write a program to implement midpoint circle generation algorithm for


7 drawing a circle of given center (x, y) and radius r. 16

Write a program to implement bresenham’s circle algorithm for drawing


8 a circle of given center (x, y) and radius r. 18

9 Write a program to draw an ellipse using Midpoint Algorithm. 20

10 Write a program to implement drawpoly() function 22

11 Write a program illustrating the use of Fillpoly function: 23

To perform 2D transformations such as translation, scaling, and


12 24
rotation on a polygon.

13 Write a program in C to draw 3D bar chart 26


3

Basic Structure of a C-graphics program:

#include<stdio.h>

#include<graphics.h>//must be included for every graphics program

#include<conio.h>

#include<dos.h> //for including delay function.

void main()

int gd=DETECT,gm; //gd=detects best available graphics driver, gm =graphics mode.

initgraph(&gd,&gm,”C:\\TurboC3\\BGI”);// for initializing graph mode

// above 2 steps are must for every graphics program.

//declaration of any variables must be done before calling initgraph() function.

// next write code for producing requiring design or drawing object

line(100,100,200,200);//draws a line segment.

getch();

}
4

Use of primitive functions of graphic

1. Putpixel : This function is used to draw a pixel on the screen. Pixel is a small dot on the screen.

Syntax: putpixel(x co-orinate, y co-ordinate,COLOR);

2. line() : It draws a line between two specific points.

Syntax : line(int x1,int y1,int x2,int y2);

3. circle() : This function is used to draw the circle.

Syntax : circle(int x,int y,radius);

Argument :

(x,y)---Centre point of circle.


radius-- of the circle.

4. rectangle() : This function is used to draw rectangle. It draws the rectangle in the current line style,
thickness and drawing color.

Syntax : rectangle(int left,int top,int right,int bottom);

(left,top) is the upper left corner of the rectangle and (right,bottom) is the bottom right corner of the
rectangle.

5. arc() : It draws a circular arc in the current drawing color.

Syntax : arc(int x,int y,int stangle,int endangle,radius);

(x,y)---centre point of arc


stangle---starting angle
endangle---ending angle

6. ellipse() : It is used to draw an elliptical arc in the current color.

Syntax : ellipse(int x,int y,int stangle,int endangle,int xradius,int yradius);

xradius---Horizontal axis
yradius---Vertical axis

7. setcolor() : It sets the current drawing color.

Syntax : setcolor(int color);

8. setfillstyle() : It sets the current fill pattern and fill color.

Syntax : setfillstyle(int pattern,int color);


5

Below is the table showing INT VALUES corresponding to Colors :

COLOR INT VALUES


-------------------------------
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15

Below is the table showing INT VALUES corresponding to Patterns :

PATTERN INT VALUES


-------------------------------
EMPTY_FILL 0
SOLID_FILL 1
LINE_FILL 2
LTSLASH_FILL 3
SLASH_FILL 4
BKSLASH_FILL 5
LTBKSLASH_FILL 6
HATCH_FILL 7
XHATCH_FILL 8
INTERLEAVE_FILL 9
WIDE_DOT_FILL 10
CLOSE_DOT_FILL 11
USER_FILL 12

9. floodfill() : It fills a bounded region.

Syntax : floodfill(int x,int y,int border);

Here (x,y) is the 'seed point'.

If the seed is within enclosed area, the inside will be filled.


If the seed is outside the enclosed area,the exterior will be filled.
6

10. Putpixel : This function is used to draw a pixel on the screen. Pixel is a small dot on the screen.

Syntax: putpixel(x co-orinate, y co-ordinate,COLOR);

11. SetbkColor: This function is used to set background color of the screen.

Syntax: setbkcolor(COLOR);

Program -1 Write a program to implement the basic primitive functions of graphics.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setbkcolor(7);
setcolor(14);
line(50,50,100,100);
setcolor(10);
circle(300,90,50);
setfillstyle(2,3);
floodfill(300,90,10);
setcolor(13);
rectangle(450,50,600,150);
setfillstyle(7,4);
floodfill(451,51,13);
setcolor(4);
arc(70,350,0,225,50);
setcolor(15);
ellipse(300,350,0,360,50,60);
setfillstyle(11,9);
floodfill(300,350,15);
getch();
closegraph();
}
7

OUTPUT:
8

Use of setlinestyle() Function

 Setlinestyle: This function is used to set the current line style, width and pattern

Syntax: setlinestyle(linestyle, pattern, thickness);

Example: setlinestyle(SOLID_LINE,1,2);

Program 2: Write a program to implement setlinestyle() function :

#include <graphics.h>
#include<iostream.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
int c;
int x = 200, y = 100;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for ( c = 0 ; c < 5 ; c++ )
{
setlinestyle(c, 0, 1);
line(x, y, x+200, y);
y = y + 25;
}

getch();
closegraph();
}

OUTPUT :-
9

Creating various types of texts and fonts

The following graphics functions are available for text formatting in C.

1) settextstyle() : It sets the current text characteristics. It sets the text font, the direction in which text is
displayed and the size of the characters.

Syntax : settextstyle(int font,int direction,int charsize);

direction values:-

if this argument is 0---Horizontal direction


if this argument is 1---Vertical direction

The table below shows the fonts with their INT values and appearance:

2) Outtext: outtext function displays text at current position.


Example: outtext("To display text at a particular position on the screen use outtextxy");

3) outtextxy() : It displays a string at the specified position.

Syntax : outtextxy()int x,int y,textstring);


10

Program 3: write a C-program for displaying text in different sizes, different colors and
different font styles by using graphics functions.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x=25,y=25,font=10;
initgraph(&gd,&gm,"C:\\turboC3\\BGI");
for(font=0;font<=4;font++)
{
settextstyle(font,HORIZ_DIR,font+1);// sets font type, font direction, size
setcolor(font+1); // sets color for text.
outtextxy(x,y,"text with different fonts"); // prints message on screen at (x,y)
y=y+25;
}
for(font=0;font<=2;font++)
{
settextstyle(font,VERT_DIR,font+2);
setcolor(font+1);
x=250;
y=100;
outtextxy(x,y,"text in vertical direction");
y=y+25;
}
getch();
closegraph();
}

Output:
11

Program 4: Write a program to design a logo/poster using primitive functions.

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

void main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\turboc3\\bgi");

// Create Background color as Grey


setbkcolor(7);

// Create two circles in each


// another & color Blue
setfillstyle(SOLID_FILL, BLUE);
circle(300, 300, 100);
circle(300, 300, 90);
floodfill(202, 300, 15);

// Create two circles in each


// another & color Yellow
setfillstyle(SOLID_FILL, YELLOW);
circle(400, 400, 100);
circle(400, 400, 90);
floodfill(322, 350, 15);
floodfill(302, 400, 15);

// Create two circles in each


// another & color Black
setfillstyle(SOLID_FILL, BLACK);
circle(520, 300, 100);
circle(520, 300, 90);
floodfill(442, 350, 15);
floodfill(422, 300, 15);

// Create two circles in each


// another & color Green
setfillstyle(SOLID_FILL, GREEN);
circle(620, 400, 100);
circle(620, 400, 90);
floodfill(522, 400, 15);
floodfill(542, 350, 15);

// Create two circles in each


// another & color Red
setfillstyle(SOLID_FILL, RED);
circle(740, 300, 100);
circle(740, 300, 90);
floodfill(642, 300, 15);
floodfill(662, 350, 15);

// Hold the screen for a while


getch();
12

// Close the initialized gdriver


closegraph();
}

Output:
13

Program 5: Write a program 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(dy))
steps=abs(dx);
else
steps=abs(dy);
xincr=dx/steps;
yincr=dy/steps;
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),WHITE);
}
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; OUTPUT
}
14

Program 6: Write a program To implement Bresenham’s line drawing algorithm for drawing
a line segment between two given endpoints A (x1, y2) and B(x2, y2).

#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<=x2)
{
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();
}
Output:
15

Program 7: Write a program to implement midpoint circle generation algorithm for drawing
a circle of given center (x, y) and radius r.

#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();
}
void draw_circle(int xc,int yc,int rad)
{
int x = 0;
int y = rad;
int p = 1-rad;
symmetry(x,y,xc,yc);
for(x=0;y>x;x++)
{
if(p<0)
p += 2*x + 3;
else
{
p += 2*(x-y) + 5;
y--;
}
symmetry(x,y,xc,yc);
delay(50);
}
}
void symmetry(int x,int y,int xc,int yc)
{
putpixel(xc+x,yc-y,GREEN); //For pixel (x,y)
delay(50);
putpixel(xc+y,yc-x, GREEN); //For pixel (y,x)
delay(50);
putpixel(xc+y,yc+x, GREEN); //For pixel (y,-x)
delay(50);
putpixel(xc+x,yc+y, GREEN); //For pixel (x,-y)
delay(50);
16

putpixel(xc-x,yc+y, GREEN); //For pixel (-x,-y)


delay(50);
putpixel(xc-y,yc+x, GREEN); //For pixel (-y,-x)
delay(50);
putpixel(xc-y,yc-x, GREEN); //For pixel (-y,x)
delay(50);
putpixel(xc-x,yc-y, GREEN); //For pixel (-x,y)
delay(50);
}

Output:
17

Program 8: Write a program to implement bresenham’s circle algorithm for drawing a circle
of given center (x, y) and radius r.

// C-program for circle drawing


// using Bresenham’s Algorithm
// in computer-graphics
#include <iostream.h>
#include <dos.h>
#include <graphics.h>

// Function to put pixels


// at subsequence points
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}

// Function for circle-generation


// using Bresenham's algorithm
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x)
{
// for each pixel we will
// draw all eight pixels

x++;

// check for decision parameter


// and correspondingly
// update d, x, y
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(50);
}
}

// Driver code
void main()
{
18

int xc = 50, yc = 50, r = 30;


int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); // initialize graph
circleBres(xc, yc, r); // function call
getch();
}

Output:
19

Program 9: Write a program to draw an ellipse using Midpoint Algorithm.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int rx,ry;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the center point :");
scanf("%d%d",&xc,&yc);
printf("Enter the value for Rx and Ry :");
scanf("%d%d",&rx,&ry);
x=0;
y=ry;
disp();
p1=(ry*ry)-(rx*rx*ry)+(rx*rx)/4;
while((2.0*ry*ry*x)<=(2.0*rx*rx*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*ry*ry*x)+(ry*ry);
else
{
y--;
p1=p1+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(ry*ry);
}
disp();
x=-x;
disp();
x=-x;
}
x=rx;
y=0;
disp();
p2=(rx*rx)+2.0*(ry*ry*rx)+(ry*ry)/4;
while((2.0*ry*ry*x)>(2.0*rx*rx*y))
{
y++;
if(p2>0)
p2=p2+(rx*rx)-(2.0*rx*rx*y);
else
{
x--;
15
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
p2=p2+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(rx*rx);
20

}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
delay(50);
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc-x,yc-y,10);
}

Output:
21

Drawing polygons
1. Drawpoly(): This function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.

Syntax: drawpoly( int num, int *polypoints );

where,
number 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.

Program 10: Write a program to impliment drawpoly() function

#include <graphics.h>
#include <conio.h>
main()
{
int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};

initgraph(&gd, &gm, "C:\\TC\\BGI");

drawpoly(4, points);

getch();
closegraph();
return 0;
}

Output:
22

2. 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.

Program 11: Write a program illustrating the use of Fillpoly function:

#include <graphics.h>
#include <conio.h>
main()
{
int
gd=DETECT,gm,points[]={100,200,120,230,150,260,120,290,100,330,80,290,50,260,80,230,100,
200};
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
setfillstyle(SOLID_FILL,10); //giving the type of fill
fillpoly(9, points); //drawing a polygon of 9 vertices with coordinates defined in points array and
filling
with color defined in setfillstyle function.
getch();
closegraph();
return 0;
}

Output:
23

Program 12: To perform 2D transformations such as translation, scaling, and rotation on a


polygon.

ALGORITHM:
1. Start
2. Initialize the graphics mode.
3. Construct a 2D object (use Drawpoly()) e.g. (x,y)
4. A) Translation
a. Get the translation value tx, ty
b. Move the 2d object with tx, ty (x’=x+tx,y’=y+ty)
c. Plot (x’,y’)
5. B) Scaling
a. Get the scaling value Sx,Sy
b. Resize the object with Sx,Sy (x’=x*Sx,y’=y*Sy)
c. Plot (x’,y’)
6. C) Rotation
a. Get the Rotation angle
b. Rotate the object by the angle ф
x’=x cos ф - y sin ф
y’=x sin ф - y cosф
c. Plot (x’,y’)

PROGRAM:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"c:\tc\bg:");
printf("\t Program for basic transactions");
printf("\n\t Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
24

nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 4:
break;
default:
printf("Enter the correct choice");
}
closegraph();
}

OUTPUT:-
25

Program 13: Write a program in C to draw 3D bar chart

In this program, we will draw a 3D bar graph on screen. Here, we will use line, setfillstyle and bar3d
functions of graphics.h header file to draw horizontal and vertical axis and bars on screen.

void line(int x1, int y1, int x2, int y2);


It draws a line from (x1, y1) to (x2, y2).

void setfillstyle(int pattern, int color);


It sets the current fill pattern and fill color.

void bar3d(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight, int depth, int topFlag);
bar3d function draws a 3D cuboid and fill front facing surface with current fill pattern and color.

Function Argument Description


xTopLeft X coordinate of top left corner.
yTopLeft Y coordinate of top left corner.
xBottomRight X coordinate of bottom right corner.
yBottomRight Y coordinate of bottom right corner.
depth It specifies the depth of bar in pixels.
topFlag It specifies whether a 3D top to put on the bar or not(any non-zero value specifies a 3d top
other wise no 3d top).

C program to draw 3D bar graph using graphics

#include <graphics.h>
#include <conio.h>

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

settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0,"3D BAR GRAPH");

setlinestyle(SOLID_LINE,0,2);
/* Print X and Y Axis */
line(90,410,90,50);
line(90,410,590,410);
line(85,60,90,50);
line(95,60,90,50);
line(585,405,590,410);
line(585,415,590,410);

outtextxy(65,60,"Y");
outtextxy(570,420,"X");
outtextxy(70,415,"O");

/* Print 3D bars */
setfillstyle(XHATCH_FILL, RED);
bar3d(150,80,200,410, 15, 1);
26

bar3d(225,100,275,410, 15, 1);


bar3d(300,120,350,410, 15, 1);
bar3d(375,170,425,410, 15, 1);
bar3d(450,135,500,410, 15, 1);

getch();
closegraph();
return 0;
}

Output:

You might also like