Sie sind auf Seite 1von 30

Ex.

No: 1
BASIC GRAPHIC FUNCTIONS
Aim:
To write a program in C to draw the basic graphics primitives.
Algorithm:
1. Start the program.
2. Initialize the variables.
3. Call the initgraph() function.
4. Set colour for the output primitives.
5. Get the option to draw the basic primitives like arc, bar, circle, 3D bar, ellipse, rectangle, line and the
options to fill.
6. Based on the option get the inputs for the primitives from user.
7. By using existing functions, the primitives can be drawn.
8. Stop the graphics driver.
9. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
void draw_arc()
{
int midx, midy,stangle,endangle,radius;
printf("Enter mid x,mid y,start angle,end angle,radius:\n");
scanf("%d%d%d%d%d",&midx,&midy,&stangle,&endangle,&radius);
setcolor(getmaxcolor());
arc(midx, midy, stangle, endangle, radius);
getch();
}
void draw_bar()
{
int x1,y1,x2,y2,i;
printf("Enter x1,y1,x2,y2:\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
bar(x1,y1,x2,y2);
getch();}
void draw_bar3d()
{
int x1,y1,x2,y2,width,i;
printf("Enter x1,y1,x2,y2,width:\n");
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&width);
bar3d(x1,y1, x2,y2,width,1);
getch();
}
void draw_circle()
{
int midx, midy,radius;
printf("Enter mid x,mid y,radius:\n");
scanf("%d%d%d",&midx,&midy,&radius);
circle(midx, midy, radius);
getch();
}
void draw_rectangle()
{
int left, top, right, bottom;
printf("Enter left,right,top,bottom:\n");
scanf("%d%d%d%d",&left,&right,&top,&bottom);
rectangle(left,top,right,bottom);
getch();
}
void draw_ellipse()
{
int midx, midy,xradius,yradius;
printf("Enter mid x,mid y,radius1,radius2:\n");
scanf("%d%d%d%d",&midx,&midy,&xradius,&yradius);
ellipse(midx, midy,0,360,xradius, yradius);
getch();
}
void draw_fillellipse()
{
int midx, midy, i;
int xradius = 100, yradius = 50;
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setfillstyle(1, getmaxcolor());
fillellipse(midx, midy, xradius, yradius);
getch();
3
}
void draw_polygon()
{
int n,n1,m[100],i;
printf("Enter the no. of sides:\n");
scanf("%d",&n);
n1=n*2;
printf("Enter the no. of co-cordinates:\n");
for(i=0;i<n1+2;i++)
{ scanf("%d",&m[i]);}
drawpoly(n+1,m);
getch();
}
void draw_fillpolygon()
{
int n,n1,m[100],i;
printf("Enter the no. of sides:\n");
scanf("%d",&n);
n1=n*2;
printf("Enter the no. of co-cordinates:\n");
for(i=0;i<n1+2;i++)
{ scanf("%d",&m[i]);}
setfillstyle(1, getmaxcolor());
fillpoly(n+1,m);
getch();
}
void draw_floodfill ()
{
int maxx, maxy;
maxx = getmaxx();
maxy = getmaxy();
setcolor(getmaxcolor());
setfillstyle(SOLID_FILL, getmaxcolor());
rectangle(0, 0, maxx, maxy);
circle(maxx / 3, maxy /2, 50);
circle(maxx / 2, 20, 100);
circle(maxx-20, maxy-50, 75);
circle(20, maxy-20, 25);
getch();
floodfill(2, 2, getmaxcolor());
getch();
}
4
void draw_line()
{
int x1, y1,x2,y2;
printf("Enter x1,y1,x2,y2:\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
getch();
}
void draw_lineto()
{
int x1,y1,x2,y2;
char msg[80];
printf("Enter x1,y1,x2,y2:\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
moveto(x1,y1);
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(x1,y1, msg);
lineto(x2,y2);
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
getch();
}
void draw_linerel()
{
int x1,y1,x2,y2;
char msg[80];
printf("Enter x1,y1,x2,y2:\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
moveto(x1,y1);
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(x1,y1, msg);
linerel(x2,y2);
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
getch();
}
void draw_outtext()
{
char msg[100];
int midx, midy;
printf("\nEnter the string:\t");
scanf("%s",&msg);
5
midx = getmaxx() / 2;
midy = getmaxy() / 2;
moveto(midx, midy);
outtext(msg);
getch();
}
void draw_outtextxy()
{
char msg[100];
int midx, midy;
printf("\nEnter the string and x ,y:\t");
scanf("%s%d%d",&msg,&midx,&midy);
moveto(midx, midy);
outtextxy(midx, midy,msg);
getch();
}
void set_background()
{
int bkcol, maxcolor;
maxcolor = getmaxcolor();
for (bkcol=0; bkcol<=maxcolor; bkcol++)
{
cleardevice();
setbkcolor(bkcol);
if (bkcol == 14)
break;
getch();
}
getch();
}
void set_color()
{
int color, maxcolor, x, y;
char msg[80];
maxcolor = getmaxcolor();
settextjustify(CENTER_TEXT, CENTER_TEXT);
x = getmaxx() / 2;
y = getmaxy() / 2;
for (color=1; color<=maxcolor; color++)
{
cleardevice();
setcolor(color);
sprintf(msg, "Color: %d", color);
6
outtextxy(x, y, msg);
getch();
}
}
void main()
{
int ch;
int flag=1;
int gdriver=DETECT,gmode,gerror;
initgraph(&gdriver,&gmode,"e:\\tc\\bgi:");
while(flag)
{
printf("1)arc\t\t");
printf("2)bar\n");
printf("3)3d bar\t");
printf("4)circle\n");
printf("5)rectangle\t");
printf("6)ellipse\n");
printf("7)fill ellipse\t");
printf("8)polygon\n");
printf("9)fill polygon\t");
printf("10)floodfill\n");
printf("11)line\t\t");
printf("12)line to\n");
printf("13)linerel\t");
printf("14)out text\n");
printf("15)out text xy\t");
printf("16)set background color\n");
printf("17)set color\t");
printf("18)exit\n");
printf("\n Enter the your choice:\t");
scanf("%d",&ch);
cleardevice();
switch(ch)
{
case 1:
draw_arc();
break;
case 2:
draw_bar();
break;
case 3:
draw_bar3d();
break;
case 4:
7
draw_circle();
break;
case 5:
draw_rectangle();
break;
case 6:
draw_ellipse();
break;
case 7:
draw_fillellipse();
break;
case 8:
draw_polygon();
break;
case 9:
draw_fillpolygon();
break;
case 10:
draw_floodfill();
break;
case 11:
draw_line();
break;
case 12:
draw_lineto();
break;
case 13:
draw_linerel();
break;
case 14:
draw_outtext();
break;
case 15:
draw_outtextxy();
break;
case 16:
set_background();
break;
case 17:
set_color();
default:
flag=0;
}
}
getch();
}
8
Output:
1)arc
2)bar
3)3d bar
4)circle
5)rectangle
6)ellipse
7)fill ellipse
8)polygon
9)fill polygon
10)floodfill
11)line
12)line to
13)linerel
14)out text
15)out text xy
16)set background color
17)set color
18)exit
Enter your choice: 1
Enter mid x,mid y,start angle,end angle,radius: 100 200 180 270 40

Ex.No: 3.a)
BRESENHAMS ALGORITHM FOR LINE DRAWING
Aim:
To write a program in C to draw a line using Bresenhams algorithm.
Algorithm:
1. Start the program.
2. Initialize the variables.
3. Call the initgraph() function.
4. Input the two line end-points and store the left end-point in (x0, y0).
5. Plot the point (x0, y0).
6. Calculate the constants x, y, 2y, and (2y - 2x) and get the first value for the decision parameter
as:
7. 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
8. Otherwise, the next point to plot is (xk+1, yk+1) and Repeat step 4 (x 1) times.
9. Stop the graphics driver.
10. Stop the program.
Program:
#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);
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;
xyp=20yppkk+=+21xyppkk+=+221
17
y=y1;
dx=x2-x1;
dy=y2-y1;
putpixel(x,y,2);
p=(2dy-dx);
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();
}
18
Output:
Enter the x-coordinate of the first point ::40
Enter the y-coordinate of the first point ::50
Enter the x-coordinate of the second point ::60
Enter the y-coordinate of the second point ::80
Result:
Thus the program in C to draw the line by using Bresenhams algorithm was done successfully.
19
Ex.No: 3.b)
BRESENHAMS ALGORITHM TO DRAW A CIRCLE
Aim:
To write a program in C to draw a circle using Bresenhams Algorithm.
Algorithm:
1. Start the program.
2. Initialize the variables.
3. Call the initgraph() function.
4. 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:
5. Calculate the initial value of the decision parameter as:
6. 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
7. Otherwise the next point along the circle is (xk+1, yk-1) and
8. Determine symmetry points in the other seven octants
9. Move each calculated pixel position (x, y) onto the circular path centred at (xc, yc) to plot the
coordinate values:
10. Repeat steps 3 to 5 until x >= y
11. Stop the graphics driver.
12. Stop the program.
Program:
#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++;
20
if(p>0)
{
p=p+2*(x-y)+1;
y--;
}
else
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);
}
21
Output:
Enter the radius 50
Result:
Thus the program in C to draw the circle by using Bresenhams algorithm was done successfully.
Enter your choice: 2
Enter x1,y1,x2,y2: 100 200 70 80
Enter your choice: 3
9
Enter x1,y1,x2,y2,width: 100 200 350 250 1
Enter your choice: 4
Enter mid x,mid y,radius: 100 200 80
Enter your choice: 5
Enter left,right,top,bottom: 100 200 70 80
Enter your choice: 6
Enter mid x,mid y,radius1,radius2: 100 300 50 60
Enter your choice: 7
10
Enter your choice: 8
Enter the no. of sides: 6
Enter the no. of co-cordinates: 40 40 40 60 70 80 100 60 100 40 70 20 40 40
Enter your choice: 9
Enter the no. of sides: 6
Enter the no. of co-cordinates: 40 40 40 60 70 80 100 60 100 40 70 20 40 40
Enter your choice: 10
11
Enter your choice: 11
Enter x1,y1,x2,y2,: 100 200 350 250
Enter your choice: 12
Enter x1,y1,x2,y2,: 10 20 40 50
(10,20)
(40,50)
Enter your choice: 13
Enter x1,y1,x2,y2,: 100 200 350 250
12
(100,200)
(350,250)
Enter your choice: 14
Enter the string: hello
hello
Enter the string and x ,y: hello 10,10
hello
Enter your choice: 18
Result:
Thus the program in C to draw the basic graphics primitives was done successfully.

