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

Computer Graphics LAB FILE

This document is a computer graphics lab file submitted by a student named Ashish to their professor, Ms. Renuka Nagpal. It contains: 1. An index listing 14 mandatory experiments and 2 design-based open-ended experiments completed by the student, along with their dates of allotment and evaluation. 2. Code and summaries for 9 basic graphics functions implemented by the student, including initgraph(), line(), circle(), rectangle(), floodfill(), and DDA line algorithm. 3. A conclusion that the student learnt the basic graphics.h functions and implemented several to create graphics. The file demonstrates the student's completion of graphics experiments for their computer science course.

Uploaded by

Ashish Dahiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Computer Graphics LAB FILE

This document is a computer graphics lab file submitted by a student named Ashish to their professor, Ms. Renuka Nagpal. It contains: 1. An index listing 14 mandatory experiments and 2 design-based open-ended experiments completed by the student, along with their dates of allotment and evaluation. 2. Code and summaries for 9 basic graphics functions implemented by the student, including initgraph(), line(), circle(), rectangle(), floodfill(), and DDA line algorithm. 3. A conclusion that the student learnt the basic graphics.h functions and implemented several to create graphics. The file demonstrates the student's completion of graphics experiments for their computer science course.

Uploaded by

Ashish Dahiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Computer Graphics LAB FILE

Amity University Uttar Pradesh

Submitted To:

Submitted By:

Ms. Renuka Nagpal

Ashish

Department of Computer Science

A2305214229

ASET(AUUP)

B.Tech CSE
4-CSE-4X

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


AMITY SCHOOL OF ENGINEERING AND TECHNOLOGY
AMITY UNIVERSITY UTTAR PRADESH
NOIDA (U.P.)
2015

INDEX
S.NO

Category
Code
of
assignment

Exp
no.

Name
Of Experiment

Date of Date of
allotmen Evaluatio
t
n

MaxMark
s

Mandatory
Experimen
t

LR(10) 1

Mandatory
Experimen
t

LR(10) 2

Mandatory
Experimen
t

LR(10) 3

Mandatory
Experimen
t

LR(10) 4

Mandatory
Experimen
t

LR(10) 5

Marks
Obtaine
d

Signatur
of
Faculty

Mandatory
Experimen
t

LR(10) 6

Mandatory
Experimen
t

LR(10) 7

Mandatory
Experimen
t

LR(10) 8

Mandatory
Experimen
t

LR(10) 9

Mandatory
Experimen
t

LR(10) 10

Mandatory
Experimen
t

LR(10) 11

Mandatory
Experimen
t

LR(10) 12

Mandatory
Experimen
t

LR(10) 13

14

Design
PR(10) 15
based open
Ended
Experimen
t
Design
PR(10) 16
based open
Ended
Experimen
t
Viva
Viva(5
)

15.

10

16.

10

Detailed Marks Criteria for VIVA

Criteria
Clarity of the
subject
Quality of
theoretical
discussion
Total

Total Marks
2

Marks Obtained

Comments

A Pre-Note:

Note: All the codes mentioned have been written as per


latest C ISO/IEC 9899:2011 guidelines and compiled
using GNU/GCC compiler Code Blocks IDE and Turbo C.

All the code written our original and compiled.

Aim: To write short summary of basic Graphics functions


DATE: 4.1.16
SOFTWARE USED: Turbo C
Summary :

1. INITGRAPH:
Initializes the graphics system.

FUNCTION PROTOTYPE
void far initgraph(int far* graphdriver)

WORKING
To start the graphics system, you must first call the initgraph function
Initgraph initializes the graphic system by loading a graphics driver
from disk (or validating a registered driver) then putting the system into
the graphics mode.
Initgraph also resets all the graphic settings (colour, pallete, current
position, viewport, etc) to their deports and then resets graph.
2. CLOSEGRAPH:
Shuts down the graphics system

FUNCTION PROTOTYPE
void far closegraph(void)

WORKING
Closegraph deallocates all memory allocated by the graphics system
It then restores the screen to the mode it was in before you called
initgraph

RETURN VALUE
None
3. FLOODFILL:

Floodfill fills a bounded region

FUNCTION PROTOTYPE
void far floodfill(int x, int y, int border)

WORKING
Floodfill fills an enclosed area on a bitmap device
The area bounded by the colour border is flooded with the current fill
pattern and fil colour
Use fillpoly instead of floodfill wherever possible so you can maintain
code compatibility with future versions
Floodfill does not work with the IBM-8514 driver
(x, y) is seed point:
If the seed is within an enclosed area, the inside will be filled.
If the seed is outside an enclosed area, the exterior will be filled.

RETURN VALUE
If an error occurs while filling the region,, graph result returns 1

4. GETCOLOUR AND SETCOLOUR:


Getcolour returns the current drawing colour
Setcolour sets the current drawing colour

FUNCTION PROTOTYPE
Int far getclour(void)

Void far setcolour(int colour)

WORKING
Getcolour returns the current drawing colour
Setcolour sets the current drawing colour to colour, which can range
from 0 to getmaxcolour
To set a drawing colour with setcolor, you can pass either the colour
number or the equivalent colour name

5. GETPIXEL AND PUTPIXEL:


Getpixel gets the colour of a specified pixel
Putpixel places a pixel at a specified point

FUNCTION PROTOTYPE
Unsigned far getpixel(int x, int y)
Void far putpixel(int x, int y, int colour)

WORKING
Getpixel gets the colour of a specified pixel located at (x, y)
Putpixel plots a point in the colour defined at (x, y)

RETURN VALUE
Getpixel returns the colour of the given pixel
Putpixel does not return anything

Implementation:
Here we printed a line by choosing a starting a point and using PUTPIXEL.

6. ARC, CIRCLE AND PIESLICE:

arc draws a circular arc


Circle draws a circle
Pieslice draws and fills a circular pie slice

FUNCTION PROTOTYPE:
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)

WORKING
Arc draws a circular arc in the current drawing colour
Circle draws a circle in the current drawing colour
Pieslice draws a pie slice in the current drawing colour, then fills it
using, the current fill pattern and fill colour

7. ELLIPSE, FILLELLIPSE AND SECTOR:


Ellipse draws an elliptical arc
Fillellipse draws and fills ellipse
Sector draws and fills an elliptical pie slice

FUNCTION PROTOTYPE
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)

WORKING
Ellipse draws an elliptical arc in the current drawing colour
Fillellipse draws an elliptical arc in the current drawing colour and then
fills it with fill colour and fill pattern
Sector draws an elliptical pie slice in the current drawing colour and
then fills it using

the pattern and colour defined by setfillstyle or setfillpattern

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

