0% found this document useful (0 votes)
222 views62 pages

CG Lab Manual

The document is a lab manual for computer graphics experiments from the Department of Computer Science at Government College Women University, Faisalabad. It contains 35 questions and answers related to drawing basic shapes and performing coordinate operations in computer graphics. The questions cover topics like putting a pixel, getting coordinates, setting colors, drawing lines, circles, triangles, and more complex shapes like a smiley face and moving objects. The answers provide code snippets in C programming language to demonstrate how to perform each task using graphics functions.

Uploaded by

Black Hawk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views62 pages

CG Lab Manual

The document is a lab manual for computer graphics experiments from the Department of Computer Science at Government College Women University, Faisalabad. It contains 35 questions and answers related to drawing basic shapes and performing coordinate operations in computer graphics. The questions cover topics like putting a pixel, getting coordinates, setting colors, drawing lines, circles, triangles, and more complex shapes like a smiley face and moving objects. The answers provide code snippets in C programming language to demonstrate how to perform each task using graphics functions.

Uploaded by

Black Hawk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Lab Manual

Department Of Computer Science


Government College Women University, Faisalabad
Computer Graphics
Table of Content
Sr Name of Experiment Page No
1 Put pixel 4
2 Get X
3 Get Y
4 Get Max X
5 Get Max Y
6 Out text x,y
7 Set Font Color
8 Set Back Ground Color
9 Get Back Ground Color
10 DDA Line
11 Draw a line in computer graphics
12 Draw a circle in computer graphics
13 Font-style(Horizontal)
14 Font-style(Vertical)
15 Font-style(Multiple-horizontal)
16 Font-style(Multiple-vertical)
17 Draw a triangle in Computer graphics
18 Draw a ellipse in computer graphics
19 Draw a rectangle in computer graphics
20 Draw a filled circle in computer graphics
21 Draw a smiley face in computer graphics
22 Draw a moving cycle in computer graphics
23 Draw a moving car in computer graphics
24 Draw a hut in computer graphics
25 Draw a animation using increasing circles filled with
colors
26
28
29
30
31
32
33
34
35
Question No 1: Put a pixel on screen using computer graphics
programming.
Code:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

int main()
{
//initilizing graphic mode and
//graphic driver variable
int graphicdriver=DETECT,graphicmode;
initwindow(800,800);
putpixel(100,200,3);
getch();
}
Output:
Question No 2: Get X coordinates using computer graphics programming.
Answer:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
char arr[100];

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");

// sprintf stands for “String print”.


// Instead of printing on console, it
// store output on char buffer which
// are specified in sprintf
sprintf(arr, "Current position of x = %d",
getx());

// outtext function displays text at


// current position.
outtext(arr);

// moveto function
moveto(150, 100);
sprintf(arr, "Current position of x = %d",
getx());
// outtext function displays text at
// current position.
outtext(arr);

getch();

// closegraph function closes the


// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

return 0;
}
OutPut:
Question No 3: Get Y coordinates in computer graphics
Answer:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
char arr[100];

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");

sprintf(arr, "Current position of y = %d",


gety());
// outtext function displays text at
// current position.
outtext(arr);

// moveto function
moveto(100, 150);
sprintf(arr, "Current position of y = %d",
gety());

// outtext function displays text at


// current position.
outtext(arr);

getch();

// closegraph function closes the


// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

return 0;
}
Output:

Question No 4: Get Max X coordinates in computer graphics


Answer:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
char arr[100];

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");

sprintf(arr, "Maximum X coordinate for current "


"graphics mode And driver = %d", getmaxx());

// outtext function displays text at


// current position.
outtext(arr);

getch();

// closegraph function closes the


// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

return 0;
}
OutPut:

Question No 5: Get max Y coordinates in computer graphics


Answer:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
char arr[100];

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");

sprintf(arr, "Maximum Y coordinate for current "


"graphics mode And driver = %d", getmaxy());

// outtext function displays text at


// current position.
outtext(arr);

getch();

// closegraph function closes the


// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

return 0;
}
OutPut:

