Computer Graphics Lab Practicals (E-Next - In)
Computer Graphics Lab Practicals (E-Next - In)
Declaration :- void arc(int x, int y, int stangle, int endangle, int radius);
arc function is used to draw an arc with center (x,y) and stangle specifies starting angle, endangle
specifies the end angle and last parameter specifies the radius of the arc. arc function can also be used
to draw a circle but for that starting angle and end angle should be 0 and 360 respectively.
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
Bar function in c
Declaration: - void bar(int left, int top, int right, int bottom);
Bar function is used to draw a 2-dimensional, rectangular filled in bar . Coordinates of left top and right
bottom corner are required to draw the bar. Left specifies the X-coordinate of top left corner, top
specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner,
bottom specifies the Y-coordinate of right bottom corner. Current fill pattern and fill color is used to fill
the bar. To change fill pattern and fill color use setfillstyle.
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
Draw 3d Bar
Declaration: - void bar3d (int left, int top, int right, int bottom, int depth, int topflag);
Bar3d function is used to draw a 2-dimensional, rectangular filled in bar . Coordinates of left top and right
bottom corner of bar are required to draw the bar. left specifies the X-coordinate of top left corner, top
specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom
specifies the Y-coordinate of right bottom corner, depth specifies the depth of bar in pixels, topflag determines
whether a 3 dimensional top is put on the bar or not ( if it is non-zero then it is put otherwise not ). Current fill
pattern and fill color is used to fill the bar. To change fill pattern and fill color use setfillstyle
include <graphics.h.
include <conio.h>
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the
circle. The code given below draws a circle.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
Ellipse function in c
getch();
closegraph();
return 0;
}
getcolor function
getcolor function returns the current drawing color.
main()
{
int gd = DETECT, gm, drawing_color;
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
drawing_color = getcolor();
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
getbkcolor function in c
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm, bkcolor;
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
bkcolor = getbkcolor();
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
getmaxcolor function
getmaxcolor function returns maximum color value for current graphics mode and driver. Total number
of colors available for current graphics mode and driver are ( getmaxcolor() + 1 ) as color numbering
starts from zero.
#include <graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, max_colors;
char a[100];
initgraph(&gd,&gm,"C:\\TC\\BGI");
max_colors = getmaxcolor();
getch();
closegraph();
return 0;
https://E-next.in
Computer Graphics Lab Practicals
#include <graphics.h>
#include<conio.h.>
#include<dos.h>
main()
{
int gd = DETECT, gm, x, y, color, angle = 0;
struct arccoordstype a, b;
delay(2000);
while(angle<=360)
{
setcolor(BLACK);
arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
setcolor(RED);
getarccoords(&a);
circle(a.xstart,a.ystart,25);
setcolor(BLACK);
arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
getarccoords(&a);
setcolor(GREEN);
circle(a.xstart,a.ystart,25);
angle = angle+5;
delay(50);
}
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
C graphics examples
#include <graphics.h>
int main()
{
int gd = DETECT, gm;
int x = 320, y = 240, radius;
getch();
closegraph();
return 0;
}
#include <graphics.h>
#include <dos.h>
int main()
{
int i, j = 0, gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(25,240,"Press any key to view the moving car");
getch();
if( i == 420 )
break;
if ( j == 15 )
https://E-next.in
Computer Graphics Lab Practicals
j = 2;
getch();
closegraph();
return 0;
}
3.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, height;
char array[100];
sprintf(array,"Textheight = %d",height);
outtext(array);
getch();
closegraph();
return 0;
}
4.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, width;
char array[100];
sprintf(array,"Textwidth = %d",width);
https://E-next.in
Computer Graphics Lab Practicals
outtext(array);
getch();
closegraph();
return 0;
}
setviewport function in c
Declaration :- void setviewport(int left, int top, int right, int bottom, int clip);
setviewport function is used to restrict drawing to a particular portion on the screen. For example
setviewport(100 , 100, 200, 200, 1);
will restrict our drawing activity inside the rectangle(100,100, 200, 200).
left, top, right, bottom are the coordinates of main diagonal of rectangle in which we wish to restrict
our drawing. Also note that the point (left, top) becomes the new origin.
5.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm, midx, midy;
midx = getmaxx()/2;
midy = getmaxy()/2;
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
6.
Settextstyle function is used to change the way in which text appears, using it we can modify the size of
text, change direction of text and change the font of text.
Different fonts
enum font_names
{
DEFAULT_FONT,
TRIPLEX_FONT,
SMALL_FONT,
SANS_SERIF_FONT,
GOTHIC_FONT,
SCRIPT_FONT,
SIMPLEX_FONT,
TRIPLEX_SCR_FONT,
COMPLEX_FONT,
EUROPEAN_FONT,
BOLD_FONT
};
7.
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm, x = 25, y = 25, font = 0;
initgraph(&gd,&gm,"C:\\TC\\BGI");
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
setlinestyle in c
Declaration:
void setlinestyle( int linestyle, unsigned upattern, int thickness );
enum line_styles
{
SOLID_LINE,
DOTTED_LINE,
CENTER_LINE,
DASHED_LINE,
USERBIT_LINE
};
8.
#include <graphics.h>
main()
{
int gd = DETECT, gm, c , x = 100, y = 50;
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
setfillstyle function in c
setfillstyle function sets the current fill pattern and fill color.
enum fill_styles
{
EMPTY_FILL,
SOLID_FILL,
LINE_FILL,
LTSLASH_FILL,
SLASH_FILL,
BKSLASH_FILL,
LTBKSLASH_FILL,
HATCH_FILL,
XHATCH_FILL,
INTERLEAVE_FILL,
WIDE_DOT_FILL,
CLOSE_DOT_FILL,
USER_FILL
};
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
setbkcolor function in c
setbkcolor function changes current background color e.g. setbkcolor(YELLLOW) changes the current
background color to YELLOW.
Remember that default drawing color is WHITE and background color is BLACK.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(GREEN);
getch();
closegraph();
return 0;
}
setcolor function in c
In Turbo Graphics each color is assigned a number. Total 16 colors are available. Strictly speaking
number of available colors depends on current graphics mode and driver.For Example :- BLACK is
assigned 0, RED is assigned 4 etc. setcolor function is used to change the current drawing color.e.g.
setcolor(RED) or setcolor(4) changes the current drawing color to RED. Remember that default
drawing color is WHITE.
#include<graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
https://E-next.in
Computer Graphics Lab Practicals
getch();
closegraph();
return 0;
}
moverel function in c
#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm, x, y;
char message[100];
moveto(100, 100);
moverel(100, -100);
x = getx();
y = gety();
getch();
closegraph();
return 0;
}
https://E-next.in
Computer Graphics Lab Practicals
DDA ALGORITHM
DDA Line ( X1, Y1, XN, YN):
Description: Here X1 and Y1 denote the starting x – coordinate and y – coordinate of the line
2. Repeat For I = X1 to XN
3. If (M <= 1) Then
4. Set DX = 1
5. Set DY = M * DX
6. Else
7. Set DY = 1
8. Set DX = DY / M
[End of If]
9. Set X1 = X1 + DX
10. Set Y1 = Y1 + DY
https://E-next.in
Computer Graphics Lab Practicals
While algorithms such as Wu's algorithm are also frequently used in modern computer graphics
because they can support antialiasing, the speed and simplicity of Bresenham's line algorithm mean
that it is still important. The algorithm is used in hardware such as plotters and in the graphics chips of
modern graphics cards. It can also be found in many software graphics libraries. Because the
algorithm is very simple, it is often implemented in either the firmware or the hardware of modern
graphics cards.
The label "Bresenham" is used today for a whole family of algorithms extending or modifying
Bresenham's original algorithm. See further references below.
Contents
1 The algorithm
2 Generalization
3 Optimization
4 Simplification
5 History
6 Similar algorithms
7 See also
8 References
9 Further reading
10 External links
The algorithm
The common conventions that pixel coordinates increase in the down and right directions (e.g. that
the pixel at (1,1) is directly above the pixel at (1,2)) and that the pixel centers that have integer
coordinates will be used. The endpoints of the line are the pixels at (x0, y0) and (x1, y1), where the first
coordinate of the pair is the column and the second is the row.
The algorithm will be initially presented only for the octant in which the segment goes down and to
the right (x0≤x1 and y0≤y1), and its horizontal projection x1 − x0 is longer than the vertical
projection y1 − y0 (the line has aslope whose absolute value is less than 1 and greater than 0.) In this
octant, for each column x between x0 and x1, there is exactly one row y (computed by the algorithm)
containing a pixel of the line, while each row between y0and y1 may contain multiple rasterized pixels.
https://E-next.in