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

Computer Graphics

The document contains programs using graphics.h library functions to: 1. Simulate functions like initGraphics, arc, circle, line, rectangle, ellipse, outtext, cleardevice, closegraph. 2. Draw a moving car using functions like line, circle, delay. 3. Design a smiley face using circle, fillEllipse, ellipse. 4. Create circles inside other circles using circle. 5. The programs initialize graphics mode, set colors, draw shapes, add text, and close graphics.

Uploaded by

meenu giby
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Computer Graphics

The document contains programs using graphics.h library functions to: 1. Simulate functions like initGraphics, arc, circle, line, rectangle, ellipse, outtext, cleardevice, closegraph. 2. Draw a moving car using functions like line, circle, delay. 3. Design a smiley face using circle, fillEllipse, ellipse. 4. Create circles inside other circles using circle. 5. The programs initialize graphics mode, set colors, draw shapes, add text, and close graphics.

Uploaded by

meenu giby
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CG –ASS2

MEENU MARIA GIBY


U18CO106
1. Explore different functions of graphics.h library.

2. Write a program for the simulation of following functions:

initGraphics, arc, bar, circle, line, rectangle, ellipse:

outtext, outtextxy, cleardevice, closegraph:

#include <graphics.h>

#include <conio.h>

main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

outtext("Press any key to clear the screen.");

getch();

cleardevice();
outtextxy(100,100,"Press any key to exit...");

getch();

closegraph();

return 0;

Drawpoly

#include <graphics.h>

#include <conio.h>

main()

int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};

initgraph(&gd, &gm, "C:\\TC\\BGI");

drawpoly(4, points);

getch();

closegraph();

return 0;

}
Fillelipse

#include <graphics.h>

#include <conio.h>

int main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

fillellipse(100, 100, 50, 25);

getch();

closegraph();

return 0;

fillpoly

#include <graphics.h>

#include <conio.h>

main()

int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};

initgraph(&gd, &gm, "C:\\TC\\BGI");

fillpoly(4, points);

getch();

closegraph();
return 0;

fillArc

Fills a wedge-shaped area of an elliptical arc. The parameters are interpreted in the same way as those
for drawArc.

Usage:

fillArc(bounds, start, sweep);


fillArc(x, y, width, height, start, sweep);

fillRect

Fills the frame of a rectangle with the specified bounds.

Usage:

fillRect(bounds);
fillRect(x, y, width, height);

setFont:

Sets a new font. The font parameter is a string in the form family-style-size. In this string, family is the name
of the font family; style is either missing (indicating a plain font) or one of the strings Bold, Italic,
or BoldItalic; and size is an integer indicating the point size. If any of these components is specified as an
asterisk, the existing value is retained. The font parameter can also be a sequence of such specifications
separated by semicolons, in which the first available font on the system is used.

Usage: setFont(font);

getFont:

Returns the current font.

Usage: string font = getFont();


Getarccoords:

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

main()

int gd = DETECT, gm;

struct arccoordstype a;

char arr[100];

initgraph(&gd, &gm,"C:\\TC\\BGI");

arc(250,200,0,90,100);

getarccoords(&a);

sprintf(arr,"(%d, %d)",a.xstart,a.ystart);

outtextxy(360,195,arr);

sprintf(arr,"(%d, %d)",a.xend,a.yend);

outtextxy(245,85,arr);

getch();

closegraph();

return 0;

getbkcolor

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

#include<stdio.h>

int main()

int gd = DETECT, gm, bkcolor;

char a[100];

initgraph(&gd,&gm,"C:\\TC\\BGI");

bkcolor = getbkcolor();

sprintf(a, "Current background color = %d", bkcolor);

outtextxy(10, 10, a);

getch();

closegraph();

return 0;

getColor:

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

main()

int gd = DETECT, gm, drawing_color;

char a[100];

initgraph(&gd,&gm,"C:\\TC\\BGI");

drawing_color = getcolor();

sprintf(a,"Current drawing color = %d", drawing_color);


outtextxy( 10, 10, a );

getch();

closegraph();

return 0;

setColor:

#include<graphics.h>

#include<conio.h>

main()

int gd = DETECT, gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");

circle(100,100,50); /* drawn in white color */

setcolor(RED);

circle(200,200,50); /* drawn in red color */

getch();

closegraph();

return 0;

}
Pause

Pauses for the indicated number of milliseconds. This function is useful for animation where the motion would
otherwise be too fast.

