Sie sind auf Seite 1von 74

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)
Constant

Back- ForeValue
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
CGA0
CGA1
CGA2
CGA3

CGA_LIGHTGREEN
CGA_LIGHTCYAN
CGA_GREEN
CGA_CYAN

CGA_LIGHTRED
CGA_YELLOW
CGA_LIGHTMAGENTA CGA_WHITE
CGA_RED
CGA_BROWN
CGA_MAGENTA
CGA_LIGHTGRAY

Page | 1

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

EGA_BLACK
EGA_BLUE
EGA_GREEN
EGA_CYAN
EGA_RED
EGA_MAGENTA
EGA_LIGHTGRAY
EGA_BROWN

0
1
2
3
4
5
7
20

Value

EGA_DARKGRAY
EGA_LIGHTBLUE
EGA_LIGHTGREEN
EGA_LIGHTCYAN
EGA_LIGHTRED
EGA_LIGHTMAGENTA
EGA_YELLOW
EGA_WHITE

56
57
58
59
60
61
62
63

B ) fill_pattern
Enum: Fill patterns for getfillsettings and setfillstyle.
Names

Value

Means Fill With...

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

0
1
2
3
4
5
6
7
8
9
10
11
12

Background color
Solid fill
--///
///, thick lines
\\\, thick lines
\\\
Light hatch
Heavy crosshatch
Interleaving lines
Widely spaced dots
Closely spaced dots
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
CGA
MCGA
EGA
EGA64
EGAMONO
IBM8514
HERCMONO
ATT400

0 (requests autodetection)
1
2
3
4
5
6
7
8
Page | 2

VGA
PC3270

9
10

D ) graphics_errors
graphics_errors
<GRAPHICS.H>
Enum: Error return code from graphresult
Error graphics_errors
code
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-11
-12
-13
-14
-15
-18

constant

Corresponding error message string

grOk
grNoInitGraph
grNotDetected
grFileNotFound
grInvalidDriver
grNoLoadMem
grNoScanMem
grNoFloodMem
grFontNotFound
grNoFontMem
grInvalidMode
grError
grIOerror
grInvalidFont
grInvalidFontNum
grInvalidDeviceNum
grInvalidVersion

No error
(BGI) graphics not installed (use initgraph)
Graphics hardware not detected
Device driver file not found
Invalid device driver file
Not enough memory to load driver
Out of memory in scan fill
Out of memory in flood fill
Font file not found
Not enough memory to load font
Invalid graphics mode for selected driver
Graphics error
Graphics I/O error
Invalid font file
Invalid font number
Invalid device number
Invalid version number

E) graphics_modes
Enum: Graphics modes for each BGI driver
Graphics driver graphics_modes Value
CGA

CGAC0
CGAC1
CGAC2
CGAC3
CGAHI

0
1
2
3
4

MCGA

MCGAC0
MCGAC1
MCGAC2
MCGAC3
MCGAMED
MCGAHI

0
1
2
3

Column x Row
320 x 200
320 x 200
320 x 200
320 x 200
640 x 200

Palette
C0
C1
C2
C3
2 color

320 x 200
C0
320 x 200
C1
320 x 200
C2
320 x 200
C3
4 640 x 200 2 color 1
5 640 x 480 2 color 1

EGA

EGALO
EGAHI

0
1

640 x 200 16 color 4


640 x 350 16 color 2

EGA64

EGA64LO

640 x 200 16 color 1


Page | 3

Pages
1
1
1
1
1
1
1
1
1

EGA64HI

640 x 350

4 color 1

EGA-MONO

EGAMONOHI
EGAMONOHI

3
3

640 x 350 2 color 1*


640 x 350 2 color 2**

HERC

HERCMONOHI

720 x 348

ATT400

ATT400C0
ATT400C1
ATT400C2
ATT400C3
ATT400MED
ATT400HI

0
1
2
3
4
5

