Computer Graphics Lab
By Priya Goyal
How to run the graphics program in Turbo C++
• Open turbo C++
• Click on Option => Linker => Library
• Click on Graphics(mark it as ‘X’)
• Click on OK
Now you can run your graphics programs.
Basic functions to draw shapes in graphics
Shape Function syntax
pixel Draw a pixel Putpixel (X1,Y1,Color)
Line Draw a line Line(X1,Y1,X2,Y2)
Rectangle Draw a rectangle Rectangle(Left,Top,Right,Bottom)
Circle Draw a circle Circle(X,Y,Radius)
Ellipse Draw a ellipse Ellipse(X1,Y1,0,360,XaxisRadius,YaxisRadius)
Arc Draw a arc Arc(X1,Y1,StartingAngle,EndingAngle,Radius)
Bar Draw a filled rectangle Bar(Left,Top,Right,Bottom)
Bar3d Draw a filled 3d rectangle Bar3d(Left,Top,Right,Bottom,depth,topflag)
**topflag will be 0 or 1**
Here X1=distance of first point of shape from X axis,
X2=Distance of last point of shape from X axis
Here Y1=distance of first point of shape from Y axis,
Y2=Distance of last point of shape from Y axis
Structure of graphics program
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
……………………
…………………….
getch( );
closegraph( );
}
Location of BGI folder, where
display files are saved. Write the
location of (C:-TC-BGI)
Initialization of
graph
Draw a Pixel
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
Putpixel(100,100,RED)
getch( );
closegraph( );
}
Putpixel(X1,Y1,Color)
Draw a line
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
Line(100,100,200,200)
getch( );
closegraph( );
}
Line(X1,Y1,X2,Y2)
Draw a Rectangle
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
Rectangle(100,100,200,200)
getch( );
closegraph( );
}
rectangle(X1,Y1,X2,Y2)
Draw a circle
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
Circle(100,100,10)
getch( );
closegraph( );
}
circle(X1,Y1,radius)
Draw an ellipse
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
ellipse(100,100,0,360,20,10);
getch( );
closegraph( );
}
ellipse(x1,y1,0,360,xAxisRadius,yAxisRadius)
Draw an Arc
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
arc(100,100,90,280,10);
getch( );
closegraph( );
}
arc(x1,y1,StartingAngle,EndingAngle,Radius)
Draw a Bar
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
bar(100,100,200,200);
getch( );
closegraph( );
}
Bar(Left,Top,Right,Bottom)
Draw a 3D bar
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main( )
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
bar3d(100,100,200,200,10,1);
getch( );
closegraph( );
}
Bar3d(left,top,right,bottom,depth,topflag)
Assignment
Q.1 Draw a flag using line and circle.
Q.2 Draw a hut using line, rectangle and circle.
Q.3 Draw a smiley face using ellipse, circle and arc.
Q.4 Draw a fish using arc, circle and line.

Basics of Computer graphics lab

  • 1.
  • 2.
    How to runthe graphics program in Turbo C++ • Open turbo C++ • Click on Option => Linker => Library • Click on Graphics(mark it as ‘X’) • Click on OK Now you can run your graphics programs.
  • 3.
    Basic functions todraw shapes in graphics Shape Function syntax pixel Draw a pixel Putpixel (X1,Y1,Color) Line Draw a line Line(X1,Y1,X2,Y2) Rectangle Draw a rectangle Rectangle(Left,Top,Right,Bottom) Circle Draw a circle Circle(X,Y,Radius) Ellipse Draw a ellipse Ellipse(X1,Y1,0,360,XaxisRadius,YaxisRadius) Arc Draw a arc Arc(X1,Y1,StartingAngle,EndingAngle,Radius) Bar Draw a filled rectangle Bar(Left,Top,Right,Bottom) Bar3d Draw a filled 3d rectangle Bar3d(Left,Top,Right,Bottom,depth,topflag) **topflag will be 0 or 1** Here X1=distance of first point of shape from X axis, X2=Distance of last point of shape from X axis Here Y1=distance of first point of shape from Y axis, Y2=Distance of last point of shape from Y axis
  • 4.
    Structure of graphicsprogram #include<graphics.h> #include<stdio.h> #include<conio.h> void main( ) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); …………………… ……………………. getch( ); closegraph( ); } Location of BGI folder, where display files are saved. Write the location of (C:-TC-BGI) Initialization of graph
  • 5.
    Draw a Pixel #include<graphics.h> #include<stdio.h> #include<conio.h> voidmain( ) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); Putpixel(100,100,RED) getch( ); closegraph( ); } Putpixel(X1,Y1,Color)
  • 6.
    Draw a line #include<graphics.h> #include<stdio.h> #include<conio.h> voidmain( ) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); Line(100,100,200,200) getch( ); closegraph( ); } Line(X1,Y1,X2,Y2)
  • 7.
    Draw a Rectangle #include<graphics.h> #include<stdio.h> #include<conio.h> voidmain( ) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); Rectangle(100,100,200,200) getch( ); closegraph( ); } rectangle(X1,Y1,X2,Y2)
  • 8.
    Draw a circle #include<graphics.h> #include<stdio.h> #include<conio.h> voidmain( ) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); Circle(100,100,10) getch( ); closegraph( ); } circle(X1,Y1,radius)
  • 9.
    Draw an ellipse #include<graphics.h> #include<stdio.h> #include<conio.h> voidmain( ) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); ellipse(100,100,0,360,20,10); getch( ); closegraph( ); } ellipse(x1,y1,0,360,xAxisRadius,yAxisRadius)
  • 10.
    Draw an Arc #include<graphics.h> #include<stdio.h> #include<conio.h> voidmain( ) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); arc(100,100,90,280,10); getch( ); closegraph( ); } arc(x1,y1,StartingAngle,EndingAngle,Radius)
  • 11.
    Draw a Bar #include<graphics.h> #include<stdio.h> #include<conio.h> voidmain( ) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); bar(100,100,200,200); getch( ); closegraph( ); } Bar(Left,Top,Right,Bottom)
  • 12.
    Draw a 3Dbar #include<graphics.h> #include<stdio.h> #include<conio.h> void main( ) { int gd = DETECT, gm; initgraph(&gd, &gm, "C:TCBGI"); bar3d(100,100,200,200,10,1); getch( ); closegraph( ); } Bar3d(left,top,right,bottom,depth,topflag)
  • 13.
    Assignment Q.1 Draw aflag using line and circle. Q.2 Draw a hut using line, rectangle and circle. Q.3 Draw a smiley face using ellipse, circle and arc. Q.4 Draw a fish using arc, circle and line.