Usage:

pause(milliseconds);

waitForClick

Waits for a mouse click to occur anywhere in the window.

Usage:

waitForClick();

settextstyle

#include <graphics.h>

#include <conio.h>

main()

int gd = DETECT, gm, x = 25, y = 25, font = 0;

initgraph(&gd,&gm,"C:\\TC\\BGI");

for (font = 0; font <= 10; font++)

settextstyle(font, HORIZ_DIR, 1);

outtextxy(x, y, "Text with different fonts");

y = y + 25;
}

getch();

closegraph();

return 0;

Setlinestyle

#include <graphics.h>

int main()

int gd = DETECT, gm, c , x = 100, y = 50;

initgraph(&gd, &gm, "C:\\TC\\BGI");

for (c = 0; c < 5; c++)

setlinestyle(c, 0, 2);

line(x, y, x+200, y);

y = y + 25;

getch();

closegraph();
return 0;

Setfillstyle

#include<graphics.h>

#include<conio.h>

main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

setfillstyle(XHATCH_FILL, RED);

circle(100, 100, 50);

floodfill(100, 100, WHITE);

getch();

closegraph();

return 0;

}
Pieslice

#include <graphics.h>

#include <conio.h>

main()

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

pieslice(200, 200, 0, 135, 100);

getch();

closegraph();

return 0;

3. Write a program to design a car using pre-defined functions of graphics.h.

#include <graphics.h>

#include <stdio.h>
// C program to draw a moving car. This

// program run in gcc compiler having

// graphics.h library installed

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

}
4. Write a program to design a smiley face using graphics.h functions.

#include <conio.h>

#include <dos.h>

#include <graphics.h>

#include <stdio.h>

// Driver Code

int main()

// Initilize graphic driver

int gr = DETECT, gm;

initgraph(&gr, &gm, "C:\\Turboc3\\BGI");

// 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 function closes the

// graphics mode and deallocates

// all memory allocated by

// graphics system

closegraph();

return 0;

}
5. Write a program to create circles inside various circles using graphics.h functions.

#include <graphics.h>

#include <conio.h>

int main()

//initilizing driver and mode

//variable of graphics

int graphicdriver=DETECT,graphicmode;

//calling initgraph function

initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");

//Printing message for user

outtextxy(10, 10 + 10, "Program to draw a circle inside various circles in C graphics");

//creating circle inside circle

setcolor(RED);

circle(200,200,100);
setcolor(BLUE);

circle(200,200,80);

setcolor(YELLOW);

circle(200,200,60);

setcolor(BROWN);

circle(200,200,40);

getch();

return 0;

6. Write a program to design traffic signal using graphics.h functions.

#include <graphics.h>

#include <conio.h>

int main()

//initilizing graphic driver and

//graphic mode variable


int graphicdriver=DETECT,graphicmode;

//calling initgraph with parameters

initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");

//Printing message for user

outtextxy(50, 50 + 50, "Program to create traffic signal in C graphics");

//initilizing variables

int middlex, middley;

//getting middle x and y

middlex = getmaxx()/2;

middley = getmaxy()/2;

//setting color as white for the outline

setcolor(WHITE);

settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);

rectangle(middlex-30,middley-80,middlex+30,middley+80);

circle(middlex, middley-50, 22);

//filling red color to signify stop sign

setfillstyle(SOLID_FILL,RED);

floodfill(middlex, middley-50,WHITE);

setcolor(WHITE);

outtextxy(middlex-15,middley-50,"STOP");

//setting color as white for the outline


setcolor(WHITE);

rectangle(middlex-30,middley-80,middlex+30,middley+80);

circle(middlex, middley, 20);

//filling yellow color to signify ready sign

setfillstyle(SOLID_FILL,YELLOW);

floodfill(middlex, middley,WHITE);

setcolor(WHITE);

outtextxy(middlex-18,middley-3,"READY");

//setting white color for outline

setcolor(WHITE);

rectangle(middlex-30,middley-80,middlex+30,middley+80);

circle(middlex, middley+50, 22);

//filling green color to signify go sign

setfillstyle(SOLID_FILL,GREEN);

floodfill(middlex, middley+50,WHITE);

setcolor(WHITE);

outtextxy(middlex-7,middley+48,"GO");

setcolor(RED);

settextstyle(SCRIPT_FONT, HORIZ_DIR, 4);

getch();

return 0;

You might also like