Practical 1: Aim: To Study All The Graphics Function
Practical 1: Aim: To Study All The Graphics Function
: 080410107081
PRACTICAL 1
Declaration:
void far line(int x1, int y1, int x2, int y2);
void far linerel(int dx, int dy);
void far lineto(int x, int y);
Remarks:
Line draws a line from (x1, y1) to (x2, y2) using the current color, line style, and
thickness. It does not update the current position (cp).
Linerel draws a line from the cp to a point that is a relative distance (dx, dy) from the cp,
then advances the cp by (dx, dy).
Lineto draws a line from the cp to (x, y), then moves the cp to (x, y).
2) moverel, moveto :
Declaration:
void far moverel(int dx, int dy);
void far moveto(int x, int y);
Remarks:
Moverel moves the current position (CP) dx pixels in the x direction and dy pixels in the
y direction.
Moveto moves the current position (CP) to viewport position (x, y).
1
Enrollment No.: 080410107081
Declaration:
void far arc(int x, int y, int stangle, int endangle, int radius);
void far circle(int x, int y, int radius);
void far pieslice(int x, int y, int stangle, int endangle, int radius);
Remarks:
Arc draws a circular arc in the current drawing color.
Circle draws a circle in the current drawing color.
Pieslice draws a pie slice in the current drawing color, then fills it using the current fill
pattern and fill color.
Declaration:
void far ellipse(int x, int y, int stangle, int endangle,int xradius, int yradius);
void far fillellipse(int x, int y,int xradius, int yradius);
void far sector(int x, int y, int stangle, int endangle,int xradius, int yradius);
Remarks:
Ellipse draws an elliptical arc in the current drawing color.
Fillellipse draws an ellipse, then fills the ellipse with the current fill color and fill pattern.
Sector draws and fills an elliptical pie slice in the current drawing color, then fills it using
the pattern and color defined by setfillstyle or setfillpattern.
Argument What It Is
---------------------------------------------------
(x,y) Center of ellipse
xradius Horizontal axis
2
Enrollment No.: 080410107081
5) getcolor, setcolor :
Declaration:
int far getcolor(void);
void far setcolor(int color);
Remarks:
Getcolor returns the current drawing color.
Setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
To select a drawing color with setcolor, you can pass either the color number or the
equivalent color name.
The drawing color is the value that pixels are set to when the program draws lines, etc.
For example, in CGAC0 mode (palette number 0), the palette contains four colors (background,
light green, light red, and yellow):
If getcolor returns 1, the current drawing color is light green.
Either setcolor(3) or setcolor(CGA_YELLOW) selects yellow as the drawing color.
Return Value:
Getcolor returns the current drawing color.
Setcolor does not return
6) imagesize :
Declaration:
Unsigned far imagesize(int left, int top, int right, int bottom);
Remarks:
Imagesize determines the size of the memory area required to store a bit Image.
3
Enrollment No.: 080410107081
Return Value:
On success, returns the size of the required memory area in bytes.
On error (if the size required for the selected image is >= (64K - 1) bytes), returns 0xFFFF (-1)
7) getpixel, putpixel :
Declaration:
unsigned far getpixel(int x, int y);
void far putpixel(int x, int y, int color);
Remarks:
Getpixel gets the color of the pixel located at (x,y).
Putpixel plots a point in the color defined by color at (x,y).
Return Value:
Getpixel returns the color of the given pixel
Putpixel does not return
8) getx, gety :
Declaration:
int far getx(void);
int far gety(void);
Remarks:
Getx returns the x-coordinate of the current graphics position.
Gety returns the y-coordinate of the current graphics position.
The values are viewport-relative.
Return Value:
Getx : x-coordinate of current position
Gety : y-coordinate of current position
9) getimage, putimage :
4
Enrollment No.: 080410107081
Declaration:
void far getimage(int left, int top, int right, int bottom,void far *bitmap);
void far putimage(int left, int top, void far *bitmap, int op);
Remarks:
getimage copies an image from the screen to memory.
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).
Right Top (left, top) is where putimage places the upper left corner of the stored image.
---------------------------------------------------------------------------------------------------------------------
op Specifies a combination operator that controls how the color for each destination
pixel onscreen is computed, based on the pixel already onscreen and the
corresponding source pixel in memory.
10)rectangle :
Declaration:
void far rectangle(int left, int top, int right, int bottom);
Remarks:
Rectangle draws a rectangle in the current line style, thickness, and drawing color.
(left,top) is the upper left corner of the rectangle, and (right,bottom) is its lower right
corner.
11)drawpoly, fillpoly :
Declaration:
void far drawpoly(int numpoints, int far *polypoints);
void far fillpoly(int numpoints, int far *polypoints);
5
Enrollment No.: 080410107081
Remarks:
Drawpoly draws a polygon using the current line style and color.
Fillpoly draws the outline of a polygon using the current line style and color, then fills the
polygon using the current fill pattern and fill color.
Each pair of integers gives the x and y coordinates of a point on the polygon.
To draw a closed figure with N vertices, you must pass N+1 coordinates to drawpoly,
where the Nth coordinate == the 0th coordinate.
12)setlinestyle :
Declaration:
void far setlinestyle(int linestyle, unsigned upattern, int thickness);
Remarks:
Setlinestyle sets the style for all lines drawn by line, lineto, rectangle, drawpoly, etc.
Return Value:
If invalid input is passed to setlinestyle, graphresult returns -11, and the current line style
remains unchanged.
Declaration:
int far getmaxx(void);
int far getmaxy(void);
Remarks:
Getmaxx returns the maximum x value (screen-relative) for the current graphics driver
and mode.
Getmaxy returns the maximum y value (screen-relative) for the current graphics driver
and mode.
For example, on a CGA in 320 x 200 mode, getmaxx returns 319 and getmaxy returns
199.
6
Enrollment No.: 080410107081
14)getmodename :
Declaration:
char * far getmodename(int mode_number);
Remarks:
Getmodename accepts a graphics mode number as input and returns a string containing
the name of the corresponding graphics mode.
The mode names are embedded in each driver.
The return values are useful for building menus or displaying status.
Return Value:
Getmodename returns a pointer to a string contining the name of the graphics mode.
15)getbkcolor, setbkcolor :
Declaration:
int far getbkcolor(void);
void far setbkcolor(int color);
Remarks:
Getbkcolor returns the current background color.
Setbkcolor sets the background to the color specified by color.
Color:
color is either a number or symbolic name specifying the color to set.
For example, if you want to set the background color to blue, you can call
setbkcolor(BLUE) /* or */ setbkcolor(1)
On CGA and EGA systems, setbkcolor changes the background color by changing the
first entry in the palette.
On an EGA or a VGA, if you call setpalette or setallpalette to change the palette colors,
the defined symbolic constants might not give the correct color.
This is because the color parameter to setbkcolor indicates the entry number in the
current palette, rather than a specific color. (Except 0, this always sets the background
color to black).
Return Value:
Getbkcolor returns the current background color.
Setbkcolor does not return.
7
Enrollment No.: 080410107081
PRACTICAL 2
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
char *msg;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
clrscr();
setcolor(BLACK);
line(100,100,200,100);
moveto(200,100);
lineto(150,200);
lineto(100,100);
moveto(100,180);
linerel(100,0);
moveto(200,180);
lineto(150,80);
lineto(100,180);
moveto(200,180);
sprintf(msg, "(%d,%d)", getx(), gety());
outtextxy(205,185,msg);
moveto(100,100);
sprintf(msg, "(%d,%d)", getx(), gety());
outtextxy(70,85,msg);
moveto(200,100);
sprintf(msg, "(%d,%d)", getx(), gety());
outtextxy(205,85,msg);
getch();
}
8
Enrollment No.: 080410107081
Output:
9
Enrollment No.: 080410107081
Program:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void main()
{
int gdriver = DETECT, gmode;
int x,y,length,breadth;
clrscr();
setcolor(BLACK);
rectangle(x,y,x+length,y+breadth);
getch();
}
Output:
10
Enrollment No.: 080410107081
Program:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
void main()
{
int gdriver = DETECT, gmode;
int midx, midy,color,maxcolor;
int radius = 100;
midx = getmaxx() / 2;
midy = getmaxy() / 2;
clrscr();
setcolor(BLACK);
pieslice(319,225,225,315,30); //nose
delay(200);
arc(319,275,180,0,30);//smile
delay(200);
line(226,200,319,2); //cap
line(412,200,319,2);
getch();
}
Output:
11
Enrollment No.: 080410107081
PRACTICAL 3
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode," ");
int xa,ya,xb,yb,dx,dy,x,y;
float xInc,yInc,steps;
char *loc;
clrscr();
setcolor(0);
printf("\n ENTER CO-RDINATES OF POINT>A>> ");
scanf("%d %d",&xa,&ya);
x=xa;
y=ya;
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xInc=dx/steps;
yInc=dy/steps;
12
Enrollment No.: 080410107081
putpixel(ROUND(x),ROUND(y),0);
for(int k=0;k<steps;k++)
{
if(k==0)
{ sprintf(loc,"(%d,%d)",xa,ya);
outtextxy(x,y,loc);
}
else if(k==(steps-1))
{
sprintf(loc,"(%d,%d)",xb,yb);
outtextxy(x,y,loc);
}
x+=xInc;
y+=yInc;
putpixel(ROUND(x),ROUND(y),0);
}
getch();
}
Output:
13
Enrollment No.: 080410107081
PRACTICAL 4
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode," ");
int xa,ya,xb,yb,dx,dy,x,y,p,twoDy,twoDyDx,xEnd,count=0;
char *loc;
clrscr();
setcolor(0);
printf("\n ENTER CO-RDINATES OF POINT>A>> ");
scanf("%d %d",&xa,&ya);
printf("\n ENTER CO-RDINATES OF POINT>B>> ");
scanf("%d %d",&xb,&yb);
dx=abs(xa-xb);
dy=abs(ya-yb);
p=2*dy-dx;
twoDy=2*dy;
twoDyDx=2*(dy-dx);
if(xa>xb)
{
x=xb;
y=yb;
xEnd=xa;
}
else
{
x=xa;
y=ya;
xEnd=xb;
}
14
Enrollment No.: 080410107081
putpixel(x,y,0);
while(x<xEnd)
{
x++;
if(count==0)
{
sprintf(loc,"(%d,%d)",xa,ya);
outtextxy(x,y,loc);
count++;
}
else if(x==(xEnd))
{
sprintf(loc,"(%d,%d)",xb,yb);
outtextxy(x,y,loc);
}
if(p<0)
{
p+=twoDy;
}
else
{
y++;
p+=twoDyDx;
}
putpixel(x,y,0);
}
getch();
}
Output:
15
Enrollment No.: 080410107081
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void Bresenhamn(int,int,int,int);
void main()
{
int x1,y1,x2,y2;
int w,width,i;
int gdriver = DETECT,gmode;
clrscr();
printf("Enter width:\n");
scanf("%d",&w);
width=((w-1)/2) * sqrt((pow(x2-x1,2)+pow(x2-y1,2))/abs(x2-x1));
Bresenhamn(x1,y1,x2,y2);
for(i=-width;i<=width;i++)
{
Bresenhamn(x1,y1+i,x2,y2+i);
}
getch();
}
16
Enrollment No.: 080410107081
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,0);
while(x<xend)
{
x++;
if(p<0)
{
p+=2*dy;
}
else
{
y++;
p+=2*(dy-dx);
}
putpixel(x,y,0);
}
}
Output:
17
Enrollment No.: 080410107081
PRACTICAL 5
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int i,j;
int gdriver = DETECT,gmode;
int s[8][8]={
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1}
};
int h[8][8]={ {1,1,0,0,0,0,1,1},
{1,1,0,0,0,0,1,1},
{1,1,0,0,0,0,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,0,0,0,0,1,1},
{1,1,0,0,0,0,1,1},
{1,1,0,0,0,0,1,1},
};
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(s[i][j])
putpixel(j+150,i+100,BLACK);
18
Enrollment No.: 080410107081
if(h[i][j])
putpixel(j+200,i+100,BLACK);
}
}
getch();
}
Output:
19
Enrollment No.: 080410107081
PRACTICAL 6
Program:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>
void main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
int x1,y1,x2,y2,x,y;
clrscr();
printf("\n ENTER LEFT TOP CORNER>> ");
scanf("%d %d",&x1,&y1);
printf("\n ENTER RIGHT BOTTOM CORNER>> ");
scanf("%d %d",&x2,&y2);
printf("\n ENTER FILL CO-ORDINATES>> ");
scanf("%d %d",&x,&y);
rectangle(x1,y1,x2,y2);
bf(x,y,4,15);
getch();
}
20
Enrollment No.: 080410107081
Output:
21
Enrollment No.: 080410107081
Program:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void ff(int,int,int,int);
void main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
int x1,y1,x2,y2,x,y;
clrscr();
rectangle(x1,y1,x2,y2);
ff(x,y,BLACK,15);
getch();
}
22
Enrollment No.: 080410107081
ff(x+1,y-1,oc,nc);
ff(x-1,y+1,oc,nc);
}
}
Output:
23
Enrollment No.: 080410107081
PRACTICAL 7
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
clrscr();
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");
int x,y,count,i,maxx,maxy,poly[10];
char *loc;
setcolor(getmaxcolor());
maxx = getmaxx();
maxy = getmaxy();
poly[8] = poly[0];
poly[9] = poly[1];
24
Enrollment No.: 080410107081
drawpoly(5, poly);
getch();
count=0;
for(i=10;i<=x;i++)
{
if(getpixel(i,y)==getmaxcolor())
{
count++;
}
}
if(count%2==0)
{
printf("\n\n\n The point lies outside the polygon");
}
else
{
printf("\n\n\n The point lies inside the polygon");
}
getch();
}
Output:
25
Enrollment No.: 080410107081
26
Enrollment No.: 080410107081
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int main()
{
clrscr();
int gdriver = DETECT, gmode;
char *loc;
int x,y,count=0,i,j;
int a[6][5]={{60,60,100,40,1},{100,40,140,60,-1},{140,60,140,100,-},
{140,100,100,80,1},{100,80,60,100,-1},{60,100,60,60,1}};
setcolor(getmaxcolor());
moveto(60,60);
lineto(60,100);
lineto(100,80);
lineto(140,100);
lineto(140,60);
lineto(100,40);
lineto(60,60);
for(i=10;i<=x;i++)
{
if(getpixel(i,y)==getmaxcolor())
{
for(j=0;j<6;j++)
{
if((((i<=a[j][0])&&(i>=a[j][2])) ||
((i>=a[j][0])&&(i<=a[j][2])))&&(((y<=a[j][1])&&(y>=a[j][3])) ||
((y>=a[j][1])&&(y<=a[j][3]))))
{
break;
27
Enrollment No.: 080410107081
}
}
count+=a[j][4];
}
}
if(count%2==0)
{
printf("\n\n\n\n\n The point lies outside the polygon");
}
else
{
printf("\n\n\n\n\n The point lies inside the polygon");
}
getch();
}
Output:
28
Enrollment No.: 080410107081
PRACTICAL 8
Program:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void circlePlotPoints(int,int,int,int);
void main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode," ");
int x=0,y,radius,p,xCenter,yCenter;
clrscr();
y=radius;
p=1-radius;
circlePlotPoints(xCenter,yCenter,x,y);
while(x<y)
{
x++;
if(p<0)
{
p+=2*x+1;
}
else
{
y--;
p+=2*(x-y)+1;
}
circlePlotPoints(xCenter,yCenter,x,y);
}
getch();
}
29
Enrollment No.: 080410107081
Output:
30
Enrollment No.: 080410107081
Program:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void ellipsePlotPoints(int,int,int,int);
void main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode," ");
int xCenter,yCenter,Rx,Ry,Rx2,Ry2,twoRx2,twoRy2,p,x,y,px,py;
clrscr();
Rx2=Rx*Rx;
Ry2=Ry*Ry;
twoRx2=2*Rx2;
twoRy2=2*Ry2;
x=0;
y=Ry;
px=0;
py=twoRx2*y;
ellipsePlotPoints(xCenter,yCenter,x,y);
p=ROUND(Ry2-(Rx2*Ry)+(0.25*Rx2));
while(px<py)
{
x++;
px +=twoRy2;
if(p<0)
{
p+=Ry2+px;
}
31
Enrollment No.: 080410107081
else
{
y--;
py -=twoRx2;
p+=Ry2+px-py;
}
ellipsePlotPoints(xCenter,yCenter,x,y);
}
while(y>0)
{
y--;
py-=twoRx2;
if(p>0)
{
p+=Rx2-py;
}
else
{
x++;
px+=twoRy2;
p+=Rx2-py+px;
}
ellipsePlotPoints(xCenter,yCenter,x,y);
}
getch();
}
Output:
32