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

Computer Graphics Lab 1

The document describes experiments conducted using the graphics.h library in C. It aims to study the graphics.h library and its functions. The graphics.h library provides functions to draw various shapes and images onto a graphical window. It lists and describes several functions for drawing lines, rectangles, ovals, arcs, polygons, and strings. It then provides 4 examples demonstrating how to use some of the graphics.h functions to set text position and color, adjust text size, and draw arcs of varying radii.

Uploaded by

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

Computer Graphics Lab 1

The document describes experiments conducted using the graphics.h library in C. It aims to study the graphics.h library and its functions. The graphics.h library provides functions to draw various shapes and images onto a graphical window. It lists and describes several functions for drawing lines, rectangles, ovals, arcs, polygons, and strings. It then provides 4 examples demonstrating how to use some of the graphics.h functions to set text position and color, adjust text size, and draw arcs of varying radii.

Uploaded by

Miraj Ahemad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Experiment 1

Aim: to study about graphics.h library in C

graphics.h: This interface provides access to a simple graphics library that makes it
possible to draw lines, rectangles, ovals, arcs, polygons, images, and strings on a graphical
window.

Functions
initGraphics()
Creates the graphics window on the screen.
initGraphics(width, height)
drawArc(bounds, start, sweep) Draws an elliptical arc inscribed in a
drawArc(x, y, width, height, start, sweep) rectangle.
fillArc(bounds, start, sweep)
Fills a wedge-shaped area of an elliptical arc.
fillArc(x, y, width, height, start, sweep)
drawImage(filename, pt)
drawImage(filename, x, y) Draws the image from the specified file with
drawImage(filename, bounds) its upper left corner at the specified point.
drawImage(filename, x, y, width, height)
Returns the bounds of the image contained in
getImageBounds(filename)
the specified file.
drawLine(p0, p1)
Draws a line connecting the specified points.
drawLine(x0, y0, x1, y1)
drawPolarLine(p0, r, theta) Draws a line of length r in the
drawPolarLine(x0, y0, r, theta) direction theta from the initial point.
drawOval(bounds) Draws the frame of a oval with the specified
drawOval(x, y, width, height) bounds.
fillOval(bounds) Fills the frame of a oval with the specified
fillOval(x, y, width, height) bounds.
drawRect(bounds) Draws the frame of a rectangle with the
drawRect(x, y, width, height) specified bounds.
fillRect(bounds) Fills the frame of a rectangle with the
fillRect(x, y, width, height) specified bounds.
drawPolygon(polygon)
drawPolygon(polygon, pt) Draws the outline of the specified polygon.
drawPolygon(polygon, x, y)
fillPolygon(polygon)
fillPolygon(polygon, pt) Fills the frame of the specified polygon.
fillPolygon(polygon, x, y)
drawString(str, pt) Draws the string str so that its baseline origin
drawString(str, x, y) appears at the specified point.
Returns the width of the string str when
getStringWidth(str)
displayed in the current font.
setFont(font) Sets a new font.
getFont() Returns the current font.
setColor(color) Sets the color used for drawing.
Returns the current color as a string in the
getColor()
form "#rrggbb".
saveGraphicsState() Saves the state of the graphics context.
Restores the graphics state from the most
restoreGraphicsState()
recent call to saveGraphicsState().
Returns the width of the graphics window in
getWindowWidth()
pixels.
Returns the height of the graphics window in
getWindowHeight()
pixels.
Issues a request to update the graphics
repaint()
window.
Pauses for the indicated number of
pause(milliseconds)
milliseconds.
Waits for a mouse click to occur anywhere in
waitForClick()
the window.
setWindowTitle(title) Sets the title of the primary graphics window.
Returns the title of the primary graphics
getWindowTitle()
window.
Closes the graphics window and exits from
exitGraphics() the application without waiting for any
additional user interaction.
Example 1: To set Text Position.
#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<graphics.h>

//outtextxy

int main()

int gd=DETECT,gm,error;

initgraph(&gd,&gm,"c:\\tc\\bgi");

error = graphresult();

if (error != grOk)

printf("Graphics error occurred");

printf("Press any key to halt:");

getch();

exit(1);

outtextxy(175,250,"Welcome!!! Meraj Ahmad Mirza");

getch();

closegraph();

return 0;

}
Output
Example 2: To set Background Colour.
#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<graphics.h>

//outtextxy

int main()

int gd=DETECT,gm,error;

initgraph(&gd,&gm,"c:\\tc\\bgi");

error = graphresult();

if (error != grOk)

printf("Graphics error occurred");

printf("Press any key to halt:");

getch();

exit(1);

setbkcolor(BLUE);

outtextxy(175,250,"Welcome!!! Meraj Ahmad Mirza");

getch();

closegraph();

return 0;

}
Output
Example 3: To set text Size.
#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<graphics.h>

int main()

int gd=DETECT,gm,i,j=0,error,maximumcolor;

char message[80];

initgraph(&gd,&gm,"c:\\tc\\bgi");

error = graphresult();

if (error != grOk)

printf("Graphics error occurred");

printf("Press any key to halt:");

getch();

exit(1);

setbkcolor(BLUE);

for (i=1; i<11; i++)

/* select the text style, direction, and size */

settextstyle(SANS_SERIF_FONT, HORIZ_DIR, i);

sprintf(message, "Size: %d", i);

outtextxy(1,j, message);

/* advance to the next text line */

j += textheight(message);

}
getch();

closegraph();

return 0;

Output
Example 4: To draw graphics.
#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<graphics.h>

int main()

int gd=DETECT,gm,i,error;

char message[80];

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

error = graphresult();

if (error != grOk)

printf("Graphics error occurred");

printf("Press any key to halt:");

getch();

exit(1);

setbkcolor(BLUE);

for(i=1;i<=150;i+=10)

cleardevice();

arc (310,250,360-i, (360-i)+150,i);

//void arc (int x, int y, int startangle, int endangle, int radius);

outtextxy(getmaxx()-200,getmaxy()-100,"Center:(310,250)");

sprintf(message, "Radius : %d",i);

outtextxy(getmaxx()-200,getmaxy()-80,message);

sprintf(message, "Start angle : %d ",360-i);


outtextxy(getmaxx()-200,getmaxy()-60,message);

sprintf(message, "End angle : %d",(360-i)+150);

outtextxy(getmaxx()-200,getmaxy()-40,message);

getch();

cleardevice();

for(i=1;i<=150;i+=10)

arc (310,250,360-i, (360-i)+150,i);

getch();

closegraph();

return 0;

OutPut

You might also like