Question No 6: write a program using computer graphics


 Font style : Triplex-scr-font
 Font size: 18
 Direction: Vertical
 X and Y coordinate: 10, 10
Answer:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
initwindow(800,800);
int x=10, y=10;
int font= 7, direction= 1;
int font_size= 18;
settextstyle(font, direction, font_size);
outtextxy(x,y,"Yamna");
getch();
closegraph();
return 0;
}
Output:
Question No 7: Set font color in computer graphics
Answer:
#include<stdio.h>
#include<graphics.h>
#include<dos.h>

// function for printing


// message as colored character
void printMsg()
{
// auto detection
int gdriver = DETECT,gmode,i;

// initialize graphics mode


initwindow(800,800);

for (i=3; i<7; i++)


{
// setcolor of cursor
setcolor(i);

// set text style as


// settextstyle(font, orientation, size)
settextstyle(i,0,i);

// print text at coordinate x,y;


outtextxy(100,20*i,"MSc CS");

delay(500);
}
delay(2000);
}
// driver program
int main()
{
printMsg();
}
OutPut:

Question No 8: Set background color in computer graphics


Answer:
/* setbkcolor example */

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

#include <stdio.h>

#include <conio.h>

int main(void)

/* _select driver and mode that supports multiple background colors*/

int gdriver = EGA, gmode = EGAHI, errorcode;

int bkcol, maxcolor, x, y;

char msg[80];

/* initialize graphics and local variables */


initwindow(800,800);

/* 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 */

}
/* maximum color index supported */

maxcolor = getmaxcolor();

/* for centering text messages */

settextjustify(CENTER_TEXT, CENTER_TEXT);

x = getmaxx() / 2;

y = getmaxy() / 2;

/* loop through the available colors */

for (bkcol=0; bkcol<=maxcolor; bkcol++) {


/* clear the screen */

cleardevice();

/* select a new background color */

setbkcolor(bkcol);

/* output a messsage */

if (bkcol == WHITE)

setcolor(BLUE);

sprintf(msg, "Background color: %d", bkcol);

outtextxy(x, y, msg);

getch();
}

/* clean up */

closegraph();

return 0;

}
OutPut:
Question No 9: Get back ground color in computer
Answer:
#include <graphics.h>
#include <stdio.h>

// driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
char arr[100];

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initwindow(800,800);

// sprintf stands for “String print”.


// Instead of printing on console, it
// store output on char buffer which
// are specified in sprintf
sprintf(arr, "Current background color = %d",
getbkcolor());

// outtext function displays text


// at current position.
outtextxy(10,10, arr);

getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

return 0;
}
OutPut:

Question No 10: Draw DDA Line in computer graphics


Answer:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
float round(float a);
int main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,steps,k;
float xincr,yincr,x,y,dx,dy;
printf("enter x1,y1");
scanf("%d%d",&x1,&y1);
printf("enter x2,y2");
scanf("%d%d",&x2,&y2);
initwindow(800,800);//initializes the graph
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincr=dx/steps;
yincr=dy/steps;
x=x1;
y=y1;
for(k=1;k<=steps;k++)
{
delay(100);//for seeing the line drawing process slowly.
x+=xincr;
y+=yincr;
putpixel(round(x),round(y),WHITE);
}
outtextxy(200,20,"DDA"); // for printing text at desired screen location.
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch();
closegraph(); // closes the graph and comes back to previous graphic
mode.
}
float round(float a)
{
int b=a+0.5;
return b;
}
OutPut:
Question No 11: Draw a line in computer graphics
Answer:
#include <graphics.h>

// driver code
int main()
{
// gm is Graphics mode which is a computer display
// mode that generates image using pixels.
// DETECT is a macro defined in "graphics.h" header file
int gd = DETECT, gm;

// initgraph initializes the graphics system


// by loading a graphics driver from disk
initwindow(800,800);

// line for x1, y1, x2, y2


line(150, 200, 450, 200);

getch();

// closegraph function closes the graphics


// mode and deallocates all memory allocated
// by graphics system .
closegraph();
}
Output:

Question No 12: Draw a circle in computer graphics


Answer:
#include <graphics.h>

//driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");

// circle function
circle(350, 300, 150);