320 x 200
C0
320 x 200
C1
320 x 200
C2
320 x 200
C3
640 x 200 2 color
640 x 400 2 color

VGA

VGALO
VGAMED

0
1

640 x 200 16 color 2


640 x 350 16 color 2

PC3270

PC3270HI

720 x 350 2 color

IBM8514

IBM8514HI
IBM8514LO

1 1024 x 760
0 640 x 480

2 color 2

256 color
256 color

* 64K on EGAMONO card ** 256K on EGAMONO card

Page | 4

1
1
1
1
1
1

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.
Columns
Graphics
Driver

graphics_mode

Value

x Rows

Page | 5

Palette

Pages

CGA

CGAC0
CGAC1
CGAC2
CGAC3
CGAHI

0
1
2
3
4

320 x 200
320 x 200
320 x 200
320 x 200
640 x 200

MCGA

MCGAC0

320 x 200 C0

MCGAC1
MCGAC2
MCGAC3
MCGAMED
MCGAHI

1
2
3
4
5

320 x 200
320 x 200
320 x 200
640 x 200
640 x 480

1
1
1
1
1

EGALO

640 x 200 16 color 4

EGAHI

640 x 350 16 color 2

EGA64LO

640 x 200 16 color 1

EGA64HI

640 x 350 4 color

EGA-MONO EGAMONOHI 3
EGAMONOHI 3

640 x 350 2 color


640 x 350 2 color

1 w/64K
2 w/256K

HERC

HERCMONOHI 0

720 x 348 2 color

ATT400

ATT400C0
ATT400C1
ATT400C2
ATT400C3
ATT400MED
ATT400HI

0
1
2
3
4
5

320 x 200
320 x 200
320 x 200
320 x 200
640 x 200
640 x 400

1
1
1
1
1
1

VGA

VGALO
VGAMED
VGAHI

0
1
2

640 x 200 16 color 2


640 x 350 16 color 2
640 x 480 16 color 1

PC3270

PC3270HI

720 x 350 2 color

IBM8514

IBM8514HI
IBM8514LO

0
0

640 x 480 256 color ?


1024 x 768 256 color ?

EGA
EGA64

C0
C1
C2
C3
2 color
C1
C2
C3
2 color
2 color

C0
C1
C2
C3
2 color
2 color

1
1
1
1
1

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.

Page | 6

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.
Page | 7

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
Page | 8

-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
Page | 9

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.
Columns
Graphics
Driver
CGA

MCGA

graphics_mode
CGAC0
CGAC1
CGAC2
CGAC3
CGAHI
MCGAC0

Value
0
1
2
3
4
0

MCGAC1
1
MCGAC2
2
MCGAC3
3
MCGAMED
4
MCGAHI
5
EGA
EGALO
0
EGAHI
1
EGA64
EGA64LO
0
EGA64HI
1
EGA-MONO EGAMONOHI 3
EGAMONOHI 3
HERC
HERCMONOHI 0
ATT400
ATT400C0
0
ATT400C1
1
ATT400C2
2
ATT400C3
3
ATT400MED
4
ATT400HI
5
VGA
VGALO
0
VGAMED
1

x Rows
320 x 200
320 x 200
320 x 200
320 x 200
640 x 200
320 x 200

Palette
C0
C1
C2
C3
2 color
C0

Pages
1
1
1
1
1
1

320 x 200
320 x 200
320 x 200
640 x 200
640 x 480
640 x 200
640 x 350
640 x 200
640 x 350
640 x 350
640 x 350
720 x 348
320 x 200
320 x 200
320 x 200
320 x 200
640 x 200
640 x 400
640 x 200
640 x 350

C1
C2
C3
2 color
2 color
16 color
16 color
16 color
4 color
2 color
2 color
2 color
C0
C1
C2
C3
2 color
2 color
16 color
16 color

1
1
1
1
1
4
2
1
1
1 w/64K
2 w/256K
2
1
1
1
1
1
1
2
2