int main()
{
clrscr();

/* request auto detection */


int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 0, endangle = 360;
int xradius = 100, yradius = 50;
int radius = 100;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c://TURBOC3//BGI");

midx = getmaxx() / 2;

midy = getmaxy() / 2;
setcolor(getmaxcolor());

setfillstyle(EMPTY_FILL, getmaxcolor());
pieslice(100, 100, 45, 135, 100);
arc(500, 100,45,135, 100);
sector(100, 450, 45, 135,50,100);
ellipse(525, 400, stangle, endangle,xradius, yradius);

for(int i=0;i<5;i++)
{
delay(400);
circle(midx, midy, radius+i);

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

Output:
Various Geometrical Figures Drawn using their respective functions

8. LINE, LINEREL AND LINETO:


Line draws a line between two specified points
Linerel draws a line at a relative distance from current position (CP)
Lineto draws a line from current position (CP) to (x, y)

FUNCTION PROTOTYPE
Void far line(int x1, int y1, int x2, int y2)
void far lineto(int x, int y)

WORKING
Line draws a line from (x1, y1) to (x2, y2) using the current colour, line
style and thickness. It does not update the current position CP.
Linerel draws a line from the CP to the point that is relative distance (dx,
dy) from CP, then advances the CP by (dx, dy)
Lineto draws a line from CP to (x, y) and the advances the CP to (x, y)
RETURN VALUE
None

Implementation:
Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

int xmax, ymax;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c://TC//BGI");
setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();

/*draw a line relative to moveto() coordinate*/


moveto(200, 300);
linerel(100, 100);
outtextxy(300,400,"THIS IS A LINE FROM LINEREL() FUNCTION");
/* draw a diagonal line */

line(20,20, xmax/2, ymax/2);


outtextxy(xmax/2,ymax/2,"THIS IS A LINE FROM LINE()
FUNCTION");

moveto(600, 30);
lineto(260, 120);
outtextxy(260,120,"THIS IS A LINE FROM LINETO() FUNCTION");

/* clean up */
getch();
closegraph();
return 0;

Output:

9. RECTANGLE:
Draws a rectangle in graphics mode

DECALARTION
void far rectangle(int left, int top, int right, int bottom)

WORKING
It draws a rectangle in the current line style, thickness and drawing
colour
(left, top) is the upper left corner of the rectangle and (right, bottom) is
its lower right corner
RETURN VALUE
None

Implementation:
Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;

/* initialize graphics and local variables */

initgraph(&gdriver, &gmode, "c://TC//BGI");

/* read result of initialization */


errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

left = getmaxx() / 2 - 50;


top = getmaxy() / 2 - 50;
right = getmaxx() / 2 + 50;
bottom = getmaxy() / 2 + 50;

/* draw a rectangle */
rectangle(left,top,right,bottom);

/* clean up */
getch();
closegraph();
return 0;
}

Output:

RESULT: Through this we implemented and learnt the basic Graphics.h


functions
CONCLUSIONS : Above are several functions to implement graphics in
C.
SIGNATURE :

Criteria

Total Marks

Concept (A)
Implementation
(B)
Performance (C)
Total

2
2

Marks
Obtained

2
6

Aim: To implement DDA Line Algorithm


DATE: 11.1.16

Comments

SOFTWARE USED: Turbo C


Source Code:
#include <graphics.h>
#include <stdio.h>
#include <math.h>

int main( )
{
float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i,gd,gm;
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

printf("Enter the value of x1 : ");


scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y1);
printf("Enter the value of x2 : ");
scanf("%f",&x2);
printf("Enter the value of y2 : ");
scanf("%f",&y2);

detectgraph(&gd,&gm);

/* initialize graphics and local variables */

initgraph(&gdriver, &gmode, "c://TURBOC3//BGI");

/* read result of initialization */


errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>=dy)
pixel=dx;
else
pixel=dy;

Xinc = dx/pixel;
Yinc = dy/pixel;

x=x1;
y=y1;

i=1;
while(i<=pixel)
{
putpixel(x,y,1);
x=x+Xinc;
y=y+Yinc;
i=i+1;
delay(100);
}
getch();
closegraph();
return 0;
}

Output:

RESULT: Through this we implemented DDA Line Algorithm


CONCLUSIONS : Above is the working of DDA Algorithm

SIGNATURE :

Criteria

Total Marks

Concept (A)
Implementation
(B)
Performance (C)
Total

2
2

Marks
Obtained

2
6

Aim: To implement Bresenhams Line Algorithm


DATE: 18.1.16
SOFTWARE USED: Turbo C
Source Code:

Comments

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

void draw(int, int, int, int);

int main()
{
int x1, x2, y1, y2;
int i,gd,gm;
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;

detectgraph(&gd,&gm);
initgraph(&gdriver, &gmode, "c://TURBOC3//BGI");

/* read result of initialization */


errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();

exit(1); /* terminate with an error code */


}

printf("Enter values of x1 and y1 : ");


printf("x1:");
scanf("%d",&x1);
printf("y1:");
scanf("%d",&y1);
printf("Enter values of x2 and y2 : ");
printf("x2:");
scanf("%d",&x2);
printf("y2:");
scanf("%d",&y2);

draw(x1, y1, x2, y2);


getch();
}

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


{

float dx, dy, p;


int i, x, y, xend;
cleardevice();
dx = x2 - x1;

dy = 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, RED);
while(x < xend)
{
if(p < 0)
{
x = x + 1;
putpixel(x, y, 10);
p = p + (2*dy);
}
else
{

x = x + 1;
y = y + 1;
putpixel(x, y, 10);
p = p + (2*dy) - (2*dx);
}
}
}