Ex.No: 2
DDA ALGORITHM FOR DRAWING LINE
Aim:
To write a C program for draw a line using DDA line Algorithm.
Algorithm:
1. Start the program.
2. Enter the starting and ending point of the line.
3. Call the initgraph() function.
4. Invoke the function draw, and calculate the absolute value of dx and dy and check if
abs(dx)>abs(dy).
5. If true assign step size as abs(dx), or else assign as abs(dy).
6. Calculate the x in c, y in c and plot the start point use the loop, until k is less than or equal to step size.
7. Calculate the x and y for each steps and plot the corresponding pixels and let the line be displayed.
8. Stop the graphics driver.
9. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
void draw(int x1,int y1,int x2,int y2);
int main(void)
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
printf("\n Enter the x and y value for starting point:\n");
scanf("%d%d",&x1,&y1);
printf("\n Enter the x and y value for ending point:\n");
scanf("%d%d",&x2,&y2);
clrscr();
initgraph(&gdriver,&gmode,"E:\\TC\\BGI\\");
draw(x1,y1,x2,y2);
getch();
return 0;
14
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k,step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(abs(x),abs(y),111);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(abs(x),abs(y),111);
}
}
15
Output:
Enter the x and y value for starting point: 50 60
Enter the x and y value for ending point: 70 90
Result:

Ex.No: 3.c)
BRESENHAM'S ALGORITHM FOR ELLIPSE
Aim:
To write a program in C to draw a ellipse using Bresenhams algorithm.
Algorithm:
1. Start the program
2. Declare the variables and initialize the graphics driver.
3. The radius and center point of the ellipse be the input.
4. Calculate the rx2 and ry2 and initial decision parameter(p).
5. Repeat until 2r2yx>2r2xy is met and increment x and y values in each iteration.
6. If p<0 do p=rx2-py2 else p=rx2-py+px and y is decremented and calculate ellipsepoint()
7. Draw the corresponding arc.
8. Stop the program.
Program:
#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 a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"");
printf("\n Enter the value for xc:");
scanf("%d",&xc);
printf("\n Enter the value for yc:");
scanf("%d",&yc);
printf("\n Enter the value for a:");
scanf("%d",&a);
printf("\n Enter the value for b:");
scanf("%d",&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
23
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
}
24
Output:
Enter the value for xc: 50
Enter the value for yc: 50
Enter the value for a: 125
Enter the value for b: 150
Result:
Ex.No: 4
ATTRIBUTES OF LINE, CIRCLE, ELLIPSE
Aim:
To implement available attributes of line, circle and ellipse using c language.
Algorithm:
1. Start the program.
2. Initialize the variables a,b,i,ch,x,y,fil,col,st,wd .
3. Based on user input,the functions like gotodrawline(), drawcircle(), drawellipse() were implemented .
4. In drawline, Use setcolor() and setlinestyle() to provide attributes.
i. Use line() to draw the line.
5. In drawcircle, Use setcolor() and setfillstyle() to provide attributes.
i. Use pieslice() to create circle.
6. In drawellipse, Use setcolor() and setlinestyle() to provide attributes.
i. Use ellipse() to create ellipse.
7. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int gd=DETECT,gm;
int a,b,i,ch,x,y,fil,col,st,wd;
char msg[100];
void drawline()
{
cleardevice();
printf("\nEnter linestyle[0-4] width[1-4] color[0-15]:");
scanf("%d%d%d",&st,&wd,&col);
setcolor(col);
setlinestyle(st,1,wd);
line(100,100,200,200);
}
void drawcircle()
{
cleardevice();
printf("\nEnter fillstyle[0-11] width[1-3] color[0-15]:");
scanf("%d%d%d",&st,&wd,&col);
setcolor(col);
26
setlinestyle(0,1,wd);
setfillstyle(st,col+1);
pieslice(400,400,0,360,30);
}
void drawellipse()
{
cleardevice();
printf("\nEnter fillstyle[0-11] width[1-3] color[0-15]:");
scanf("%d%d%d",&st,&wd,&col);
setcolor(col);
setlinestyle(0,1,wd);
setfillstyle(st,col+1);
fillellipse(400,400,30,70);
}
void main()
{
initgraph(&gd,&gm,"C:\\TC\\BGI\\");
while(1)
{
printf("1.Line 2.Circle 3.Ellipse 4.Exit\n Enter the option:");
scanf("%d",&ch);
switch(ch)
{
case 1:
drawline();
break;
case 2:
drawcircle();
break;
case 3:
drawellipse();
break;
default:
exit(0);
}
}
}
27
Output:
1.Line 2.Circle 3.Ellipse 4.Exit
Enter the option:1
Enter linestyle[0-4] width[1-4] color[0-15]: 3 3 10
1.Line 2.Circle 3.Ellipse 4.Exit
Enter the option:2
1.Line 2.Circle 3.Ellipse 4.Exit
Enter the option:3
Result:
Thus the program in C to draw the attributes of line, circle, ellipse was done successfully

Das könnte Ihnen auch gefallen