Page | 10

PC3270

VGAHI
PC3270HI

2
0

640 x 480 16 color 1


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);
Page | 11

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
Page | 12

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.

Page | 13

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:\\turboc3\\bgi");
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:

Page | 14

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:\\turboc3\\bgi");
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:

Page | 15

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:\\turboc3\\bgi");
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:

Page | 16

Page | 17

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("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter 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:\\turboc3\\bgi");
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();
}

Page | 18

Output:

Page | 19

7. Write a program to implement midpoint (Bresenhams) circle generation algorithm.


Program:
#include <stdio.h>
#include<conio.h>
#include <dos.h>
#include <graphics.h>
void circleBres(int, int, int);
void drawCircle(int, int, int, int);
void main()
{
int xc, yc, r;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleBres(xc, yc, r);
}
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
while (x < y)
{
drawCircle(xc, yc, x, y);
x++;
if (d < 0)
d = d + 4 * x + 6;
else
{
y--;
d = d + 4 * (x - y) + 10;
}
drawCircle(xc, yc, x, y);
delay(50);
}
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
putpixel(xc+y, yc+x, WHITE);
putpixel(xc-y, yc+x, WHITE);
putpixel(xc+y, yc-x, WHITE);
putpixel(xc-y, yc-x, WHITE);
getch();
}
Page | 20

Output:

Page | 21

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:\\turboc3\\bgi");
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>
Page | 22

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.
Page | 23

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:\\turboc3\\bgi");
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:
Page | 24

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:\\turboc3\\bgi");
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:

Page | 25

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;
Page | 26

y_1=0;
x_2=0;
y_2=0;

}
LineCoordinates(const float x1,const float y1, const float x2,const float y2)
{
x_1=x1;
y_1=y1;
x_2=x2;
y_2=y2;
}

};
void show_screen( );
void Fill_polygon(const int,const int [],const int);
void insertEdge(Edge *,Edge *);
void makeEdgeRec(const PointCoordinates,const PointCoordinates, const int,Edge *,Edge *[]);
void buildEdgeList(const int,const PointCoordinates [],Edge *[]);
void buildActiveList(const int,Edge *,Edge *[]);
void fillScan(const int,const Edge *,const int);
void deleteAfter(Edge []);
void updateActiveList(const int,Edge []);
void resortActiveList(Edge []);
const int yNext(const int,const int,const PointCoordinates []);
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 n=10;
int polygon_points[20]={ 220,340 , 220,220 , 250,170 , 270,200 , 300,140 , 320,240 , 320,290
, 420,220 ,420,340 , 220,340 };
setcolor(15);
Polygon(10,polygon_points);
Fill_polygon(n,polygon_points,9);
getch( );
return 0;
}
void Fill_polygon(const int n,const int ppts[],const int fill_color)
{
Edge *edges[480];
Edge *active;
PointCoordinates *pts=new PointCoordinates[n];
for(int count_1=0;count_1<n;count_1++)
{
pts[count_1].x=(ppts[(count_1*2)]);
pts[count_1].y=(ppts[((count_1*2)+1)]);
}
for(int count_2=0;count_2<640;count_2++)
Page | 27

{
edges[count_2]=new Edge;
edges[count_2]->next=NULL;
}
buildEdgeList(n,pts,edges);
active=new Edge;
active->next=NULL;
for(int count_3=0;count_3<480;count_3++)
{
buildActiveList(count_3,active,edges);
if(active->next)
{
fillScan(count_3,active,fill_color);
updateActiveList(count_3,active);
resortActiveList(active);
}
}
Polygon(n,ppts);
delete pts;
}
const int yNext(const int k,const int cnt,const PointCoordinates pts[])
{
int j;
if((k+1)>(cnt-1))
j=0;
else
j=(k+1);
while(pts[k].y==pts[j].y)
{
if((j+1)>(cnt-1))
j=0;
else
j++;
}
return (pts[j].y);
}
void insertEdge(Edge *list,Edge *edge)
{
Edge *p;
Edge *q=list;
p=q->next;
while(p!=NULL)
{
if(edge->xIntersect<p->xIntersect)
p=NULL;
else
{
q=p;
p=p->next;
}

Page | 28

edge->next=q->next;
q->next=edge;
}
void makeEdgeRec(const PointCoordinates lower,const PointCoordinates upper,
const int yComp,Edge *edge,Edge *edges[])
{
edge->dxPerScan=((upper.x-lower.x)/(upper.y-lower.y));
edge->xIntersect=lower.x;
if(upper.y<yComp)
edge->yUpper=(upper.y-1);
else
edge->yUpper=upper.y;
insertEdge(edges[lower.y],edge);
}
void buildEdgeList(const int cnt,const PointCoordinates pts[],Edge *edges[])
{
Edge *edge;
PointCoordinates v1;
PointCoordinates v2;
int yPrev=(pts[cnt-2].y);
v1.x=pts[cnt-1].x;
v1.y=pts[cnt-1].y;
for(int count=0;count<cnt;count++)
{
v2=pts[count];
if(v1.y!=v2.y)
{
edge=new Edge;
if(v1.y<v2.y)
makeEdgeRec(v1,v2,yNext(count,cnt,pts),edge,edges);
else
makeEdgeRec(v2,v1,yPrev,edge,edges);
}
yPrev=v1.y;
v1=v2;
}
}
void buildActiveList(const int scan,Edge *active,Edge *edges[])
{
Edge *p;
Edge *q;
p=edges[scan]->next;
while(p)
{
q=p->next;
insertEdge(active,p);
p=q;
}
}
void fillScan(const int scan,const Edge *active,const int fill_color)
{
Page | 29

Edge *p1;
Edge *p2;
p1=active->next;
while(p1)
{
p2=p1->next;
for(int count=p1->xIntersect;count<=p2->xIntersect;count++)
putpixel(count,scan,fill_color);
p1=p2->next;
}
}
void deleteAfter(Edge * q)
{
Edge *p=q->next;
q->next=p->next;
delete p;
}
void updateActiveList(const int scan,Edge *active)
{
Edge *q=active;
Edge *p=active->next;
while(p)
{
if(scan>=p->yUpper)
{
p=p->next;
deleteAfter(q);
else

}
{

p->xIntersect=(p->xIntersect+p->dxPerScan);
q=p;
p=p->next;

}
void resortActiveList(Edge *active)
{
Edge *q;
Edge *p=active->next;
active->next=NULL;
while(p)
{
q=p->next;
insertEdge(active,p);
p=q;
}
Page | 30

}
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
Page | 31

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:

Page | 32

Page | 33

10. Write a program to perform translation of two dimensional objects.


Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h
void show_screen( );
void apply_translation(const int,int [],const int,const 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( );
int polygon_points[8]={ 270,290, 320,190, 370,290, 270,290 };
setcolor(15);
Polygon(4,polygon_points);
setcolor(15);
settextstyle(0,0,1);
outtextxy(50,415,"*** Use Arrow Keys to apply Translation.");
int key_code_1=0;
int key_code_2=0;
char Key_1=NULL;
char Key_2=NULL;
do
{
Key_1=NULL;
Key_2=NULL;
key_code_1=0;
key_code_2=0;
Key_1=getch( );
key_code_1=int(Key_1);
if(key_code_1==0)
{
Key_2=getch( );
key_code_2=int(Key_2);
}
if(key_code_1==27)
break;
else if(key_code_1==0)
{
if(key_code_2==72)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(4,polygon_points,0,-25);
setcolor(10);
Page | 34

Polygon(4,polygon_points);
}
else if(key_code_2==75)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(4,polygon_points,-25,0);
setcolor(12);
Polygon(4,polygon_points);
}
else if(key_code_2==77)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(4,polygon_points,25,0);
setcolor(14);
Polygon(4,polygon_points);
}
else if(key_code_2==80)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(4,polygon_points,0,25);
setcolor(9);
Polygon(4,polygon_points);
}
}
while(1);
return 0;

}
void apply_translation(const int n,int coordinates[],const int Tx,const int Ty)
{
for(int count_1=0;count_1<n;count_1++)
{
int matrix_a[3]={coordinates[(count_1*2)], coordinates[((count_1*2)+1)],1};
int matrix_b[3][3]={ {1,0,0} , {0,1,0} ,{ Tx,Ty,1} };
int matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count_1*2)]=matrix_c[0];
coordinates[((count_1*2)+1)]=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]);
}
}
Page | 35

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);
Page | 36

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.");
}