getch();

// closegraph function closes the


// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

return 0;
}
OutPut:
Question No 13: Set font-style in computer graphics
Answer:
#include <graphics.h>

// driver code
int main()
{

int gd = DETECT, gm;


initgraph(&gd, &gm, "");
// location of text
int x = 150;
int y = 150;

// font style
int font = 8;

// font direction
int direction = 0;

// font size
int font_size = 5;
// for setting text style
settextstyle(font, direction, font_size);

// for printing text in graphics window


outtextxy(x, y, "Pakistan Zaindabad");

getch();
closegraph();

return 0;
}
OutPut:

Question No 14: Set font-style(Vertical) in computer graphics


Answer:
#include <graphics.h>

// driver code
int main()
{

int gd = DETECT, gm;


initwindow(800,800);

// location of text
int x = 150;
int y = 150;

// font style
int font = 7;

// font direction
int direction = 1;

// font size
int font_size = 5;
// for setting text style
settextstyle(font, direction, font_size);

// for printing text in graphics window


outtextxy(x, y, "Pakistan Zaindabad");

getch();
closegraph();

return 0;
}
OutPut:

Question No : Font-style(vertical) in computer graphics:


Answer:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm,x=25,y=25,font=10;
initgraph(&gd,&gm,"C:\\turboC3\\BGI");
for(font=0;font<=2;font++)
{
settextstyle(font,VERT_DIR,font+2);
setcolor(font+1);
x=250;
y=100;
outtextxy(x,y,"text in vertical direction");
y=y+25;
}
getch();
closegraph();
}
Output:
Question No : Multi Font-style(Horizontal) in computer graphics
Answer:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm,x=25,y=25,font=10;
initgraph(&gd,&gm,"C:\\turboC3\\BGI");
for(font=0;font<=4;font++)
{
settextstyle(font,HORIZ_DIR,font+1);// sets font type, font direction, size
setcolor(font+1); // sets color for text.
outtextxy(x,y,"text with different fonts"); // prints message on screen at
(x,y)
y=y+25;
}
getch();
closegraph();
}
OutPut:

Question No 16: Multi Font-style(vertical) in computer graphics


Answer:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm,x=18,y=18,font=10;
initgraph(&gd,&gm,"C:\\turboC3\\BGI");
for(font=0;font<=4;font++)
{
settextstyle(font,VERT_DIR,font+1);// sets font type, font direction, size
setcolor(font+1); // sets color for text.
outtextxy(x,y,"MSc Cs"); // prints message on screen at (x,y)
y=y+25;
}
getch();
closegraph();
}
Output:
Question No 17: Draw a triangle in computer graphics
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\tc\\bgi");

line(300, 100, 200, 200);


line(300, 100, 400, 200);
line(200, 200, 400, 200);

getch();
closegraph();
}
OutPut:

Question No 17: Draw a ellipse in computer graphics


Answer:
#include <graphics.h>

int main()
{
// gm is Graphics mode which is a computer display
// mode that generates image using pixels.
// DETECT is a macro defined in "graphics.h" header file
int gd = DETECT, gm;

// location of ellipse
int x = 250, y = 200;

// here is the starting angle


// and end angle
int start_angle = 0;
int end_angle = 360;

// radius from x axis and y axis


int x_rad = 100;
int y_rad = 50;

// initgraph initializes the graphics system


// by loading a graphics driver from disk
initgraph(&gd, &gm, "");

// ellipse function
ellipse(x, y, start_angle,
end_angle, x_rad, y_rad);

getch();
closegraph();

return 0;
}
OutPut:

Question No 18: Draw a rectangle in computer graphics


Answer:
#include <graphics.h>

// Driver code
int main()
{
int gd = DETECT, gm;

// location of left, top, right, bottom


int left = 150, top = 150;
int right = 450, bottom = 450;
initgraph(&gd, &gm, "");

// rectangle function
rectangle(left, top, right, bottom);

getch();

closegraph();
}
OutPut:
Question No 19: Draw a circle with color in computer graphics
Answer:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
int main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
setfillstyle(CLOSE_DOT_FILL,GREEN);
circle(200,200,100);
floodfill(202,202,15);
getch();
closegraph();
}
OutPut:

Question No 21: draw a smiley face in computer graphics


Answer:
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>

// Driver Code
int main()
{

// Initilize graphic driver


int gr = DETECT, gm;

// Initialize graphics mode by passing


// three arguments to initgraph function

// &gdriver is the address of gdriver


// variable, &gmode is the address of
// gmode and "C:\\Turboc3\\BGI" is the
// directory path where BGI files
// are stored
initwindow(800,800);

// Set color of smiley to yellow


setcolor(YELLOW);

// creating circle and fill it with


// yellow color using floodfill.
circle(300, 100, 40);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(300, 100, YELLOW);

// Set color of background to black


setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);

// Use fill ellipse for creating eyes


fillellipse(310, 85, 2, 6);
fillellipse(290, 85, 2, 6);

// Use ellipse for creating mouth


ellipse(300, 100, 205, 335, 20, 9);
ellipse(300, 100, 205, 335, 20, 10);
ellipse(300, 100, 205, 335, 20, 11);

getch();
closegraph();
}
OutPut:
Question No 22: Draw a moving cycle in Computer graphics
Answer:
#include<graphics.h>
#include<conio.h>
#include<dos.h>
int main()
{

initwindow(800,800);

line(0,435,800,435);
for(int i=0;i<800;i=i+10)
{
setcolor(WHITE);
line(50+i,405,100+i,405);
line(75+i, 375, 125+i, 375);
line(50+i, 405, 75+i, 375);
line(100+i,405,125+i,375);
line(150+i,405,100+i,345);
line(75+i,375,75+i,370);
line(70+i,370,80+i,370);
line(80+i,345,100+i,345);

circle(150+i,405,30);
circle(50+i,405,30);

delay(100);
setcolor(BLACK);

line(50+i,405,100+i,405);
line(75+i, 375, 125+i, 375);
line(50+i, 405, 75+i, 375);
line(100+i,405,125+i,375);
line(150+i,405,100+i,345);
line(75+i,375,75+i,370);
line(70+i,370,80+i,370);
line(80+i,345,100+i,345);

circle(150+i,405,30);
circle(50+i,405,30);

}
getch();
closegraph();
}
OutPut:
Question No 23: Draw a moving car in computer graphics
Answer:
#include <graphics.h>
#include <stdio.h>

// Function to draw moving car


void draw_moving_car(void) {

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

// Passed three arguments to initgraph


// function to initialize graphics mode
initgraph(&gd, &gm, "");

for (i = 0; i <= 420; i = i + 10) {

// Set color of car as red


setcolor(RED);

// Thease lines for bonnet and


// body of car
line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);

// For left wheel of car


circle(65 + i, 330, 15);
circle(65 + i, 330, 2);
// For right wheel of car
circle(145 + i, 330, 15);
circle(145 + i, 330, 2);

// Line left of left wheel


line(0 + i, 330, 50 + i, 330);

// Line middle of both wheel


line(80 + i, 330, 130 + i, 330);

// Line right of right wheel


line(210 + i, 330, 160 + i, 330);

delay(100);

// To erase previous drawn car, draw


// the whole car at same possition
// but color using black
setcolor(BLACK);

// Lines for bonnet and body of car


line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);

// For left wheel of car


circle(65 + i, 330, 15);
circle(65 + i, 330, 2);

// For right wheel of car


circle(145 + i, 330, 15);
circle(145 + i, 330, 2);

// Line left of left wheel


line(0 + i, 330, 50 + i, 330);

// Line middle of both wheel


line(80 + i, 330, 130 + i, 330);
// Line right of right wheel
line(210 + i, 330, 160 + i, 330);
}

getch();

closegraph();
}

// Driver code
int main()
{
draw_moving_car();

return 0;
}
OutPut:
Question No 24: Draw hut in computer graphics
Answer:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(5);
rectangle(60,80,150,200);
rectangle(95,140,120,200);
line(60,80,100,15);
line(100,15,150,80);
circle(100,60,10);
getch();
closegraph();
}
OutPut:

You might also like