Output :

RESULT: Through this we implemented Bresenhams Line Algorithm


CONCLUSIONS : Above is how we implement Bresenhams Algorithm

SIGNATURE :

Criteria

Total Marks

Concept (A)
Implementation
(B)
Performance (C)
Total

2
2

Marks
Obtained

2
6

Aim: To draw a Circle using Mid-Point Algorithm


DATE: 25.1.16
SOFTWARE USED: Turbo C
Source Code:

Comments

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void circlemid(int,int,int);
void circlepixel(int,int,int,int);
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int r=200;
int x=250,y=250;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
circlemid(x,y,r);
getch();
}
void circlemid(int xcenter, int ycenter, int radius)
{
int x=0;
int y=radius;
int f=1-radius;
circlepixel(xcenter,ycenter,x,y);
while(x<y)
{
x++;
if(f<0)
f+=2*x+1;
else
{
y--;
f+=2*(x-y)+1;
}
circlepixel(xcenter,ycenter,x,y);
}
}
void circlepixel(int xcenter, int ycenter,int x,int y)
{
putpixel(xcenter + x,ycenter + y,RED);
putpixel(xcenter - x,ycenter + y,YELLOW);
putpixel(xcenter + x,ycenter - y,CYAN);

putpixel(xcenter - x,ycenter - y,GREEN);


putpixel(xcenter + y,ycenter + x,MAGENTA);
putpixel(xcenter - y,ycenter + x,BLUE);
putpixel(xcenter + y,ycenter - x,WHITE);
putpixel(xcenter - y,ycenter - x,BROWN);
}

Output :

RESULT: Therefore we implemented we drew a circle using bresenhams


algorithm
CONCLUSIONS : Above is how we implement bresenhams algorithm
for a circle.
SIGNATURE :

Criteria

Total Marks

Concept (A)
Implementation
(B)
Performance (C)
Total

2
2

Marks
Obtained

2
6

Aim: To draw an ellipse using Mid Point Algorithm


DATE: 1.2.16
SOFTWARE USED: Turbo C
Source Code:
#include<stdio.h>

Comments

#include<stdlib.h>
#include<graphics.h>
#include<dos.h>

using namespace std;

void DrawPoints(int x, int y, int xc, int yc)


{
putpixel(xc+x,yc+y, CYAN);
putpixel(xc-x,yc+y, CYAN);
putpixel(xc+x,yc-y, CYAN);
putpixel(xc-x,yc-y, CYAN);
}
void Eclipse(int xc, int yc, float a, float b)
{
int x=0;
int y=b;
float P,P2;
DrawPoints(x,y,xc,yc);

P = b*b - a*a*b + 0.25*a*a;

while( 2*b*b*x <= 2*a*a*y )


{
if(P < 0)
{

DrawPoints(++x, y, xc, yc);


delay(20);
P=P+ 2*b*b*x + b*b;
}
else
{
DrawPoints(++x,--y, xc, yc);
delay(20);
P=P+ 2*b*b*x - 2*a*a*y + b*b;
}

P2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;

while( y >= 0 ) //x!=a || y!=0 );


{
if(P2 < 0)
{
DrawPoints(++x,--y, xc, yc);
P2=P2+ 2*b*b*x - 2*a*a*y + a*a;
}
else
{
DrawPoints(x,--y, xc, yc);
P2=P2 - 2*a*a*y + a*a;

}
}
}

int main()
{
float a,b;
int xc,yc;

int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

xc=getmaxx()/2;
yc=getmaxy()/2;

printf("\n\n\tEnter Major axis (a):");


scanf("%d",&a);
printf("and Minor axis (b):" );
scanf("%d",&a);

Eclipse(xc,yc,a,b);
system("pause");

closegraph();
return 0;
}

Output :

RESULT: Through this we implemented Mid Point Algorithm for ellipse.


CONCLUSIONS : Above is the working of Mid Point Algorithm for ellipse.

SIGNATURE :

Criteria

Total Marks

Concept (A)
Implementation
(B)
Performance (C)
Total

2
2
2
6

Marks
Obtained

Comments

You might also like