This file contains all the practicals with output regarding GTU syllabus. so it will help to IT and Computer engineering students. It is really knowledgeable so refer these for computer graphics practicals.
1. Shri S’ad Vidya Mandal Institute of Technology
Department of Computer Science Engineering and Information Technology
(B.E. Third Year – Sem – V) Branch : Information Technology
SUBJECT :- COMPUTER GRAPHICS (2151603)
NAME:- Jainam Kapadiya
ENROLLMENT NO: 150450116015
INDEX
SR. NO. PRACTICAL TITLE DATE SIGN
1. Study of various graphics functions in C language and
use them to create any graphics object
2. Develop the DDA Line drawing algorithm using C
language
3. Develop the Bresenham’s Line drawing algorithm using
C language
4. Develop the Bresenham’s Circle drawing algorithm
using C language
5. Develop the C program for to display different types of
lines
6. Perform the following 2D Transformation operation
Translation , Rotation and Scaling
7. Perform the Line Clipping Algorithm
8. Perform the Polygon clipping algorithm
9. Perform the following tasks using MATLAB commands.
- Read the grayscale and color image.
- Display images on the computer monitor
- Write images in your destination folder.
10. Generate the complement image using MATLAB.
2. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 1
PRACTICAL – 1
1. Study of various graphics functions in C language and use them to create any
graphics object
In C graphics programming you have to use standard library functions , every graphics program should
include "graphics.h" header file.
#include<graphics.h>
Graphics Mode Initialization :- In a C program, first step is to initialize the graphics drivers on the
computer. This is done using the initgraph method provided in graphics.h library The initgraph function
that will initialize the graphics mode on the computer. initigraph has the following prototype.
void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);
int gd=DETECT,gm;
initgraph(&gd,&gm,"f:tcbgi");
initgraph:- initializes the graphics system by loading the graphics driver from disk (or validating a
REGISTERED driver) then putting the system into graphics mode. initgraph also resets all graphics
settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.
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
3. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 2
*graphdriver:- the integer that specifies the graphics driver to be used. We can give graphdriver a value
using a constant of the graphics_drivers enumeration type which is listed in graphics.h. Normally we use
value as “0” (requests auto-detect). Other values are 1 to 10 and description of each enumeration type is
listed here.
*graphmode:- the integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If
*graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected
driver.
graphdriver and graphmode must be set to valid values from the following tables, or you will get
unpredictable results. The exception is graphdriver = DETECT.
*pathtodriver:- inorder to specify the directory path where initgraph looks for graphics drivers (*.BGI)
first.
(1) If they’re not there, initgraph looks in the current directory.
(2) If pathtodriver is null, the driver files must be in the current directory.
*graphdriver and *graphmode must be set to valid graphics_drivers and graphics_mode values or we
will get unpredictable results. (The exception is graphdriver = DETECT.)
After a call to initgraph, *graphdriver is set to the current graphics driver, and *graphmode is set to the
current graphics mode. We can tell initgraph to use a particular graphics driver and mode, or to auto
detect the attached video adapter at run time and pick the corresponding driver. If we tell initgraph to
autodetect, it calls detectgraph to select a graphics driver and mode.
Normally, initgraph loads a graphics driver by allocating memory for the driver (through
_graphgetmem), then loading the appropriate .BGI file from disk. This dynamic loading scheme, you
can link a graphics driver file (or several of them) directly into your executable program file.
closegraph:- deallocates all memory allocated by the graphics system, then restores the screen to the
mode it was in before you called initgraph. (The graphics system deallocates memory, such as the
drivers, fonts, and an internal buffer, through a call to _graphfreemem.)
Below are mentioned various graphics functions with description, syntax and sample code of
same:
4. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 3
1. getmaxx function
Description: - getmaxx function returns the maximum X coordinate for current graphics
mode and driver.
Declaration: - int getmaxx();
Program :- #include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm, max_x;
char array[100];
initgraph(&gd,&gm,"C:TCBGI");
max_x = getmaxx();
printf(array, "Maximum X coordinate for current graphics mode and driver
= %d.",max_x);
outtext(array);
getch();
}
2. getmaxy function
Description: - getmaxy function returns the maximum Y coordinate for current graphics
mode and driver.
Declaration: - int getmaxy();
Program :- #include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm, max_y;
char array[100];
initgraph(&gd,&gm,"C:TCBGI");
max_y = getmaxy();
printf(array, "Maximum Y coordinate for current graphics mode & driver is
= %d.",max_y);
outtext(array);
getch();
}
5. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 4
3. setcolor function
Description: - In Turbo Graphics each color is assigned a number. Total 16 colors are
available. Strictly speaking number of available colors depends on current
graphics mode and driver.
For Example: - BLACK is assigned 0, RED is assigned 4 etc.
setcolor function is used to change the current drawing color.
e.g. setcolor(RED) or setcolor(4) changes the current drawing color to RED.
Remember that default drawing color is WHITE.
Declaration: - void setcolor(int color);
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main ()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:TCBGI");
circle (100,100,50);
setcolor(RED);
circle(200,200,50);
getch();
closegraph();
}
4.
getcolor function
Description: - getcolor function returns the current drawing color.
E.g. a = getcolor(); // a is an integer variable if current drawing color is WHITE
then a will be 15.
Declaration:- int getcolor();
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm, drawing_color;
char a[100];
initgraph(&gd,&gm,"C:TCBGI");
drawing_color = getcolor();
printf(a,"Current drawing color = %d", drawing_color);
6. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 5
outtextxy( 10, 10, a );
getch();
}
5. outtextxy function
Description: - outtextxy function display text or string at a specified point(x,y) on the screen.
Declaration:- void outtextxy(int x, int y, char *string);
x, y are coordinates of the point and third argument contains the address of
string to be displayed.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:TCBGI");
outtextxy(100, 100, "WELCOME- TO COMPUTER GRAPHICS LAB");
getch();
}
6. getpixel function
Description: - int getpixel(int x, int y);
Declaration: - getpixel function returns the color of pixel present at location(x, y).
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm, color;
char array[50];
initgraph(&gd,&gm,"C:TCBGI");
color = getpixel(0, 0);
printf(array,"color of pixel at (0,0) = %d",color);
outtext(array);
getch();
}
7. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 6
7. putpixel function
Description: - putpixel function plots a pixel at location (x, y) of specified color.
Declaration: - void putpixel(int x, int y, int color);
For example if we want to draw a GREEN color pixel at (35, 45) then we will
write putpixel(35, 35, GREEN); in our c program, putpixel function can be
used to draw circles, lines and ellipses using various algorithms.
Program:- #include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
putpixel(25, 25, RED);
getch(); }
8. circle function
Description: -
circle function is used to draw a circle with center (x,y) and third parameter
specifies the radius of the circle. The code given below draws a circle.
Declaration: - void circle(int x, int y, int radius);
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
circle(100, 100, 50);
getch();
}
9. line function
Description :- line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e.
(x1,y1) and (x2,y2) are end points of the line.The code given below draws a
line.
Declaration :- void line(int x1, int y1, int x2, int y2);
#include<stdio.h>
#include <graphics.h>
8. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 7
#include <conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
line(100, 100, 200, 200);
getch();
}
10.
rectangle function
Description: - rectangle function is used to draw a rectangle. Coordinates of left top and
right bottom corner are required to draw the rectangle. left specifies the X-
coordinate of top left corner, top specifies the Y-coordinate of top left corner,
right specifies the X-coordinate of right bottom corner, bottom specifies the Y-
coordinate of right bottom corner.
Declaration: - void rectangle(int left, int top, int right, int bottom);
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
rectangle(100,100,200,200);
getch();
}
11.
ellipse function
Description: - Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse,
stangle is the starting angle, end angle is the ending angle, and fifth and sixth
parameters specifies the X and Y radius of the ellipse. To draw a complete
ellipse strangles and end angle should be 0 and 360 respectively.
Declarations:- void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
9. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 8
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
ellipse(100, 100, 0, 360, 50, 25);
getch();
}
12. getx function
Description: - getx function returns the X coordinate of current position.
Declaration: int getx();
#include<stdio.h>
#include <graphics.h>
#include <conio.h>
void main()
{
int gd = DETECT, gm;
char array[100];
initgraph(&gd, &gm, "C:TCBGI");
printf(array, "Current position of x = %d",getx());
outtext(array);
getch();
}
13. gety function
Description: - gety function returns the y coordinate of current position.
Declaration: - int gety();
#include<stdio.h>
#include <graphics.h>
#include <conio.h>
void main()
{
int gd = DETECT, gm, y;
char array[100];
initgraph(&gd, &gm, "C:TCBGI");
y = gety();
printf(array, "Current position of y = %d", y);
outtext(array);
getch();
}
10. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 9
14. getmaxcolor function
Description: - getmaxcolor function returns maximum color value for current graphics
mode and driver. Total number of colors available for current graphics mode
and driver are ( getmaxcolor() + 1 ) as color numbering starts from zero.
Declaration: int getmaxcolor();
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm, max_colors;
char a[100];
initgraph(&gd,&gm,"C:TCBGI");
max_colors = getmaxcolor();
printf(a,"Maximum number of colors for current graphics mode and driver
= %d",max_colors+1);
outtextxy(0, 40, a);
getch();
}
15. setfillstyle function
Description:- setfillstyle function sets the current fill pattern and fill color.
Different fill styles:
enum fill_style
{EMPTY_FILL,SOLID_FILL,LINE_FILL,LTSLASH_FILL,SLASH_FILL,KSLAS
H_FILL,
LTBKSLASH_FILL,HATCH_FILL,XHATCH_FILL,INTERLEAVE_FILL,WIDE
_DOT_FILL, CLOSE_DOT_FILL, USER_FILL };
Declaration :- void setfillstyle( int pattern, int color);
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:TCBGI");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
11. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 10
floodfill(100, 100, WHITE);
getch();
}
16. Setlinestyle
Description:- Available line styles:
enum line_styles
{SOLID_LINE,DOTTED_LINE,CENTER_LINE,
DASHED_LINE, USERBIT_LINE }
Declaration: void setlinestyle( int linestyle, unsigned upattern, int thickness );
#include<stdio.h>
#incude<conio.h>
#include <graphics.h>
void main()
{
int gd = DETECT, gm, c , x = 100, y = 50;
initgraph(&gd, &gm, "C:TCBGI");
for ( c = 0 ; c < 5 ; c++ )
{
setlinestyle(c, 0, 2);
line(x, y, x+200, y);
y = y + 25;
}
getch();
}
17. setbkcolor function
Description:- setbkcolor function changes current background color e.g.
setbkcolor(YELLLOW) changes the current background color to YELLOW.
Remember that default drawing color is WHITE and background color is
BLACK.
Declaration: - void setbkcolor(int color);
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
12. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 11
initgraph(&gd, &gm, "C:TCBGI");
outtext("Press any key to change the background color to GREEN.");
getch();
setbkcolor(GREEN);
getch();
}
18. Setfillpattern
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.
Declaration:- void setfillpattern(char *upattern, int color);
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00};
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
getch();
exit(1);
maxx = getmaxx();
maxy = getmaxy();
setcolor(getmaxcolor());
setfillpattern(pattern, getmaxcolor());
bar(0, 0, maxx, maxy);
getch();
}
19. Getfillpattern
Description: - getfillpattern copies the user-defined fill pattern, as set by setfillpattern, into
the 8-byte area pointed to by pattern.pattern is a pointer to a sequence of 8
bytes, with each byte corresponding to 8 pixels in the pattern. Whenever a bit
13. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 12
in a pattern byte is set to 1, the corresponding pixel will be plotted.
For example, the following user-defined fill pattern represents a checkerboard:
char checkboard[8] = { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
Declaration:- void getfillpattern(char *pattern);
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x25, 0x27, 0x04, 0x04};
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk) {
printf("Graphics error: %sn", grapherrormsg(errorcode)); printf("Press any
key to halt:");
getch();
exit(1);
}
maxx = getmaxx();
maxy = getmaxy();
setcolor(getmaxcolor());
setfillpattern(pattern, getmaxcolor());
bar(0, 0, maxx, maxy); getch();
getfillpattern(pattern);
pattern[4] -= 1; pattern[5] -= 3; pattern[6] += 3; pattern[7] -= 4;
setfillpattern(pattern, getmaxcolor());
bar(0, 0, maxx, maxy);
getch();
}
20. gotoxy function
Description:- gotoxy in c: gotoxy function places cursor at a desired location on screen i.e.
we can change cursor position using gotoxy function.
Declaration:- void gotoxy(int x,int y)
14. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 13
where (x, y) is the position where we want to place the cursor.
#include <stdio.h>
#include <conio.h>
void main()
{
int x, y;
x = 10;
y = 10;
gotoxy(x, y);
printf("C program to change cursor position.");
getch();
}
Below is a C program that includes the usage of above graphics function with its output :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int choice;
int gd=DETECT,gm;
int style;
char *fname[] = {"SOLID_FILL"};
initgraph(&gd,&gm,"c:tcbgi");
while(1)
{
printf("enter the choice which you want:");
printf("n 1)circle");
printf("n 2)rectangle");
printf("n 3)ellipse");
printf("n 4)line");
printf("n 5)point");
printf("n 6)exit");
15. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 14
scanf("%d",&choice);
clrscr();
cleardevice();
switch(choice)
{
case 1:
for(style = SOLID_FILL; style < USER_FILL; style++)
setfillstyle(SOLID_FILL, getmaxcolor());
setbkcolor(5);
setcolor(11);
circle(40,50,20);
floodfill(50,60,11);
getch();
break;
case 2:setbkcolor(6);
setcolor(12);
rectangle(100,80,200,140);
getch();
break;
case 3:setbkcolor(8);
setcolor(13);
ellipse(80,100,45,405,10,20);
getch();
break;
case 4: setbkcolor(12);
line(160,350,250,390);
getch();
16. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 15
break;
case 5:setbkcolor(2);
putpixel(40,50,20);
getch();
break;
case 6:exit();
getch();
break;
}
getch();
}
}
Output:-
17. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 16
18. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 17
19. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 18
PRACTICAL – 2
2. Develop the DDA Line drawing algorithm using C language.
DDA ALGORITHM:-
Pseudo Code Of DDA Algorithm
Procedure lineDDA(xa,ya,xb,yb:integer);
var
dx,dy,steps,k:integer
xIncrement, yIncrement, x, y:real;
begin
dx:=xb-xa;
dy:=yb-ya;
if abs(dx)>abs(dy) then steps:=abs(dx)
else steps:=abs(dy);
xIncrement:=dx/steps;
yIncrement:=dy/steps;
x:=xa;
y:=ya;
setPixel(round(x),round(y),1);
for k:=1 to steps do
begin
x:=x+xIncrement;
y:=y+yIncrement;
setPixel(round(x),round(y),1)
end
end
20. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 19
Below is a C program of DDA line drawing algorithm with output:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void main()
{
int i,gd,gm;
float x,y,x1,y1,x2,y2,x_incr,y_incr,length,dx,dy,m;
printf("enter the value of x1:");
scanf("%f",&x1);
printf("n enter the value of y1:");
scanf("%f",&y1);
printf("n enter the value of x2:");
scanf("%f",&x2);
printf("n enter the value of y2:");
scanf("%f",&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm," ");
setbkcolor(7);
dx=abs(x2-x1);
dy=abs(y2-y1);
m=dy/dx;
21. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 20
printf("Slope=%f",m);
if(dx>=dy)
{
length=dx;
}
else
{
length=dy;
}
x_incr=abs(x2-x1)/length;
y_incr=abs(y2-y1)/length;
x=x1;
y=y1;
putpixel(x,y,12);
i=1;
while(i<=length)
{
x=x+x_incr;
y=y+y_incr;
putpixel(x,y,12);
printf("n %f %f",x,y);
i=i+1;
delay(50);
}
22. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 21
getch();
closegraph();
}
Output:-
23. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 22
PRACTICAL – 3
3. Develop the Bresenham’s Line drawing algorithm using C language.
The Bresenham’s Line drawing Algorithm
(For |m| < 1.0)
1. Input the two line end-points, storing the left end-point in (x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the
decision parameter as:
4. At each xk along the line, starting at k = 0, perform the following test. If pk < 0, the next
point to plot is (xk+1, yk) and:
5. Otherwise, the next point to plot is (xk+1, yk+1) and:
6. Repeat step 4 (Δx – 1) times.
Below is a C program of Bresenham’s line drawing algorithm with output:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2,x,y,xend;
int dx,dy,twody,twodydx,p;
xyp 20
ypp kk 21
xypp kk 221
24. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 23
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"");
printf("n Enter the value of x1 and y1:");
scanf("%d %d",&x1,&y1);
outtextxy(x1,y1,"(x1,y1)");
printf("n Enter the value of x2 and y2:");
scanf("%d %d",&x2,&y2);
outtextxy(x2,y2,"(x2,y2)");
setbkcolor(0);
dx=(x2-x1);
dy=(y2-y1);
twody=2*dy;
twodydx=2*(dy-dx);
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
25. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 24
y=y1;
xend=x2;
}
printf("np=%d,t x=%d,t y=%d",p,x,y);
putpixel(x,y,5);
while(x < xend)
{
x++;
if(p<0)
p+=twody;
else
{
y++;
p+=twodydx;
}
printf("np=%d,t x=%d,t y=%d",p,x,y);
putpixel(x,y,5);
}
getch();
closegraph();
}
26. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 25
Output:-
27. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 26
PRACTICAL – 4
4. Develop the Bresenham’s Circle drawing algorithm using C language.
The Bresenham’s circle drawing Algorithm
1. Initial values:- point(0,r)
x0 = 0
y0 = r
move circle origin at (0,0) by
x = x – xc and y = y – yc
2. Initial decision parameter
3. At each xi position, starting at i = 0, perform the following test: if pi < 0, the next point is (xi
+ 1, yi) and
pi+1 = pi + 2xi+1 + 1
At each xi position, starting at i = 0, perform the following test: if pi < 0, the next point is
(xi + 1, yi) and
pi+1 = pi + 2xi+1 + 1
4. Determine symmetry points in the other octants
5. Move pixel positions (x,y) onto the circular path centered on (xc, yc) and plot the
coordinates: x = x + xc, y = y + yc
6. Repeat 3 – 5 until x ≥ y
Below is a C program of Bresenham’s circle drawing algorithm with output:-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
2 2 51 1
0 2 2 4(1, ) 1 ( )circlep f r r r r
28. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 27
void circleplotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,5);
putpixel(xc-x,yc+y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+y,yc+x,5);
putpixel(xc-y,yc+x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,5);
}
void main()
{
int xc,yc,r,p,x,y;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"");
printf("n Enter the value of circle center x and y:");
scanf("%d %d",&xc,&yc);
outtextxy(xc,yc,"(x,y)");
printf("n Enter the value of circle radius:");
scanf("%d",&r);
setbkcolor(0);
x=0;
y=r;
29. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 28
p=1-r;
circleplotpoints(xc,yc,x,y);
printf("nx=%d,t y=%d",x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
circleplotpoints(xc,yc,x,y);
printf("nx=%d,t y=%d",x,y);
}
getch();
}
Output:-
30. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 29
31. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 30
PRACTICAL – 5
5. Develop the C program for to display different types of lines.
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm,c;
int x1,x2,y1,y2;
initgraph(&gd,&gm, "");
setbkcolor(0);
while(1)
{
printf("n1.SOLID_LINEn2.DOTTED_LINEn3.CENTER_LINEn");
printf("n4.DASHED_LINEn5.USERBIT_LINEn6.Exit");
printf("nEnter Your Choice:");
scanf("%d",&c);
clrscr();
cleardevice();
if(c<6)
{
printf("nEnter starting point (x1 ,y1):");
scanf("%d %d",&x1,&y1);
printf("nEnter End point (x2,y2):");
scanf("%d %d",&x2,&y2);
32. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 31
}
switch(c)
{
case 1:
setlinestyle(SOLID_LINE,1,1);
setcolor(15);
line(x1,y1,x2,y2);
getch();
cleardevice();
break;
case 2:
setlinestyle(DOTTED_LINE,1,1);
setcolor(15);
line(x1,y1,x2,y2);
getch();
cleardevice();
break;
case 3:
setlinestyle(CENTER_LINE,1,1);
setcolor(15);
line(x1,y1,x2,y2);
getch();
cleardevice();
break;
33. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 32
case 4:
setlinestyle(DASHED_LINE,1,1);
setcolor(15);
line(x1,y1,x2,y2);
getch();
cleardevice();
break;
case 5:
setlinestyle(USERBIT_LINE,1,1);
setcolor(15);
line(x1,y1,x2,y2);
getch();
cleardevice();
break;
case 6:
exit(1);
break;
default:
printf("!Enter the correct choice!");
}
clrscr();
cleardevice();
34. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 33
getch();
}
}
Output:-
(Choice 1)
(Choice 2) (Choice 3)
)
(Choice 4) (Choice 5)
35. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 34
PRACTICAL – 6
6. Perform the following 2D Transformation operation Translation , Rotation
and Scaling.
ALGORITHM:
1. Start
2. Initialize the graphics mode.
3. Construct a 2D object (use Drawpoly()) e.g. (x,y)
4. A) Translation
– a. Get the translation value tx, ty
– b. Move the 2d object with tx, ty (x’=x+tx,y’=y+ty)
– c. Plot (x’,y’)
5. B) Scaling
– a. Get the scaling value Sx,Sy
– b. Resize the object with Sx,Sy (x’=x*Sx,y’=y*Sy)
– c. Plot (x’,y’)
6. C) Rotation
– a. Get the Rotation angle
– b. Rotate the object by the angle ф
x’=x cos ф - y sin ф
y’=x sin ф - y cosф
– c. Plot (x’,y’)
Below is a C program of 2D Transformation operation Translation , Rotation and Scaling
with output:-
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
36. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 35
#include<conio.h>
#include<math.h>
void translation();
void rotation();
void scalling();
void triangle();
void quadrant();
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,c;
initgraph(&gd,&gm,"");
printf("nEnter the point of triangle (x1,y1):");
scanf("%d %d",&x1,&y1);
printf("nEnter the point of triangle (x2,y2):");
scanf("%d %d",&x2,&y2);
printf("nEnter the point of triangle (x3,y3):");
scanf("%d %d",&x3,&y3);
clrscr();
cleardevice();
setbkcolor(0);
triangle(x1,y1,x2,y2,x3,y3);
while(1)
{
printf("n 1.Transactionn 2.Rotationn 3.Scallingn 4.exit n");
37. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 36
printf("nEnter your choice:");
scanf("%d",&c);
clrscr();
cleardevice();
quadrant();
switch(c)
{
case 1:
triangle(x1,y1,x2,y2,x3,y3);
translation(x1,y1,x2,y2,x3,y3);
getch();
break;
case 2:
triangle(x1,y1,x2,y2,x3,y3);
rotation(x1,y1,x2,y2,x3,y3);
getch();
break;
case 3:
triangle(x1,y1,x2,y2,x3,y3);
scalling(x1,y1,x2,y2,x3,y3);
getch();
break;
case 4:
exit(1);
break;
default:
38. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 37
printf("!Enter the correct choice!");
}
clrscr();
cleardevice();
getch();
}
}
void quadrant()
{
line(320,0,320,479);
line(0,240,639,240);
}
void triangle(int x1,int y1,int x2,int y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void translation(int x1,int y1,int x2,int y2,int x3,int y3)
{
int nx1,nx2,nx3,ny1,ny2,ny3;
int xt,yt;
printf("n Enter the translation factor xt & yt:");
scanf("%d %d",&xt,&yt);
cleardevice();
quadrant();
39. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 38
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
}
void rotation(int x1,int y1,int x2,int y2,int x3,int y3)
{
int nx1,nx2,nx3,ny1,ny2,ny3;
float t,r;
printf("n Enter the angle of rotation :");
scanf("%f",&r);
cleardevice();
quadrant();
t=(r*(3.14))/180;
nx1=x2+((x1-x2)*cos(t)-(y1-y2)*sin(t));
ny1=y2+((x1-x2)*sin(t)+(y1-y2)*cos(t));
nx2=x2+((x2-x2)*cos(t)-(y2-y2)*sin(t));
ny2=y2+((x2-x2)*sin(t)+(y2-y2)*cos(t));
nx3=x2+((x3-x2)*cos(t)-(y3-y2)*sin(t));
ny3=y2+((x3-x2)*sin(t)+(y3-y2)*cos(t));
40. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 39
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
}
void scalling(int x1,int y1,int x2,int y2,int x3,int y3)
{
int nx1,nx2,nx3,ny1,ny2,ny3;
float sx,sy;
printf("n Enter the scalling factor sx & sy:");
scanf("%f %f",&sx,&sy);
cleardevice();
quadrant();
nx1=x1*sx;
ny1=y1*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
}
41. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 40
Output:-
(Choice 1)
42. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 41
(After translation)
(Choice 2)
43. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 42
(After rotation)
(Choice 3)
44. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 43
(After Scaling)
45. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 44
PRACTICAL – 7
Perform the Line Clipping Algorithm.
#include"stdio.h"
#include"conio.h"
#include"graphics.h"
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"");
printf("nt Please enter the bottom left co-ordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("nt Please enter the top right co-ordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("nt Please enter the co-ordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("nt Please enter the co-ordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)
{
start[i]=0;
end[i]=0;
}
46. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 45
m=(y2-y1)/(x2-x1);
if(x1 <xmin) start[0]=1;
if(x1 >xmax) start[1]=1;
if(y1 >ymax) start[2]=1;
if(y1 <ymin) start[3]=1;
if(x2 <xmin) end[0]=1;
if(x2 >xmax) end[1]=1;
if(y2 >ymax) end[2]=1;
if(y2 <ymin) end[3]=1;
for(i=0;i <4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]==0
)&&(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("nttThe line is totally visiblenttand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
else
47. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 46
{
cleardevice();
printf("nttLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
48. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 47
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
clrscr();
cleardevice();
printf("nttAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
49. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 48
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}
Output:
50. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 49
51. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 50
PRACTICAL – 8
Perform the Polygon clipping algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void clip(float,float,float);
int i,j=0,n;
int rx1,rx2,ry1,ry2;
float x1[8],y1[8];
void main()
{
int gd=DETECT,gm;
int i,n;
float x[8],y[8],m;
clrscr();
initgraph(&gd,&gm,"");
printf("coordinates for rectangle : ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("no. of sides for polygon : ");
scanf("%d",&n);
printf("coordinates : ");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
cleardevice();
outtextxy(10,10,"Before clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
52. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 51
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
clip(x[i+1],y[i+1],m);
}
m=(y[i]-y[0])/(x[i]-x[0]);
clip(x[i],y[i],m);
clip(x[0],y[0],m);
outtextxy(10,10,"After clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
}
void clip(float e,float f,float m)
{
while(e<rx1 || e>rx2 || f<ry1 || f>ry2)
{
if(e<rx1)
{
f+=m*(rx1-e);
e=rx1;
}
53. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 52
else if(e>rx2)
{
f+=m*(rx2-e);
e=rx2;
}
if(f<ry1)
{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
{
e+=(ry2-f)/m;
f=ry2;
}
}
x1[j]=e;
y1[j]=f;
j++;
}
Output:
54. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 53
55. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 54
PRACTICAL – 9
Perform the following tasks using MATLAB commands.
Read the grayscale and color image.
i=imread('1.png');
subplot(1,2,1);
imshow(i);title('original');
j=rgb2gray(i);
subplot(1,2,2);
imshow(j);title('gray image');
Output:-
Display images on the computer monitor
i=imread('1.png');
subplot(2,2,1);
imshow(i);title('original');
Output:-
56. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 55
Write images in your destination folder.
i=imread('1.png');
subplot(1,2,1);
imshow(i);title('original');
imwrite(i,'copy1.jpeg');
Output:-
57. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 56
PRACTICAL – 10
Generate the complement image using MATLAB.
i=imread('1.png');
subplot(2,2,1);
imshow(i);title('original');
k=imcomplement(i);
subplot(2,2,2);
imshow(k);title('complement');
Output:-
58. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 57
Open Ended Problem
Simple paint brush application –part-1
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<alloc.h>
#define MIN_X 83
#define MAX_X 629
#define MIN_Y 35
#define MAX_Y 409
#define MAX_BUTTONS 27
#define MAX_COLPAT 28
int initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}
void showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
void hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
59. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 58
}
void startmouse(int x,int y)
{
i.x.ax=4;
i.x.cx=x;
i.x.dx=y;
int86(0x33,&i,&o);
}
void getxy()
{
union REGS regs;
regs.x.ax=3;
int86(0x33,®s,®s);
prevx=mousex;
prevy=mousey;
if(regs.x.bx&1)
LeftButtonPressed=1;
else
LeftButtonPressed=0;
if(regs.x.bx&2)
RightButtonPressed=1;
else
RightButtonPressed=0;
mousex=regs.x.cx;
mousey=regs.x.dx;
}
void cut()
{
int i,j,k=0;
ShowStatus("do not cut large areas");
if(marked)
60. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 59
{
unmark();
for(i=marker.top;i<marker.bottom;i++)
for(j=marker.left;j<marker.right;j++)
buffer1[k++]=getpixel(j,i);
hidemouseptr();
for(i=marker.left;i<marker.right;i++)
for(j=marker.top;j<marker.bottom;j++)
{
putpixel(i,j,WHITE);
}
showmouseptr();
}
}
void copy()
{
int i,j,k=0;
int height=marker.bottom-marker.top;
int length=marker.right-marker.left;
ShowStatus("do not copy large areas");
if(marked)
{
unmark();
buffer1[0]=length;
buffer1[1]=height;
for(i=marker.top;i<marker.bottom;i++)
for(j=marker.left;j<marker.right;j++)
buffer1[k++]=getpixel(j,i);
}
}
void paste(int x1,int y1)
61. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 60
{
int i,j,xt,yt,k=0;
int h=marker.bottom-marker.top;
int l=marker.right-marker.left;
hidemouseptr();
for(i=y1;i<y1+h;i++)
for(j=x1;j<x1+l;j++)
{
if(j>MIN_X && j<MAX_X && i>MIN_Y && i<MAX_Y)
putpixel(j,i,buffer1[k++]);
}
showmouseptr();
}
void callpaste()
{
delay(500);
LeftButtonPressed=0;
while(1)
{
getxy();
if(LeftButtonPressed)
{
if(mousex<MIN_X || mousex>MAX_X || mousey<MIN_Y || mousey>MAX_Y)
return ;
paste(mousex,mousey);
return ;
}
else if(RightButtonPressed)
return;
}
}
63. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 62
void flip()
{
int col;
int y1,y2;
int color1,color2;
for(col=MIN_X+2;col<=MAX_X-2;++col)
{
y1=MIN_Y+2;
y2=MAX_Y-2;
while(y1<y2)
{
color1=getpixel(col,y1);
color2=getpixel(col,y2);
putpixel(col,y2,color1);
putpixel(col,y1,color2);
y1++;
y2--;
}
}
}
void mirror()
{
int row;
int x1,x2;
int color1,color2;
for(row=MIN_Y+2;row<=MAX_Y-2;++row)
{
x1=MIN_X+2;
x2=MAX_X-2;
while(x1<x2)
{
65. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 64
line(MIN_X, MIN_Y, MIN_X, MAX_Y-1);
line(MIN_X+1, MIN_Y, MIN_X+1, MAX_Y-2);
setcolor(8);
line(MAX_X, MIN_Y+1, MAX_X, MAX_Y);
line(MAX_X-1, MIN_Y+2, MAX_X-1, MAX_Y-1);
line(MAX_X, MAX_Y, MIN_X, MAX_Y);
line(MAX_X, MAX_Y-1, MIN_X+1, MAX_Y-1);
}
void showeditmenu(int x1,int y1,int xwidth,int ywidth,int xtemp,int ytemp)
{
int area,button,x,y;
int fileflag=0;
int tempx=0;
int tempy=0;
char* buff1;
hidemouseptr();
area=imagesize(x1,y1+ywidth,x1+xwidth,y1+ywidth);
buff1=(char*)malloc(area);
getimage(x1,y1+ywidth,x1+xwidth,y1+ywidth*5,buff1);
setfillstyle(1,3);
bar(x1,y1+ywidth,x1+xwidth,y1+ywidth*5);
setcolor(RED);
rectangle(x1,y1+ywidth,x1+xwidth,y1+ywidth*5);
rectangle(x1,y1+ywidth,x1+xwidth,y1+2*ywidth);
outtextxy(x1+25,y1+ywidth+5,"cut");
rectangle(x1,y1+ywidth*2,x1+xwidth,y1+3*ywidth);
outtextxy(x1+25,y1+ywidth*2+5,"copy");
rectangle(x1,y1+ywidth*3,x1+xwidth,y1+4*ywidth);
outtextxy(x1+25,y1+ywidth*3+5,"paste");
rectangle(x1,y1+ywidth*4,x1+xwidth,y1+5*ywidth);
outtextxy(x1+25,y1+ywidth*4+5,"clear");
66. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 65
showmouseptr();
getmousepos(&button,&x,&y);
while(!(button==1 && (x!=xtemp || y!=ytemp)))
{
getmousepos(&button,&x,&y);
if(x>x1 && x<x1+xwidth && y>y1+ywidth && y<y1+5*ywidth )
{
if(y<y1+2*ywidth)
{
if(fileflag!=1)
{
ShowStatus("warning: do not cut large areas");
highlight(x1,y1+fileflag*ywidth,x1+xwidth,y1+(fileflag+1)*ywidth,BLUE,CYAN);
highlight(x1,y1+ywidth,x1+xwidth,y1+2*ywidth,CYAN,BLUE);
fileflag=1;
}
}
else if(y<y1+3*ywidth)
{
if(fileflag!=2)
{
ShowStatus("warning: do not copy large areas");
highlight(x1,y1+fileflag*ywidth,x1+xwidth,y1+(fileflag+1)*ywidth,BLUE,CYAN);
highlight(x1,y1+2*ywidth,x1+xwidth,y1+3*ywidth,CYAN,BLUE);
fileflag=2;
}
}
else if(y<y1+4*ywidth)
{
if(fileflag!=3)
{
67. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 66
ShowStatus("paste the cut/copied part of the image");
highlight(x1,y1+fileflag*ywidth,x1+xwidth,y1+(fileflag+1)*ywidth,BLUE,CYAN);
highlight(x1,y1+3*ywidth,x1+xwidth,y1+4*ywidth,CYAN,BLUE);
fileflag=3;
}
}
else if(y<y1+5*ywidth)
{
if(fileflag!=4)
{
ShowStatus("clear the canvas");
highlight(x1,y1+fileflag*ywidth,x1+xwidth,y1+(fileflag+1)*ywidth,BLUE,CYAN);
highlight(x1,y1+4*ywidth,x1+xwidth,y1+5*ywidth,CYAN,BLUE);
fileflag=4;
}
}
}
}hidemouseptr();
putimage(x1,y1+ywidth,buff1,COPY_PUT);
showmouseptr();
ShowStatus(" ");
if(!(x<x1||y<y1+ywidth||x>x1+xwidth||y>y1+ywidth*5))
{
switch(fileflag)
{
case 1:
cut();//call cut function
break;
case 2:
copy();//call copy function
break;
68. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 67
case 3:
callpaste();//call paste function
break;
case 4:
setfillstyle(SOLID_FILL,WHITE);
hidemouseptr();
bar(MIN_X,MIN_Y,MAX_X,MAX_Y); //call clear function
showmouseptr();
break;
}
showmouseptr();
}
free(buff1);
}
void showfilemenu(int x1,int y1,int xwidth,int ywidth,int xtemp,int ytemp)
{
int area,button,x,y;
int fileflag=0;
int tempx=0;
int tempy=0;
char* buff1;
hidemouseptr();
area=imagesize(x1,y1+ywidth,x1+xwidth,y1+ywidth);
buff1=(char*)malloc(area);
getimage(x1,y1+ywidth,x1+xwidth,y1+ywidth*5,buff1);
setfillstyle(1,3);
bar(x1,y1+ywidth,x1+xwidth,y1+ywidth*5);
setcolor(RED);
rectangle(x1,y1+ywidth,x1+xwidth,y1+ywidth*5);
rectangle(x1,y1+ywidth,x1+xwidth,y1+2*ywidth);
outtextxy(x1+25,y1+ywidth+5,"New");
75. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 74
hidemouseptr();
putimage(x1,y1+ywidth,buff1,COPY_PUT);
showmouseptr();
if(!(x<x1||y<y1+ywidth||x>x1+xwidth||y>y1+ywidth*3))
{
switch(fileflag)
{
case 1:
about();
break;
case 2:
topic();
break;
}
}
free(buff1);
}
void quit()
{
setcolor(RED);
end_about();
exit(0);
}
void topic()
{
int i,j;
int x1,y1,x2,y2;
int area=imagesize(x1,y1,x2,y2);
char*buffer=(char*)malloc(area);
hidemouseptr();
76. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 75
x1=(MAX_X+MIN_X)/2-100;
y1=(MIN_Y+MAX_Y)/2-38;
x2=(MAX_X+MIN_X)/2+100;
y2=(MIN_Y+MAX_Y)/2+38;
getimage(x1,y1,x2,y2,buffer);
ShowStatus("press any key to continue");
for(i=x1;i<x2;i++)
for(j=y1;j<y2;j++)
{
if(i>x1+10 && i<x2-10 && j>y1+4 && j<y2-6)
putpixel(i,j,GREEN);
else
putpixel(i,j,BLUE);
}
outtextxy(x1+26,y1+7,"Look at the bottom ");
outtextxy(x1+15,y1+20,"left corner for tips. ");
outtextxy(x1+13,y1+33,"For further help ,read ");
outtextxy(x1+12,y1+46,"the manual,a chm file ");
outtextxy(x1+12,y1+59,"given with this editor");
getch();
putimage(x1,y1,buffer,COPY_PUT);
showmouseptr();
free(buffer);
}
void about()
{
int i,j;
int x1,y1,x2,y2;
int area=imagesize(x1,y1,x2,y2);
char*buffer=(char*)malloc(area);
hidemouseptr();
77. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 76
x1=(MAX_X+MIN_X)/2-100;
y1=(MIN_Y+MAX_Y)/2-38;
x2=(MAX_X+MIN_X)/2+100;
y2=(MIN_Y+MAX_Y)/2+38;
getimage(x1,y1,x2,y2,buffer);
ShowStatus("press any key to continue");
for(i=x1;i<x2;i++)
for(j=y1;j<y2;j++)
{
if(i>x1+8 && i<x2-8 && j>y1+3 && j<y2-8)
putpixel(i,j,GREEN);
else
putpixel(i,j,BLUE);
}
outtextxy(x1+12,y1+7,"Coded and Developed by");
outtextxy(x1+30,y1+20,"Awadh kishor singh");
outtextxy(x1+13,y1+33,"");
outtextxy(x1+13,y1+46,"3rd yr IT Engg");
outtextxy(x1+35,y1+59,"");
getch();
putimage(x1,y1,buffer,COPY_PUT);
showmouseptr();
free(buffer);
}
void end_about()
{
int i,j;
int x1,y1,x2,y2;
hidemouseptr();
x1=(MAX_X+MIN_X)/2-100;
y1=(MIN_Y+MAX_Y)/2-38;
78. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 77
x2=(MAX_X+MIN_X)/2+100;
y2=(MIN_Y+MAX_Y)/2+38;
ShowStatus("press any key to exit");
for(i=x1;i<x2;i++)
for(j=y1;j<y2;j++)
{
if(i>x1+8 && i<x2-8 && j>y1+3 && j<y2-8)
putpixel(i,j,GREEN);
else
putpixel(i,j,BLUE);
}
outtextxy(x1+12,y1+7,"Coded and Developed by");
outtextxy(x1+30,y1+20,"Awadh kishor singh");
outtextxy(x1+13,y1+33,"");
outtextxy(x1+13,y1+46,"3rd yr it Engg");
outtextxy(x1+35,y1+59,"");
getch();
showmouseptr();
}
void create_box(int l,int t,int r,int b,int c,int c1,int c2)
{
int fc=CYAN;
setfillstyle(1,c);
setlinestyle(0,1,0);
setcolor(0);
bar(l,t,r,b);
setcolor(c1);
line(l,t,l,b);
line(l,t,r,t);
setcolor(c2);
line(r,t,r,b);
79. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 78
line(l,b,r,b);
setcolor(fc);
}
void new_file()
{ char ch=getch();
if(marked) unmark();
hidemouseptr();
if(!saved)
{
ShowStatus("Save Changes ? ");
if(ch=='y'||ch=='Y')
save();
}
strcpy(FileName,"Untitled");
disp_filename();
setfillstyle(SOLID_FILL,WHITE);
bar(MIN_X+2,MIN_Y+2,MAX_X-2,MAX_Y-2);
draw_button_border(Current_Button);
undraw_button_border(Prev_Button);
Current_Button=Prev_Button;
setfillstyle(Current_Pattern,Current_Color);
showmouseptr();
}
void save()
{
char* name;
FILE* out;
char ch;
int row,col;
int byte;
hidemouseptr();
81. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 80
ClearStatus();
free(name);
fclose(out);
}
}
}
ClearStatus();
strcpy(FileName,name);
disp_filename();
free(name);
fclose(out);
saved=1;
showmouseptr();
}
void load()
{
FILE* in;
char* name;
char ch;
int byte;
int row,col;
int temp;
if(!saved)
{
ShowStatus(" Save Current File ? ");
ch=getch();
if(ch=='y'||ch=='Y')
{
save();
}
}
82. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 81
name=readline(" Enter File To Load : ");
if(name==NULL)
return;
in=fopen(name,"r");
if(fopen==NULL)
{
ShowStatus(" Error Opening File ");
delay(1000);
ClearStatus();
return;
}
byte=fgetc(in);
for(row=MIN_Y+2;row<=MAX_Y-2;row++)
{
for(col=MIN_X+2;col<=MAX_X-2;)
{
temp=(byte&0xf0)>>4;
putpixel(col,row,temp);
col++;
temp=(byte&0x0f);
putpixel(col,row,temp);
col++;
byte=fgetc(in);
if(byte==EOF)
{
return;
}
}
}
strcpy(FileName,name);
disp_filename();
83. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 82
free(name);
fclose(in);
saved=1;
}
int main()
{
int g=DETECT,i,j,m,closeflag=0,tempx=0,tempy=0;
initgraph(&g,&m,"c:tcbgi");
if(graphresult())
{
printf("n Couldn't initialise graphics.... we tried our level bestn");
exit(1);
}
if(initmouse()==0)
{
printf("n We tried hard but couldn't initialise mousen");
exit(1);
}
start_up();
init();
disp_filename();
showmouseptr();
while(1)
{
getmousepos(&button,&x,&y);
getxy();
disp_coord();
if(x>617 && x<629 && y>6 && y<17 )
{
if(closeflag!=1)
{
84. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 83
hidemouseptr();
for(i=618;i<629;i++)
for(j=7;j<=16;j++)
if(getpixel(i,j)!=WHITE)
putpixel(i,j,6);
showmouseptr();
closeflag=1;
}
}
else if(closeflag==1)
{
hidemouseptr();
for(i=618;i<629;i++)
for(j=7;j<=16;j++)
if(getpixel(i,j)!=WHITE)
putpixel(i,j,BLUE);
showmouseptr();
closeflag=0;
}
if(button==1)
{
int fll=0;
if(check_if_button_pressed());
if(x>617 && x<629 && y>6 && y<17)
quit();
if(x>88&&y>17&&x<88+80&&y<37)
{
showfilemenu(88,17,80,20,x,y);
fll=1;
}
else if(x>88+82&&y>17&&x<88+2*80&&y<37)
85. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 84
{
showeditmenu(88+82,17,80,20,x,y);
fll=1;
}
else if(x>88+82*2&&y>17&&x<88+3*80&&y<37)
{
showformatmenu(88+2*82+2,17,80,20,x,y);
fll=1;
}
else if(x>88+3*82&&y>17&&x<88+4*80&&y<37)
{
showhelpmenu(88+3*82+3,17,80,20,x,y);
fll=1;
}
if(fll==1)
undraw_button_border(Current_Button);
else if(check_if_color())
{
ClearStatus();
}
else if(check_mouse_on(MIN_X+2,MIN_Y+2,MAX_X-2,MAX_Y-2))
{
ClearStatus();
saved=0; //file has been altered;
if(fll!=1)
Diagram();
}
else check_if_exit();
}
else check_if_button_pressed();
tempx=x;
86. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 85
tempy=y;
}
getch();
closegraph();
return 0;
}
Output:
87. COMPUTER GRAPHICS- LAB MANUAL Enrollment No :- 150450116015 2017
S. V. M. I. T., Bharuch| Branch – Information Technology 86