The document discusses various graphics concepts used in C programming including:
1. Color constants, fill patterns, graphics drivers, errors, and modes that are used in graphics functions.
2. The functions closegraph, detectgraph, getbkcolor, getcolor, getmaxx, getmaxy, getpixel, getx, gety, grapherrormsg, graphresult, initgraph, outtext, outtexttxy, putpixel, setbkcolor, setcolor, and setfillpattern and their purposes.
3. initgraph initializes the graphics system by loading a driver and putting the system into graphics mode, while closegraph deallocates memory and restores the original screen mode.
1. Page | 1
1. Study and understanding the meaning and use of following graphics contents, data
types and global variables.
A ) COLOR C ) graphics_driver E) graphics_mode
B ) fill_pattern D ) graphics_errors
A)COLORS
COLORS, CGA_COLORS, and EGA_COLORS (Enumerated Constants for Colors)
These tables show the symbolic constants used to set text attributes on CGA and EGA
monitors. (Defined in CONIO.H.) the drawing colors available for BGI functions running on
CGA and EGA monitors. (Defined in GRAPHICS.H.)The COLORS constants are used by
these text mode functions: textattr, textbackground , textcolor
The CGA_COLORS and EGA_COLORS constants are used by these BGI graphics functions:
setallpalette setbkcolor
setcolor setpalette
Valid colors depend on the current graphics driver and current graphics mode.
COLORS (text mode)
Back- Fore-
Constant Value grnd? grnd?
BLACK 0 Yes Yes
BLUE 1 Yes Yes
GREEN 2 Yes Yes
CYAN 3 Yes Yes
RED 4 Yes Yes
MAGENTA 5 Yes Yes
BROWN 6 Yes Yes
LIGHTGRAY 7 Yes Yes
DARKGRAY 8 No Yes
LIGHTBLUE 9 No Yes
LIGHTGREEN 10 No Yes
LIGHTCYAN 11 No Yes
LIGHTRED 12 No Yes
LIGHTMAGENTA 13 No Yes
YELLOW 14 No Yes
WHITE 15 No Yes
..............................
BLINK 128 No *** To display blinking characters in text mode, add BLINK to the
foreground color.(Defined in CONIO.H.) CGA_COLORS (graphics mode)In this table, the
palette listings CGA0,CGA1, CGA2, and CGA3 refer to the fourpredefined four-color palettes
available on CGA (and compatible) systems.
Palette Constant assigned to this color number (pixel value)
Number 1 2 3
CGA0 CGA_LIGHTGREEN CGA_LIGHTRED CGA_YELLOW
CGA1 CGA_LIGHTCYAN CGA_LIGHTMAGENTA CGA_WHITE
CGA2 CGA_GREEN CGA_RED CGA_BROWN
CGA3 CGA_CYAN CGA_MAGENTA CGA_LIGHTGRAY
2. Page | 2
You can select the background color (entry 0)in each of these palettes, but the other colors are
fixed.
EGA_ COLORS (graphics mode)
Constant Value Constant Value
EGA_BLACK 0 EGA_DARKGRAY 56
EGA_BLUE 1 EGA_LIGHTBLUE 57
EGA_GREEN 2 EGA_LIGHTGREEN 58
EGA_CYAN 3 EGA_LIGHTCYAN 59
EGA_RED 4 EGA_LIGHTRED 60
EGA_MAGENTA 5 EGA_LIGHTMAGENTA 61
EGA_LIGHTGRAY 7 EGA_YELLOW 62
EGA_BROWN 20 EGA_WHITE 63
B ) fill_pattern
Enum: Fill patterns for getfillsettings and setfillstyle.
Names Value Means Fill With...
EMPTY_FILL 0 Background color
SOLID_FILL 1 Solid fill
LINE_FILL 2 ---
LTSLASH_FILL 3 ///
SLASH_FILL 4 ///, thick lines
BKSLASH_FILL 5 , thick lines
LTBKSLASH_FILL 6
HATCH_FILL 7 Light hatch
XHATCH_FILL 8 Heavy crosshatch
INTERLEAVE_FILL 9 Interleaving lines
WIDE_DOT_FILL 10 Widely spaced dots
CLOSE_DOT_FILL 11 Closely spaced dots
USER_FILL 12 User-defined fill pattern
All but EMPTY_FILL fill with the current fill color. EMPTY_FILL uses the current
background color.
C ) graphics_driver
Enum: BGI graphics drivers
Constant Value
DETECT 0 (requests autodetection)
CGA 1
MCGA 2
EGA 3
EGA64 4
EGAMONO 5
IBM8514 6
HERCMONO 7
ATT400 8
3. Page | 3
VGA 9
PC3270 10
D ) graphics_errors
graphics_errors <GRAPHICS.H>
Enum: Error return code from graphresult
Error graphics_errors
code constant Corresponding error message string
0 grOk No error
-1 grNoInitGraph (BGI) graphics not installed (use initgraph)
-2 grNotDetected Graphics hardware not detected
-3 grFileNotFound Device driver file not found
-4 grInvalidDriver Invalid device driver file
-5 grNoLoadMem Not enough memory to load driver
-6 grNoScanMem Out of memory in scan fill
-7 grNoFloodMem Out of memory in flood fill
-8 grFontNotFound Font file not found
-9 grNoFontMem Not enough memory to load font
-10 grInvalidMode Invalid graphics mode for selected driver
-11 grError Graphics error
-12 grIOerror Graphics I/O error
-13 grInvalidFont Invalid font file
-14 grInvalidFontNum Invalid font number
-15 grInvalidDeviceNum Invalid device number
-18 grInvalidVersion Invalid version number
E) graphics_modes
Enum: Graphics modes for each BGI driver
Graphics driver graphics_modes Value Column x Row Palette Pages
CGA CGAC0 0 320 x 200 C0 1
CGAC1 1 320 x 200 C1 1
CGAC2 2 320 x 200 C2 1
CGAC3 3 320 x 200 C3 1
CGAHI 4 640 x 200 2 color 1
MCGA MCGAC0 0 320 x 200 C0 1
MCGAC1 1 320 x 200 C1 1
MCGAC2 2 320 x 200 C2 1
MCGAC3 3 320 x 200 C3 1
MCGAMED 4 640 x 200 2 color 1
MCGAHI 5 640 x 480 2 color 1
EGA EGALO 0 640 x 200 16 color 4
EGAHI 1 640 x 350 16 color 2
EGA64 EGA64LO 0 640 x 200 16 color 1
4. Page | 4
EGA64HI 1 640 x 350 4 color 1
EGA-MONO EGAMONOHI 3 640 x 350 2 color 1*
EGAMONOHI 3 640 x 350 2 color 2**
HERC HERCMONOHI 0 720 x 348 2 color 2
ATT400 ATT400C0 0 320 x 200 C0 1
ATT400C1 1 320 x 200 C1 1
ATT400C2 2 320 x 200 C2 1
ATT400C3 3 320 x 200 C3 1
ATT400MED 4 640 x 200 2 color 1
ATT400HI 5 640 x 400 2 color 1
VGA VGALO 0 640 x 200 16 color 2
VGAMED 1 640 x 350 16 color 2
PC3270 PC3270HI 0 720 x 350 2 color 1
IBM8514 IBM8514HI 1 1024 x 760 256 color
IBM8514LO 0 640 x 480 256 color
* 64K on EGAMONO card ** 256K on EGAMONO card
5. Page | 5
2. Study and understand the meaning and use of following graphics function.
a) Closegraph b) detectgraph c) getbkcolor d)getcolor e)getmaxx f)getmaxy g)
getpixel h) getx i) gety j) grapherrormsg k) graphresult l) initgraph m)outtext
n)outtexttxy o)putpixel p) setbkcolor q ) setcolor r) setfillpattern
a) Closegraph
Syntax:
#include <graphics.h>
void closegraph(void);
Description:
closegraph deal locates all memory allocated by the graphics system, then restores the screen to
the mode it was in before you called initgraph. (The graphics system deal locates memory, such
as the drivers, fonts, and an internal buffer, through a call to_graphfreemem.)
Return Value
None.
b) detectgraph
Syntax:
#include <graphics.h>
void detectgraph(int *graphdriver, int *graphmode);
Description:
detectgraph detects your system's graphics adapter and chooses the mode that provides the
highest resolution for that adapter. If no graphics hardware is detected, *graphdriver is set to
grNotDetected (-2), and graphresult returns grNotDetected (-2).
*graphdriver is an integer that specifies the graphics driver to be used. You can give it a value
using a constant of the graphics_drivers enumeration type defined in graphics.h and listed as
follows:
graphics_drivers constant Numeric value
DETECT 0 (requests autodetect)
CGA 1
MCGA 2
EGA 3
EGA64 4
EGAMONO 5
IBM8514 6
HERCMONO 7
ATT400 8
VGA 9
PC3270 10
*graphmode is an integer that specifies the initial graphics mode (unless *graphdriver equals
DETECT; in which case, *graphmode is set to the highest resolution available for the detected
driver). You can give *graphmode a value using a constant of the graphics_modes enumeration
type defined in graphics.h and listed as follows.
Graphics
Columns
Driver graphics_mode Value x Rows Palette Pages
6. Page | 6
CGA CGAC0 0 320 x 200 C0 1
CGAC1 1 320 x 200 C1 1
CGAC2 2 320 x 200 C2 1
CGAC3 3 320 x 200 C3 1
CGAHI 4 640 x 200 2 color 1
MCGA MCGAC0 0 320 x 200 C0 1
MCGAC1 1 320 x 200 C1 1
MCGAC2 2 320 x 200 C2 1
MCGAC3 3 320 x 200 C3 1
MCGAMED 4 640 x 200 2 color 1
MCGAHI 5 640 x 480 2 color 1
EGA EGALO 0 640 x 200 16 color 4
EGAHI 1 640 x 350 16 color 2
EGA64 EGA64LO 0 640 x 200 16 color 1
EGA64HI 1 640 x 350 4 color 1
EGA-MONO EGAMONOHI 3 640 x 350 2 color 1 w/64K
EGAMONOHI 3 640 x 350 2 color 2 w/256K
HERC HERCMONOHI 0 720 x 348 2 color 2
ATT400 ATT400C0 0 320 x 200 C0 1
ATT400C1 1 320 x 200 C1 1
ATT400C2 2 320 x 200 C2 1
ATT400C3 3 320 x 200 C3 1
ATT400MED 4 640 x 200 2 color 1
ATT400HI 5 640 x 400 2 color 1
VGA VGALO 0 640 x 200 16 color 2
VGAMED 1 640 x 350 16 color 2
VGAHI 2 640 x 480 16 color 1
PC3270 PC3270HI 0 720 x 350 2 color 1
IBM8514 IBM8514HI 0 640 x 480 256 color ?
IBM8514LO 0 1024 x 768 256 color ?
Return Value : None.
c) getbkcolor
Syntax
#include <graphics.h>
int getbkcolor(void);
Description
getbkcolor returns the current background color. (See the table in setbkcolor for details.)
Return Value getbkcolor returns the current background color.
7. Page | 7
d) getcolor
Syntax
#include <graphics.h>
int getcolor(void);
Description
getcolor returns the current drawing color. The drawing color is the value to which pixels are set
when lines and so on are drawn. For example, in CGAC0 mode, the palette contains four colors:
the background color, light green, light red, and yellow. In this mode, if getcolor returns 1, the
current drawing color is light green.
Return Valuen :getcolor returns the current drawing color.
e) getmaxx
Syntax
#include <graphics.h>
int getmaxx(void);
Description
getmaxx returns the maximum (screen-relative) x value for the current graphics driver and mode.
For example, on a CGA in 320*200 mode, getmaxx returns 319. getmaxx is invaluable for
centering, determining the boundaries of a region onscreen, and so on.
Return Value : getmaxx returns the maximum x screen coordinate.
f)getmaxx
Syntax
#include <graphics.h>
int getmaxy(void);
Description
getmaxy returns the maximum (screen-relative) y value for the current graphics driver and mode.
For example, on a CGA in 320*200 mode, getmaxy returns 199. getmaxy is invaluable for
centering, determining the boundaries of a region onscreen, and so on.
Return Value : getmaxy returns the maximum y screen coordinate.
g) getpixel
Syntax
#include <graphics.h>
unsigned getpixel(int x, int y);
Description
getpixel gets the color of the pixel located at (x,y).
Return Value : getpixel returns the color of the given pixel.
h) getx
Syntax
#include <graphics.h>
int getx(void);
Description
getx finds the current graphics position's x-coordinate. The value is viewport-relative.
Return Value : getx returns the x-coordinate of the current position.
8. Page | 8
i) gety
Syntax:
#include <graphics.h>
int gety(void);
Description:
gety returns the current graphics position's y-coordinate. The value is viewport-relative.
Return Value : gety returns the y-coordinate of the current position.
j) grapherrormsg
Syntax
#include <graphics.h>
char * grapherrormsg(int errorcode);
Description
grapherrormsg returns a pointer to the error message string associated with errorcode, the value
returned by graphresult.
Refer to the entry for errno in the Library Reference, Chapter 4, for a list of error messages and
mnemonics.
Return Value : grapherrormsg returns a pointer to an error message string.
k) graphresult
Syntax
#include <graphics.h>
int graphresult(void);
Description
graphresult returns the error code for the last graphics operation that reported an error and
resets the error level to grOk.
The following table lists the error codes returned by graphresult. The enumerated type
graph_errors defines the errors in this table. graph_errors is declared in graphics.h.
code constant Corresponding error message string
0 grOk No error
-1 grNoInitGraph (BGI) graphics not installed (use initgraph)
-2 grNotDetected Graphics hardware not detected
-3 grFileNotFound Device driver file not found
-4 grInvalidDriver Invalid device driver file
-5 grNoLoadMem Not enough memory to load driver
-6 grNoScanMem Out of memory in scan fill
-7 grNoFloodMem Out of memory in flood fill
-8 grFontNotFound Font file not found
-9 grNoFontMem Not enough memory to load font
-10 grInvalidMode Invalid graphics mode for selected driver
-11 grError Graphics error
-12 grIOerror Graphics I/O error
-13 grInvalidFont Invalid font file
-14 grInvalidFontNum Invalid font number
9. Page | 9
-15 grInvalidDeviceNum Invalid device number
-18 grInvalidVersion Invalid version number
Note: The variable maintained by graphresult is reset to 0 after graphresult has been called.
Therefore, you should store the value of graphresult into a temporary variable and then test it.
Return Value : graphresult returns the current graphics error number, an integer in the range -15
to 0; grapherrormsg returns a pointer to a string associated with the value returned by
graphresult.
l) initgraph
Syntax
#include <graphics.h>
void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
Description
initgraph initializes the graphics system by loading a graphics driver from disk (or validating a
registered driver), and putting the system into graphics mode.
To start the graphics system, first call the initgraph function. initgraph loads the graphics driver
and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver
and mode, or to autodetect the attached video adapter at run time and pick the corresponding
driver.
If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode.
initgraph also resets all graphics settings to their defaults (current position, palette, color,
viewport, and so on) and resets graphresult to 0.
Normally, initgraph loads a graphics driver by allocating memory for the driver (through
_graphgetmem), then loading the appropriate .BGI file from disk. As an alternative to this
dynamic loading scheme, you can link a graphics driver file (or several of them) directly into your
executable program file.
pathtodriver specifies the directory path where initgraph looks for graphics drivers. initgraph first
looks in the path specified in pathtodriver, then (if they are not there) in the current directory.
Accordingly, if pathtodriver is null, the driver files (*.BGI) must be in the current directory. This
is also the path settextstyle searches for the stroked character font files (*.CHR).
*graphdriver is an integer that specifies the graphics driver to be used. You can give it a value
using a constant of the graphics_drivers enumeration type, which is defined in graphics.h and
listed below.
graphics_drivers constant Numeric value
DETECT 0 (requests autodetect)
CGA 1
MCGA 2
EGA 3
EGA64 4
EGAMONO 5
IBM8514 6
HERCMONO 7
ATT400 8
VGA 9
PC3270 10
*graphmode is an integer that specifies the initial graphics mode (unless *graphdriver equals
DETECT; in which case, *graphmode is set by initgraph to the highest resolution available for
10. Page | 10
the detected driver). You can give *graphmode a value using a constant of the graphics_modes
enumeration type, which is defined in graphics.h and listed below.
graphdriver and graphmode must be set to valid values from the following tables, or you will get
unpredictable results. The exception is graphdriver = DETECT.
Palette listings C0, C1, C2, and C3 refer to the four predefined four-color palettes available on
CGA (and compatible) systems. You can select the background color (entry #0) in each of these
palettes, but the other colors are fixed.
Palette Number Three Colors
0 LIGHTGREEN LIGHTRED YELLOW
1 LIGHTCYAN LIGHTMAGENTA WHITE
2 GREEN RED BROWN
3 CYAN MAGENTA LIGHTGRAY
After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set
to the current graphics mode.
Graphics
Columns
Driver graphics_mode Value x Rows Palette Pages
CGA CGAC0 0 320 x 200 C0 1
CGAC1 1 320 x 200 C1 1
CGAC2 2 320 x 200 C2 1
CGAC3 3 320 x 200 C3 1
CGAHI 4 640 x 200 2 color 1
MCGA MCGAC0 0 320 x 200 C0 1
MCGAC1 1 320 x 200 C1 1
MCGAC2 2 320 x 200 C2 1
MCGAC3 3 320 x 200 C3 1
MCGAMED 4 640 x 200 2 color 1
MCGAHI 5 640 x 480 2 color 1
EGA EGALO 0 640 x 200 16 color 4
EGAHI 1 640 x 350 16 color 2
EGA64 EGA64LO 0 640 x 200 16 color 1
EGA64HI 1 640 x 350 4 color 1
EGA-MONO EGAMONOHI 3 640 x 350 2 color 1 w/64K
EGAMONOHI 3 640 x 350 2 color 2 w/256K
HERC HERCMONOHI 0 720 x 348 2 color 2
ATT400 ATT400C0 0 320 x 200 C0 1
ATT400C1 1 320 x 200 C1 1
ATT400C2 2 320 x 200 C2 1
ATT400C3 3 320 x 200 C3 1
ATT400MED 4 640 x 200 2 color 1
ATT400HI 5 640 x 400 2 color 1
VGA VGALO 0 640 x 200 16 color 2
VGAMED 1 640 x 350 16 color 2
11. Page | 11
VGAHI 2 640 x 480 16 color 1
PC3270 PC3270HI 0 720 x 350 2 color 1
IBM8514 IBM8514HI 0 640 x 480 256 color ?
IBM8514LO 0 1024 x 768 256 color ?
Return Value : initgraph always sets the internal error code; on success, it sets the code to 0. If an
error occurred, *graphdriver is set to -2, -3, -4, or -5, and graphresult returns the same value as
listed below:
Constant Name Number Meaning
grNotDetected -2 Cannot detect a graphics card
grFileNotFound -3 Cannot find driver file
grInvalidDriver -4 Invalid driver
grNoLoadMem -5 Insufficient memory to load driver
m) outtext
Syntax
#include <graphics.h>
void outtext(char *textstring);
Description
outtext displays a text string in the viewport, using the current font, direction, and size.
outtext outputs textstring at the current position (CP). If the horizontal text justification is
LEFT_TEXT and the text direction is HORIZ_DIR, the CP's x-coordinate is advanced by
textwidth(textstring). Otherwise, the CP remains unchanged.
To maintain code compatibility when using several fonts, use textwidth and textheight to
determine the dimensions of the string.
If a string is printed with the default font using outtext, any part of the string that extends
outside the current viewport is truncated.
outtext is for use in graphics mode; it will not work in text mode.
Return Value : None.
n) outtextxy
Syntax
#include <graphics.h>
void outtextxy(int x, int y, char *textstring);
Description
outtextxy displays a text string in the viewport at the given position (x, y), using the current
justification settings and the current font, direction, and size.
To maintain code compatibility when using several fonts, use textwidth and textheight to
determine the dimensions of the string.
If a string is printed with the default font using outtext or outtextxy, any part of the string that
extends outside the current viewport is truncated.
outtextxy is for use in graphics mode; it will not work in text mode.
Return Value : None.
o) putpixel
Syntax
#include <graphics.h>
void putpixel(int x, int y, int color);
12. Page | 12
Description
putpixel plots a point in the color defined by color at (x,y).
Return Value : None.
p)setbkcolor
Syntax
#include <graphics.h>
void setbkcolor(int color);
Description
setbkcolor sets the background to the color specified by color. The argument color can be a
name or a number as listed below. (These symbolic names are defined in graphics.h.)
Name Value
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15
For example, if you want to set the background color to blue, you can call setbkcolor(BLUE) /*
or */ setbkcolor(1)On CGA and EGA systems, setbkcolor changes the background color by
changing the first entry in the palette. If you use an EGA or a VGA, and you change the palette
colors with setpalette or setallpalette, the defined symbolic constants might not give you the
correct color. This is because the parameter to setbkcolor indicates the entry number in the
current palette rather than a specific color (unless the parameter passed is 0, which always sets
the background color to black).
Return Value :None.
q)setcolor
Syntax
#include <graphics.h>
void setcolor(int color);
Description
setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The
current drawing color is the value to which pixels are set when lines, and so on are drawn. The
drawing colors shown below are available for the CGA and EGA, respectively.
Palette Number Three Colors
13. Page | 13
0 LIGHTGREEN LIGHTRED YELLOW
1 LIGHTCYAN LIGHTMAGENTA WHITE
2 GREEN RED BROWN
3 CYAN MAGENTA LIGHTGRAY
Name Value
BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15
You select a drawing color by passing either the color number itself or the equivalent symbolic
name to setcolor. For example, in CGAC0 mode, the palette contains four colors: the
background color, light green, light red, and yellow. In this mode, either setcolor(3) or
setcolor(CGA_YELLOW) selects a drawing color of yellow.
Return Value:None.
r) setfillpattern
Syntax
#include <graphics.h>
void setfillpattern(char *upattern, int color);
Description
setfillpattern is like setfillstyle, except that you use it to set a user-defined 8x8 pattern rather than
a predefined pattern.
upattern is a pointer to a sequence of 8 bytes, with each byte corresponding to 8 pixels in the
pattern. Whenever a bit in a pattern byte is set to 1, the corresponding pixel is plotted.
Return Value : None.
14. Page | 14
3. Write a program to draw pixels into the various location of the VDU.
Program:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int x,y;
cout<<"enter the x or y Quordinates x,y=";
cin>>x>>y;
initgraph(&gd,&gd,"c:turboc3bgi");
int r1,r2;
r1=getmaxx();
r2=getmaxy();
while(!kbhit())
{
delay(100);
cleardevice();
for(int i=x;i<r1;i+=30)
{
for(int j=y;j<r2;j+=30)
putpixel(i,j,15);
}
}
getch();
closegraph();
}
Output:
15. Page | 15
4 . Write a program to transfer the origin of monitor from top left corner to center of the
monitor.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translate();
void main()
{
int ch;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:turboc3bgi");
setcolor(6);
printf("Object: ");
rectangle(100,175,175,100);
int tx,ty;
setcolor(2);
outtextxy(240,10,"TRANSLATION");
outtextxy(238,20,"------------");
printf("nEnter tx: ");
scanf("%d",&tx);
printf("nEnter ty: ");
scanf("%d",&ty);
cleardevice();
rectangle(100,150,150,100);
printf("nAfter Translation");
rectangle(100+tx,150+ty,150+tx,100+ty);
getch();
closegraph();
}
Output:
16. Page | 16
5. Write a program to implement Digital Differential Analyzer (DDA) line Drawing
algorithm.
Program:
#include <graphics.h>
#include <stdio.h>
#include<conio.h>
#include <math.h>
#include<dos.h>
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i,gd,gm;
printf("Enter the value of x1 : ");
scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y1);
printf("Enter the value of x2 : ");
scanf("%f",&x2);
printf("Enter the value of y1 : ");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:turboc3bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,1);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
Output:
18. Page | 18
6. Write a program to implement the Bresenhams line drawing algorithm.
Program:
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("nntEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("nntEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:turboc3bgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
22. Page | 22
8. Write a program to show the following attribute of output primitives.
A) Line style B) Color C) Intensity
A) Line styles
Program:
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
void main()
{
int gd=DETECT,gm;
int s;
char *lname[]={"SOLID LINE","DOTTED LINE","CENTER LINE",
"DASHED LINE","USERBIT LINE"};
initgraph(&gd,&gm,"c:turboc3bgi");
clrscr();
cleardevice();
printf("Line styles:");
for (s=0;s<5;s++)
{
setlinestyle(s,1,3);
line(100,30+s*50,250,250+s*50);
outtextxy(255,250+s*50,lname[s]);
}
getch();
closegraph();
}
Output:
B)Color
Program:
#include<stdio.h>
#include<conio.h>
23. Page | 23
main()
{
clrscr();
textcolor(RED);
cprintf("C programming n ");
textcolor(BLUE);
cprintf("C programming n ");
textcolor(GREEN);
cprintf("C programming n ");
textcolor(YELLOW);
cprintf("C programming n ");
textcolor(BROWN);
cprintf("C programming n ");
getch();
return 0;
}
Output:
C)Intensity:
Hue, Intensity, Brightness
The Hue (or simply, the "colour") is the dominant Wavelength or dominant frequency
Energy distribution of a light source with a dominant frequency near the red end of the
frequency range .The integration of the energy for all the visible wavelengths is proportional to
the intensity of the colour.
Intensity : Radiant Energy emitted per unit of time, per unit solid angle, and per unit projected
area of the source (related to the luminance of the source)
Brightness : perceived intensity of light.
24. Page | 24
9. Write a program to implement the following aria fill algorithm.
A) Boundary fill B) Flood fill C) Scan line algorithm
A) Boundary Fill algorithm
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(int x,int y);
void fill_left(int x,int y);
void main()
{
int gd=DETECT,gm,x,y,n,i;
clrscr();
initgraph(&gd,&gm,"c:turboc3bgi");
printf("*** Boundary Fill algorithm ***");
/*- draw object -*/
line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);
/*- set seed point -*/
x=100; y=100;
fill_right(x,y);
fill_left(x-1,y);
getch();
}
void fill_right(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y); x=x-1;
fill_right(x,y-1);
fill_right(x,y+1);
}
delay(1);
}
void fill_left(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_left(--x,y); x=x+1;
fill_left(x,y-1);
fill_left(x,y+1);
}
delay(1);
}
Output:
25. Page | 25
B) Flood Fill
Program:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
void flood(int,int,int,int);
void main()
{
int gd,gm=DETECT;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:turboc3bgi");
rectangle(50,50,100,100);
flood(55,55,12,0);
getch();
}
void flood(int x,int y, int fill_col, int old_col)
{
if(getpixel(x,y)==old_col)
{
delay(10);
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);
}
}
Output:
26. Page | 26
C) Scan line
Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
class Edge
{
public:
int yUpper;
float xIntersect;
float dxPerScan;
Edge *next;
};
class PointCoordinates
{
public:
float x;
float y;
PointCoordinates( )
{
x=0;
y=0;
}
};
class LineCoordinates
{
public:
float x_1;
float y_1;
float x_2;
float y_2;
LineCoordinates( )
{
x_1=0;
31. Page | 31
}
void Polygon(const int n,const int coordinates[])
{
if(n>=2)
{
Line(coordinates[0],coordinates[1], coordinates[2],coordinates[3]);
for(int count=1;count<(n-1);count++)
Line(coordinates[(count*2)],coordinates[((count*2)+1)], coordinates[((count+1)*2)],
coordinates[(((count+1)*2)+1)]);
}
}
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
32. Page | 32
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}
void show_screen( )
{
setfillstyle(1,1);
bar(178,26,450,38);
settextstyle(0,0,1);
setcolor(15);
outtextxy(5,5,"");
outtextxy(5,17,"");
outtextxy(5,29,"");
outtextxy(5,41,"");
outtextxy(5,53,"");
setcolor(11);
outtextxy(185,29,"Scan Line Polygon Fill Algorithm");
setcolor(15);
for(int count=0;count<=30;count++)
outtextxy(5,(65+(count*12)),"");
outtextxy(5,438,"");
outtextxy(5,450,"");
outtextxy(5,462,"");
setcolor(12);
outtextxy(229,450,"");
}
Output:
36. Page | 36
void Polygon(const int n,const int coordinates[])
{
if(n>=2)
{
Line(coordinates[0],coordinates[1],coordinates[2],coordinates[3]);
for(int count=1;count<(n-1);count+
Line(coordinates[(count*2)],coordinates[((count*2)+1)],coordinates[((count+1)*2)],
coordinates[(((count+1)*2)+1)]);
}
}
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
37. Page | 37
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}
void show_screen( )
{
setfillstyle(1,1);
bar(205,26,430,38);
settextstyle(0,0,1);
setcolor(15);
outtextxy(5,5,"");
outtextxy(5,17,"");
outtextxy(5,29,"");
outtextxy(5,41,"");
outtextxy(5,53,"");
setcolor(11);
outtextxy(210,29,"Translation Transformation");
setcolor(15);
for(int count=0;count<=30;count++)
outtextxy(5,(65+(count*12)),"");
outtextxy(5,438,"");
outtextxy(5,450,"");
outtextxy(5,462,"");
setcolor(12);
outtextxy(229,450,"Press any Key to exit.");
}
39. Page | 39
11. Write a program to perform scaling of two dimensional objects.
A) About the origin B) About fixed point
Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void apply_fixed_point_scaling(const int,int [],const float, const float,const int,const int);
void multiply_matrices(const float[3],const float[3][3],float[3]);
void Polygon(const int,const int []);
void Line(const int,const int,const int,const int);
int main( )
{
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,"..Bgi");
show_screen( );
int polygon_points[10]={ 270,290, 270,190, 370,190, 370,290, 270,290 };
setcolor(15);
Polygon(5,polygon_points);
setcolor(15);
settextstyle(0,0,1);
outtextxy(50,400,"*** (320,240) is taken as Fixed Point.");
outtextxy(50,415,"*** Use '+' and '-' Keys to apply Scaling.");
int key_code=0;
char Key=NULL;
do
{
Key=NULL;
key_code=0;
Key=getch( );
key_code=int(Key);
if(key_code==0)
{
Key=getch( );
key_code=int(Key);
}
if(key_code==27)
break;
else if(key_code==43)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_fixed_point_scaling(5,polygon_points, 1.1,1.1,320,240);
setcolor(10);
Polygon(5,polygon_points);
}
else if(key_code==45)
{
40. Page | 40
setfillstyle(1,0);
bar(40,70,600,410);
apply_fixed_point_scaling(5,polygon_points, 0.9,0.9,320,240);
setcolor(12);
Polygon(5,polygon_points);
}
}
while(1);
return 0;
}
void apply_fixed_point_scaling(const int n,int coordinates[],const float Sx,const float Sy,
const int xf,const int yf)
{
for(int count_1=0;count_1<n;count_1++)
{
float matrix_a[3]={coordinates[(count_1*2)] coordinates[((count_1*2)+1)],1};
float matrix_b[3][3]={ {Sx,0,0} , {0,Sy,0} ,{ ((1-Sx)*xf),((1-Sy)*yf),1} };
float matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count_1*2)]=(int)(matrix_c[0]+0.5);
coordinates[((count_1*2)+1)]=(int)(matrix_c[1]+0.5);
}
}
void multiply_matrices(const float matrix_1[3],const float matrix_2[3][3],float matrix_3[3])
{
for(int count_1=0;count_1<3;count_1++)
{
for(int count_2=0;count_2<3;count_2++)
matrix_3[count_1]+=
(matrix_1[count_2]*matrix_2[count_2][count_1]);
}
}
void Polygon(const int n,const int coordinates[])
{
if(n>=2)
{
Line(coordinates[0],coordinates[1], coordinates[2],coordinates[3]);
for(int count=1;count<(n-1);count++)
Line(coordinates[(count*2)],coordinates[((count*2)+1)], coordinates[((count+1)*2)],
coordinates[(((count+1)*2)+1)]);
}
}
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
41. Page | 41
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
43. Page | 43
12. Write a program to perform the Rotation of two dimensional objects.
A) About the origin B) About fixed point
Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void apply_pivot_point_rotation(const int,int [],float,const int,const int);
void multiply_matrices(const float[3],const float[3][3],float[3]);
void Polygon(const int,const int []);
void Line(const int,const int,const int,const int);
int main( )
{
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,"..Bgi");
show_screen( );
int polygon_points[8]={ 250,290, 320,190, 390,290, 250,290 };
setcolor(15);
Polygon(5,polygon_points);
setcolor(15);
settextstyle(0,0,1);
outtextxy(50,400,"*** (320,240) is taken as Fix Point.");
outtextxy(50,415,"*** Use '+' and '-' Keys to apply Rotation.");
int key_code=0;
char Key=NULL;
do
{
Key=NULL;
key_code=0;
Key=getch( );
key_code=int(Key);
if(key_code==0)
{
Key=getch( );
key_code=int(Key);
}
if(key_code==27)
break;
else if(key_code==43)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_pivot_point_rotation(4,polygon_points,5,320,240);
setcolor(10);
Polygon(4,polygon_points);
}
else if(key_code==45)
{
44. Page | 44
setfillstyle(1,0);
bar(40,70,600,410);
apply_pivot_point_rotation(4,polygon_points,-5,320,240);
setcolor(12);
Polygon(4,polygon_points);
}
}
while(1);
return 0;
}
void apply_pivot_point_rotation(const int n,int coordinates[],
float angle,const int xr,const int yr)
{
angle*=(M_PI/180);
for(int count_1=0;count_1<n;count_1++)
{
float matrix_a[3]={coordinates[(count_1*2)],coordinates[((count_1*2)+1)],1};
float temp_1=(((1-cos(angle))*xr)+(yr*sin(angle)));
float temp_2=(((1-cos(angle))*yr)-(xr*sin(angle)));
float matrix_b[3][3]={ { cos(angle),sin(angle),0 } ,{ temp_1,temp_2,1 } };
float matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count_1*2)]=(int)(matrix_c[0]+0.5);
coordinates[((count_1*2)+1)]=(int)(matrix_c[1]+0.5);
}
}
void multiply_matrices(const float matrix_1[3],const float matrix_2[3][3],float matrix_3[3])
{
for(int count_1=0;count_1<3;count_1++)
{
for(int count_2=0;count_2<3;count_2++)
matrix_3[count_1]+= (matrix_1[count_2]*matrix_2[count_2][count_1]);
}
}
void Polygon(const int n,const int coordinates[])
{
if(n>=2)
{
Line(coordinates[0],coordinates[1], coordinates[2],coordinates[3]);
for(int count=1;count<(n-1);count++)
Line(coordinates[(count*2)],coordinates[((count*2)+1)],coordinates[((count+1)*2)],
coordinates[(((count+1)*2)+1)]);
}
}
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
45. Page | 45
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
47. Page | 47
13. Write a program to perform reflection of two dimensional objects.
A) Y=0(X-axis) B) X=0(Y-axis) C) Y=X
Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void apply_reflection_along_x_axis(const int,int []);
void apply_reflection_along_y_axis(const int,int []);
void apply_reflection_wrt_origin(const int,int []);
void multiply_matrices(const int[3],const int[3][3],int[3]);
void Polygon(const int,const int []);
void Line(const int,const int,const int,const int);
int main( )
{
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,"..Bgi");
show_screen( );
setcolor(15);
Line(320,100,320,400);
Line(315,105,320,100);
Line(320,100,325,105);
Line(315,395,320,400);
Line(320,400,325,395);
Line(150,240,500,240);
Line(150,240,155,235);
Line(150,240,155,245);
Line(500,240,495,235);
Line(500,240,495,245);
settextstyle(2,0,4);
outtextxy(305,85,"y-axis");
outtextxy(305,402,"y'-axis");
outtextxy(505,233,"x-axis");
outtextxy(105,233,"x'-axis");
outtextxy(380,100,"Original Object");
outtextxy(380,385,"Reflection along x-axis");
outtextxy(135,100,"Reflection along y-axis");
outtextxy(135,385,"Reflection w.r.t origin");
int polygon_points[8]={ 350,200, 380,150, 470,200, 350,200 };
int x_polygon[8]={ 350,200, 380,150, 470,200, 350,200 };
int y_polygon[8]={ 350,200, 380,150, 470,200, 350,200 };
int origin_polygon[8]={ 350,200, 380,150, 470,200, 350,200 };
setcolor(15);
Polygon(4,polygon_points);
apply_reflection_along_x_axis(4,x_polygon);
setcolor(12);
Polygon(4,x_polygon);
apply_reflection_along_y_axis(4,y_polygon);
48. Page | 48
setcolor(14);
Polygon(4,y_polygon);
apply_reflection_wrt_origin(4,origin_polygon);
setcolor(10);
Polygon(4,origin_polygon);
getch( );
return 0;
}
void apply_reflection_along_x_axis(const int n,int coordinates[])
{
for(int count=0;count<n;count++)
{
int matrix_a[3]={coordinates[(count*2)],coordinates[((count*2)+1)],1};
int matrix_b[3][3]={ {1,0,0} , {0,-1,0} ,{ 0,0,1} };
int matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count*2)]=matrix_c[0];
coordinates[((count*2)+1)]=(480+matrix_c[1]);
}
}
void apply_reflection_along_y_axis(const int n,int coordinates[])
{
for(int count=0;count<n;count++)
{
int matrix_a[3]={coordinates[(count*2)],coordinates[((count*2)+1)],1};
int matrix_b[3][3]={ {-1,0,0} , {0,1,0} ,{ 0,0,1} };
int matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count*2)]=(640+matrix_c[0]);
coordinates[((count*2)+1)]=matrix_c[1];
}
}
void apply_reflection_wrt_origin(const int n,int coordinates[])
{
for(int count=0;count<n;count++)
{
int matrix_a[3]={coordinates[(count*2)], coordinates[((count*2)+1)],1};
int matrix_b[3][3]={ {-1,0,0} , {0,-1,0} ,{ 0,0,1} };
int matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count*2)]=(640+matrix_c[0]);
coordinates[((count*2)+1)]=(480+matrix_c[1]);
}
}
void multiply_matrices(const int matrix_1[3], const int matrix_2[3][3],int matrix_3[3])
{
for(int count_1=0;count_1<3;count_1++)
{
for(int count_2=0;count_2<3;count_2++)
matrix_3[count_1]+=
(matrix_1[count_2]*matrix_2[count_2][count_1]);
49. Page | 49
}
}
void Polygon(const int n,const int coordinates[])
{
if(n>=2)
{
Line(coordinates[0],coordinates[1],coordinates[2],coordinates[3]);
for(int count=1;count<(n-1);count++)
Line(coordinates[(count*2)],coordinates[((count*2)+1)], coordinates[((count+1)*2)],
coordinates[(((count+1)*2)+1)]);
}
}
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
50. Page | 50
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}
void show_screen( )
{
setfillstyle(1,1);
bar(208,26,430,38);
settextstyle(0,0,1);
setcolor(15);
outtextxy(5,5,"");
outtextxy(5,17,"");
outtextxy(5,29,"");
outtextxy(5,41,"");
outtextxy(5,53,"");
setcolor(11);
outtextxy(218,29,"___________ Reflection ___________");
setcolor(15);
for(int count=0;count<=30;count++)
outtextxy(5,(65+(count*12)),"");
outtextxy(5,438,"");
outtextxy(5,450,"");
outtextxy(5,462,"");
setcolor(12);
outtextxy(229,450,"Press any Key to exit.");
}
Output:
52. Page | 52
outtextxy(305,402,"y'-axis");
outtextxy(505,233,"x-axis");
outtextxy(105,233,"x'-axis");
outtextxy(350,100,"Reflection about the line y=x");
outtextxy(115,100,"Reflection about the line y=-x");
int x_polygon[8]={ 340,200, 420,120, 370,120, 340,200 };
int y_polygon[8]={ 300,200, 220,120, 270,120, 300,200 };
setcolor(15);
Polygon(4,x_polygon);
Polygon(4,y_polygon);
apply_reflection_about_line_yex(4,x_polygon);
apply_reflection_about_line_yemx(4,y_polygon);
setcolor(7);
Polygon(4,x_polygon);
Polygon(4,y_polygon);
getch( );
return 0;
}
void apply_reflection_about_line_yex(const int n,int coordinates[])
{
apply_rotation(n,coordinates,45);
apply_reflection_along_x_axis(n,coordinates);
apply_rotation(n,coordinates,-45);
}
void apply_reflection_about_line_yemx(const int n,int coordinates[])
{
apply_rotation(n,coordinates,45);
apply_reflection_along_y_axis(n,coordinates);
apply_rotation(n,coordinates,-45);
}
void apply_rotation(const int n,int coordinates[],float angle)
{
float xr=320;
float yr=240;
angle*=(M_PI/180);
for(int count_1=0;count_1<n;count_1++)
{
float matrix_a[3]={coordinates[(count_1*2)],coordinates[((count_1*2)+1)],1};
float temp_1=(((1-cos(angle))*xr)+(yr*sin(angle)));
float temp_2=(((1-cos(angle))*yr)-(xr*sin(angle)));
float matrix_b[3][3]={ { cos(angle),sin(angle),0 } , { -sin(angle),cos(angle),0 } ,
{ temp_1,temp_2,1 } };
float matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count_1*2)]=(int)(matrix_c[0]+0.5);
coordinates[((count_1*2)+1)]=(int)(matrix_c[1]+0.5);
}
}
void apply_reflection_along_x_axis(const int n,int coordinates[])
{
for(int count=0;count<n;count++)
53. Page | 53
{
float matrix_a[3]={coordinates[(count*2)],coordinates[((count*2)+1)],1};
float matrix_b[3][3]={ {1,0,0} , {0,-1,0} ,{ 0,0,1} };
float matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count*2)]=matrix_c[0];
coordinates[((count*2)+1)]=(480+matrix_c[1]);
}
}
void apply_reflection_along_y_axis(const int n,int coordinates[])
{
for(int count=0;count<n;count++)
{
float matrix_a[3]={coordinates[(count*2)],coordinates[((count*2)+1)],1};
float matrix_b[3][3]={ {-1,0,0} , {0,1,0} ,{ 0,0,1} };
float matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count*2)]=(640+matrix_c[0]);
coordinates[((count*2)+1)]=matrix_c[1];
}
}
void multiply_matrices(const float matrix_1[3], const float matrix_2[3][3],float matrix_3[3])
{
for(int count_1=0;count_1<3;count_1++)
{
for(int count_2=0;count_2<3;count_2++)
matrix_3[count_1]+=
(matrix_1[count_2]*matrix_2[count_2][count_1]);
}
}
void Polygon(const int n,const int coordinates[])
{
if(n>=2)
{
Line(coordinates[0],coordinates[1], coordinates[2],coordinates[3]);
for(int count=1;count<(n-1);count++)
Line(coordinates[(count*2)],coordinates[((count*2)+1)],coordinates[((count+1)*2)],
coordinates[(((count+1)*2)+1)]);
}
}
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
54. Page | 54
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}
void Dashed_line(const int x_1,const int y_1,const int x_2,
55. Page | 55
const int y_2,const int line_type)
{
int count=0;
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
if((count%2)!=0 && line_type==0)
putpixel(x,y,color);
else if((count%5)!=4 && line_type==1)
putpixel(x,y,color);
else if((count%10)!=8 && (count%10)!=9 && line_type==2)
putpixel(x,y,color);
else if((count%20)!=18 && (count%20)!=19 && line_type==3)
putpixel(x,y,color);
else if((count%12)!=7 && (count%12)!=8 &&
(count%12)!=10 && (count%12)!=11 && line_type==4)
putpixel(x,y,color);
count++;
}
}
60. Page | 60
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
64. Page | 64
}
}
void Polygon(const int n,const int coordinates[])
{
if(n>=2)
{
Line(coordinates[0],coordinates[1], coordinates[2],coordinates[3]);
for(int count=1;count<(n-1);count++)
Line(coordinates[(count*2)],coordinates[((count*2)+1)], coordinates[((count+1)*2)],
coordinates[(((count+1)*2)+1)]);
}
}
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
65. Page | 65
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}
void show_screen( )
{
setfillstyle(1,1);
bar(205,26,430,38);
settextstyle(0,0,1);
setcolor(15);
outtextxy(5,5,"");
outtextxy(5,17,"");
outtextxy(5,29,"");
outtextxy(5,41,"");
outtextxy(5,53,"");
setcolor(11);
outtextxy(210,29,"Translation Transformation");
setcolor(15);
for(int count=0;count<=30;count++)
outtextxy(5,(65+(count*12)),"");
outtextxy(5,438,"");
outtextxy(5,450,"");
outtextxy(5,462,"");
setcolor(12);
outtextxy(229,450,"Press any Key to exit.");
}