0% found this document useful (0 votes)
78 views

Practical 1: Aim: To Study All The Graphics Function

The document describes various graphics functions in C including: 1) Functions for drawing lines, arcs, and circles like line, arc, circle. 2) Functions for manipulating the current position like moverel, moveto. 3) Functions for getting and setting color values like getcolor, setcolor. 4) Functions for working with bit images like getimage, putimage. 5) Additional functions for drawing shapes and getting pixel values.

Uploaded by

Sachin Shah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Practical 1: Aim: To Study All The Graphics Function

The document describes various graphics functions in C including: 1) Functions for drawing lines, arcs, and circles like line, arc, circle. 2) Functions for manipulating the current position like moverel, moveto. 3) Functions for getting and setting color values like getcolor, setcolor. 4) Functions for working with bit images like getimage, putimage. 5) Additional functions for drawing shapes and getting pixel values.

Uploaded by

Sachin Shah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Enrollment No.

: 080410107081

PRACTICAL 1

Aim: To study all the Graphics function.

1) line, linerel, lineto :

 Line draws a line between two specified points


 Linerel draws a line a relative distance from the current position (CP)
 Lineto draws a line from the current position (CP) to (x,y)

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 :

 Moverel moves the current position (CP) a relative distance


 Moveto moves the CP to (x, y)

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

Return Value: None

3) arc, circle, pieslice :

 Arc draws a circular arc


 Circle draws a circle

1
Enrollment No.: 080410107081

 Pieslice draws and fills a circular pie slice

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.

Argument What It Is/Does


------------------------------------------------------------------
(x,y) Center point of arc, circle, or pie slice
stangle Start angle in degrees
endangle End angle in degrees
radius Radius of arc, circle, and pieslice

 The arc or slice travels from stangle to endangle.


 If stangle = 0 and endangle = 360, the call to arc draws a complete circle.
 If your circles are not perfectly round, use setaspectratio to adjust the aspect ratio.

4) ellipse, fillellipse, sector :

 Ellipse draws an elliptical arc


 Fillellipse draws and fills an ellipse
 Sector draws and fills an elliptical pie slice

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

yradius Vertical axis


stangle Starting angle
endangle Ending angle

 The ellipse or sector travels from stangle to endangle.


 If stangle = 0 and endangle = 360, the call to ellipse draws a complete ellipse.

5) getcolor, setcolor :

 Getcolor returns the current drawing color


 Setcolor sets the current drawing color

Declaration:
 int far getcolor(void);
 void far setcolor(int color);

Remarks:
 Getcolor returns the current drawing color.
 Setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
 To 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.

In CGAC3 mode, if getcolor returns 1, the current drawing color is cyan.

Return Value:
 Getcolor returns the current drawing color.
 Setcolor does not return

6) imagesize :

 Returns the number of bytes required to store a bit image

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 :

 Getpixel gets the color of a specified pixel


 Putpixel plots a pixel at a specified point

Declaration:
 unsigned far getpixel(int x, int y);
 void far putpixel(int x, int y, int color);

Remarks:
 Getpixel gets the color of the pixel located at (x,y).
 Putpixel plots a point in the color defined by color at (x,y).

Return Value:
 Getpixel returns the color of the given pixel
 Putpixel does not return

8) getx, gety :

 Getx returns the current position's x coordinate.


 Gety returns the current position’s y coordinate.

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 :

 Getimage saves a bit image of the specified region into memory


 Putimage outputs a bit image onto the screen

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

Argument What It Is/Does


---------------------------------------------------------------------------------------------------------------------
bitmap Points to the area in memory where the bit image is stored.
The first two words of this area are used for the width and height of the
Rectangle. The remainder holds the image itself.
---------------------------------------------------------------------------------------------------------------------
bottom left (left, top) and (right, bottom) define the rectangular screen area from which
getimage copies the bit image.

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 :

 Draws a rectangle (graphics mode)

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 :

 Drawpoly draws the outline of a polygon


 Fillpoly draws and fills a polygon

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.

Argument What It Does


-----------------------------------------------------------------------------------------
numpoints Specifies number of points
*polypoints Points to a sequence of (numpoints x 2) integers

 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 :

 Sets the current line style and width or pattern

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.

13)getmaxx and getmaxy :

 Returns maximum x or y screen coordinate

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.

Return Value: Getmaxx: maximum x screen coordinate.

6
Enrollment No.: 080410107081

14)getmodename :

 Returns the name of a specified graphics mode

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 :

 Getbkcolor returns the current background color


 Setbkcolor sets the current background color using the palette

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

Aim(A):Draw a star using line,linerel & lineto function.


Program:

#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);

sprintf(msg, "(%d,%d)", getx(), gety());


outtextxy(75,190,msg);

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

Aim(B):Draw a rectangle with one side co-ordinates given.

Program:

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

void main()
{
int gdriver = DETECT, gmode;
int x,y,length,breadth;

initgraph(&gdriver, &gmode, "");

clrscr();

setcolor(BLACK);

printf("enter co-ordinate: ");


scanf("%d %d",&x,&y);

printf("enter length & breadth: ");


scanf("%d %d",&length,&breadth);

rectangle(x,y,x+length,y+breadth);

getch();
}

Output:

10
Enrollment No.: 080410107081

Aim(C): Implement Face Animation.

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;

initgraph(&gdriver, &gmode, "");

midx = getmaxx() / 2;
midy = getmaxy() / 2;
clrscr();
setcolor(BLACK);

circle(midx, midy, radius); //face


delay(200);
circle(281,200,15); //eyes
circle(357,200,15);
delay(200);

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

Aim:Draw a line using DDA algorithm.

Program:

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

#define ROUND(a) ((int)(a+0.5))

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);

printf("\n ENTER CO-RDINATES OF POINT>B>> ");


scanf("%d %d",&xb,&yb);

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

Aim(A):Draw a line using BNS algorithm.

Program:

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

#define ROUND(a) ((int)(a+0.5))

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

Aim(B):Draw Thick Line-segment.

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();

initgraph(&gdriver, &gmode, "");


clrscr();
setcolor(BLACK);

printf("Enter the value X1 & Y1:\n");


scanf("%d",&x1);
scanf("%d",&y1);

printf("Enter the value X2 & Y2:\n");


scanf("%d",&x2);
scanf("%d",&y2);

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();
}

void Bresenhamn(int x1,int y1,int x2,int y2)


{
int dx,dy,x,y;
int p,xend;

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

Aim:Print character using Dot-matrix method.

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},
};

initgraph(&gdriver, &gmode, "");


clrscr();

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

Aim(A):Implement Boundary-Fill algorithm.

Program:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>

void bf(int a,int b,int f,int bd);

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();
}

void bf(int x,int y,int fill,int bdy)


{
int c;
c=getpixel(x,y);
if(c!=bdy && c!=fill)
{
setcolor(fill); delay(1);
putpixel(x,y,fill);
bf(x+1,y,fill,bdy);
bf(x-1,y,fill,bdy);
bf(x,y+1,fill,bdy);
bf(x,y-1,fill,bdy);
}
}

20
Enrollment No.: 080410107081

Output:

21
Enrollment No.: 080410107081

Aim(B): Implement Flood-Fill algorithm.

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();

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);
ff(x,y,BLACK,15);
getch();
}

void ff(int x,int y,int oc,int nc)


{
int c;
c=getpixel(x,y);
if(c==oc)
{
setcolor(nc);
delay(1);
putpixel(x,y,nc);
ff(x+1,y,oc,nc);
ff(x-1,y,oc,nc);
ff(x,y+1,oc,nc);
ff(x,y-1,oc,nc);
ff(x+1,y+1,oc,nc);
ff(x-1,y-1,oc,nc);

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

Aim(A):Implement Odd-Even Polygon Fill algorithm.

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;

printf("\n Enter the point to be checked:");


scanf("%d%d",&x,&y);
sprintf(loc,".(%d,%d)",x,y);
outtextxy(x,y,loc);

setcolor(getmaxcolor());

maxx = getmaxx();
maxy = getmaxy();

poly[0] = 20; /* 1st vertext */


poly[1] = maxy / 2;

poly[2] = maxx - 20; /* 2nd */


poly[3] = 20;

poly[4] = maxx - 50; /* 3rd */


poly[5] = maxy - 20;

poly[6] = maxx / 2; /* 4th */


poly[7] = maxy / 2;

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

Aim(B): Implement Winding Number algorithm.

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}};

initgraph(&gdriver, &gmode, "");


printf("\n Enter the point to be checked:");
scanf("%d%d",&x,&y);
sprintf(loc,".(%d,%d)",x,y);
outtextxy(x,y,loc);

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

Aim(A):Implement Midpoint Circle algorithm.

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();

printf("\n ENTER CENTER (x,y)>> ");


scanf("%d %d",&xCenter,&yCenter);
printf("\n\n ENTER RADIUS>> ");
scanf("%d",&radius);

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

void circlePlotPoints(int xCenter,int yCenter,int x,int y)


{
putpixel(xCenter+x,yCenter+y,0);
putpixel(xCenter-x,yCenter+y,0);
putpixel(xCenter+x,yCenter-y,0);
putpixel(xCenter-x,yCenter-y,0);
putpixel(xCenter+y,yCenter+x,0);
putpixel(xCenter-y,yCenter+x,0);
putpixel(xCenter+y,yCenter-x,0);
putpixel(xCenter-y,yCenter-x,0);
}

Output:

30
Enrollment No.: 080410107081

Aim (B): Implement Midpoint Ellipse algorithm.

Program:

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

#define ROUND(a) ((int)(a+0.5))

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();

printf("\n ENTER CENTER (x,y)>> ");


scanf("%d %d",&xCenter,&yCenter);
printf("\n\n ENTER Major & Minor AXIS>> ");
scanf("%d %d",&Rx,&Ry);

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);
}

p=ROUND(Ry2*(x+0.5)*(x+0.5)+ Rx2*(y-1)*(y-1)- Rx2*Ry2);

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();
}

void ellipsePlotPoints(int xCenter,int yCenter,int x,int y)


{
putpixel(xCenter+x,yCenter+y,0);
putpixel(xCenter-x,yCenter+y,0);
putpixel(xCenter+x,yCenter-y,0);
putpixel(xCenter-x,yCenter-y,0);
}

Output:

32

You might also like