Page | 37

Output:

After Translation

Page | 38

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)
{
Page | 39

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)
{
Page | 40

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

}
Page | 41

}
void show_screen( )
{
setfillstyle(1,1);
bar(170,26,460,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(178,29,"Fixed-Point Scaling 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.");
}
Output:

Page | 42

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)
{
Page | 43

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;
Page | 44

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

Page | 45

putpixel(x,y,color);

}
}
void show_screen( )
{
setfillstyle(1,1);
bar(140,26,485,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(150,29,"Rotation Transformation along Fix Point");
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:

Page | 46

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);
Page | 47

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]);
Page | 48

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);
}
}
Page | 49

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:

Page | 50

Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void apply_reflection_about_line_yex(const int,int []);
void apply_reflection_about_line_yemx(const int,int []);
void apply_reflection_along_x_axis(const int,int []);
void apply_reflection_along_y_axis(const int,int []);
void apply_rotation(const int,int [],float);
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);
void Dashed_line(const int,const int,const int,const int,const int=0);
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);
Dashed_line(160,400,460,100,0);
Dashed_line(180,100,480,400,0);
settextstyle(2,0,4);
outtextxy(305,85,"y-axis");
Page | 51

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++)
Page | 52

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;
Page | 53

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,
Page | 54

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

}
Page | 55

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;
}
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++;

}
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)),"");
Page | 56

outtextxy(5,438,"");
outtextxy(5,450,"");
outtextxy(5,462,"");
setcolor(12);
outtextxy(229,450,"Press any Key to exit.");
}
Output:

Page | 57

14. Write a program to perform shearing transformation in X direction.


Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void apply_x_direction_shear(const int,int [],const float);
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,340, 270,140, 370,140, 370,340, 270,340 };
setcolor(15);
Polygon(5,polygon_points);
setcolor(15);
settextstyle(0,0,1);
outtextxy(50,415,"*** Use Left and Right Arrow Keys to apply X-Direction Shear.");
int key_code_1=0;
int key_code_2=0;
char Key_1=NULL;
char Key_2=NULL;
do
{
Key_1=NULL;
Key_2=NULL;
key_code_1=0;
key_code_2=0;
Key_1=getch( );
key_code_1=int(Key_1);
if(key_code_1==0)
{
Key_2=getch( );
key_code_2=int(Key_2);
}
if(key_code_1==27)
break;
else if(key_code_1==0)
{
if(key_code_2==75)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_x_direction_shear(5,polygon_points,-0.1);

Page | 58

setcolor(12);
Polygon(5,polygon_points);

}
while(1);
return 0;

}
else if(key_code_2==77)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_x_direction_shear(5,polygon_points,0.1);
setcolor(10);
Polygon(5,polygon_points);
}

}
void apply_x_direction_shear(const int n,int coordinates[],const float Sh_x)
{
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} , {Sh_x,1,0} ,{ 0,0,1} };
float matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count*2)]=(matrix_c[0]+0.5);
coordinates[((count*2)+1)]=(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( );

Page | 59

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
{
Page | 60

x++;
p+=two_dx_dy;

}
putpixel(x,y,color);

}
void show_screen( )
{
setfillstyle(1,1);
bar(184,26,460,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(194,29,"X-Direction Shear 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.");
}
Output:

Page | 61

15. Write a program to perform two successive translations.


Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void apply_translation(const int,int [],const int,const 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( );
int polygon_points[8]={ 270,290, 320,190, 370,290, 270,290 };
setcolor(15);
Polygon(4,polygon_points);
setcolor(15);
settextstyle(0,0,1);
outtextxy(50,415,"*** Use Arrow Keys to apply Translation.");
int key_code_1=0;
int key_code_2=0;
char Key_1=NULL;
char Key_2=NULL;
do
{
Key_1=NULL;
Key_2=NULL;
key_code_1=0;
key_code_2=0;
Key_1=getch( );
key_code_1=int(Key_1);
if(key_code_1==0)
{
Key_2=getch( );
key_code_2=int(Key_2);
}
if(key_code_1==27)
break;
else if(key_code_1==0)
{
if(key_code_2==72)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(4,polygon_points,0,-25);
Page | 62

setcolor(10);
Polygon(4,polygon_points);

}
while(1);
return 0;

}
else if(key_code_2==75)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(4,polygon_points,-25,0);
setcolor(12);
Polygon(4,polygon_points);
}
else if(key_code_2==77)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(4,polygon_points,25,0);
setcolor(14);
Polygon(4,polygon_points);
}
else if(key_code_2==80)
{
setfillstyle(1,0);
bar(40,70,600,410);
apply_translation(4,polygon_points,0,25);
setcolor(9);
Polygon(4,polygon_points);
}

}
void apply_translation(const int n,int coordinates[], const int Tx,const int Ty)
{
for(int count_1=0;count_1<n;count_1++)
{
int matrix_a[3]={coordinates[(count_1*2)],coordinates[((count_1*2)+1)],1};
int matrix_b[3][3]={ {1,0,0} , {0,1,0} ,{ Tx,Ty,1} };
int matrix_c[3]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
coordinates[(count_1*2)]=matrix_c[0];
coordinates[((count_1*2)+1)]=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]);
Page | 63

}
}
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);
}
}

Page | 64

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.");
}

Page | 65

Output:

Page | 66

16. Write a program to perform two successive Rotations.


Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void apply_rotation(const int,int [],float);
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,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_rotation(4,polygon_points,5);
setcolor(10);
Polygon(4,polygon_points);
}
else if(key_code==45)
{
setfillstyle(1,0);
Page | 67

}
while(1);
return 0;

bar(40,70,600,410);
apply_rotation(4,polygon_points,-5);
setcolor(12);
Polygon(4,polygon_points);

}
void apply_rotation(const int n,int coordinates[],float angle)
{
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
matrix_b[3][3]={ { cos(angle),sin(angle),0 } ,{ -sin(angle),cos(angle),0 } ,{ 0,0,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)
{
x1=x_2;
Page | 68

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);
}
}

}
void show_screen( )

Page | 69

setfillstyle(1,1);
bar(212,26,412,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(222,29,"______ Rotation ______ ");
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:

Page | 70

17. Write a program to perform two successive scaling.


Program:
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void apply_scaling(const int,int [],const float,const float);
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,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_scaling(5,polygon_points,1.1,1.1);
setcolor(10);
Polygon(5,polygon_points);
}
else if(key_code==45)
{
setfillstyle(1,0);
bar(40,70,600,410);
Page | 71

}
while(1);
return 0;

apply_scaling(5,polygon_points,0.9,0.9);
setcolor(12);
Polygon(5,polygon_points);

}
void apply_scaling(const int n,int coordinates[],const float Sx,const float Sy)
{
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} ,{ 0,0,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)
{
x1=x_2;
y1=y_2;

Page | 72

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 show_screen( )
{

Page | 73

setfillstyle(1,1);
bar(218,26,413,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(226,29,"Scaling 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.");

Output:

Page | 74

Das könnte Ihnen auch gefallen