Computer Graphics Figures

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Worksheet - 1.

4
Aim: Write a program to draw a hut, star and car using graphics.h

Software Requirement: Code Blocks (graphics.h installed), Operating System:


Microsoft Windows

Hardware Requirement: Min Processor Type: Intel 386 or higher, Minimum RAM
Size: 4 MB, Min Hard-Drive Space: 25 MB.

Description:

The C++ graphics using graphics.h functions can be used to draw different shapes,
display text in different fonts, change colours and many more. Using functions of
graphics.h in Turbo C++ compiler or Code Blocks you can make graphics programs,
animations, projects, and games. You can draw circles, lines, rectangles, bars and
many other geometrical figures. You can change their colours using the available
functions and fill them.
In this experiment, we will draw various figures like hut, star and car using various
functions of graphics.h. These figures are made using a collection lines, circles, ars,
rectangles etc. which are combined together to form an image. We have used
commands such as line(), putpixel(), circle(), arc(), drawpoly() to draw the above
mentioned figures. Further, these images are styled by changing the colour, size,
background-colour by using certain commands like setcolor(), setfillstyle(),
settextstyle(), floodfill() and other similar commands.

Codes and Outputs:


STAR:
The star is drawn using two methods:
Method 1:
#include<graphics.h>
#include<iostream>
using namespace std;
int main()

Page | 1
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "");
int arr1[]={300,50,500,350,
100,350,300,50};
drawpoly(4,arr1);
int arr2[]={100,150,500,150,
300,450,100,150};
drawpoly(4,arr2);
settextstyle(10,0,4);
outtextxy(260,240,"Star");
getch();
closegraph();
return 0;
}
Output:

Page | 2
Method 2:
#include<graphics.h>
int main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "");
setlinestyle(0,0,4);
int arr[]={300,50,375,150,500,150,425,250,500,350,375,350,300,450,
225,350,100,350,175,250,100,150,225,150,300,50};
drawpoly(13,arr);
settextstyle(10,0,4);
outtextxy(260,240,"Star");
getch();
closegraph();
return 0;
}
Output:

Page | 3
CAR:
#include<graphics.h>
#include<iostream>
using namespace std;
int main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "");

setfillstyle(10,15);
circle(200,300,60);
circle(200,300,35);
circle(450,300,60);
circle(450,300,35);
floodfill(200,300,15);
floodfill(450,300,15);

line(50,300,140,300);
line(260,300,390,300);
line(510,300,540,300);

line(150,200,500,200);
line(340,200,340,50);
line(500,200,540,300);

line(58,260,100,260);
line(52,280,90,280);
line(100,260,90,280);

arc(150,300,90,180,100);
arc(340,200,0,180,150);

settextstyle(10,0,4);

Page | 4
outtextxy(300,240,"CAR");
getch();
closegraph();
return 0;
}

Output:

Page | 5
HUT:
#include<graphics.h>
#include<iostream>
using namespace std;
int main()
{
int gd = DETECT,gm;
initgraph(&gd, &gm, "");
line(200,50,75,150);
line(75,150,100,150);
line(200,50,325,150);
line(200,50,500,50);
line(500,50,625,150);
line(625,150,600,150);

setfillstyle(1,15);
circle(235,275,5);
floodfill(235,275,15);

rectangle(100,150,300,350);
rectangle(300,150,600,350);
rectangle(375,200,525,300);
rectangle(150,200,250,350);

setfillstyle(9,15);
rectangle(380,205,445,245);
floodfill(400,230,15);
rectangle(455,205,520,245);
floodfill(460,230,15);
rectangle(380,255,445,295);
floodfill(400,270,15);
rectangle(455,255,520,295);
floodfill(460,270,15);

Page | 6
settextstyle(10,0,4);
outtextxy(300,400,"HUT");

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

Page | 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy