0% found this document useful (0 votes)
378 views37 pages

C Graphics Practical Guide

The document describes an introduction to C graphics using various functions. It provides: 1. An overview of functions like initgraph(), closegraph(), and detectgraph() to initialize and close the graphics system. 2. Descriptions of basic functions like getpixel(), putpixel(), line(), circle(), arc() to manipulate individual pixels, draw lines and shapes. 3. Explanations of functions to set color, text properties and retrieve screen dimensions like setcolor(), settextjustify(), getmaxx(), getmaxy(). 4. An example C program implementing the Digital Differential Analyzer (DDA) line drawing algorithm using functions like putpixel() to draw a line incrementally.

Uploaded by

Milan Dudhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
378 views37 pages

C Graphics Practical Guide

The document describes an introduction to C graphics using various functions. It provides: 1. An overview of functions like initgraph(), closegraph(), and detectgraph() to initialize and close the graphics system. 2. Descriptions of basic functions like getpixel(), putpixel(), line(), circle(), arc() to manipulate individual pixels, draw lines and shapes. 3. Explanations of functions to set color, text properties and retrieve screen dimensions like setcolor(), settextjustify(), getmaxx(), getmaxy(). 4. An example C program implementing the Digital Differential Analyzer (DDA) line drawing algorithm using functions like putpixel() to draw a line incrementally.

Uploaded by

Milan Dudhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Darshan Institute of Engineering & Technology Experiment-1

Experiment:-1 Introduction to C graphics.

1. initgraph()
 Declaration: void far initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);
 Remarks:
 To start the graphics system, you must first call initgraph. initgraph initializes the graphics system by
loading a graphics driver from disk 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.
 Graphdriver:- Integer that specifies the graphics driver to be used.
 Graphmode:- integer that specifies the initial graphics mode (unless *graphdriver = DETECT).
*graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the
detected driver.
 pathtodriver :- Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first.

2. closegraph()
 Declaration: void far closegraph(void);
 Remarks:
 closegraph deallocates all memory allocated by the graphics system. It then restores the screen to
the mode it was in before you called initgraph.

3. detectgraph()
 Declaration: void far detectgraph(int far *graphdriver, int far *graphmode);
 Remarks:
 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, detectgraph sets *graphdriver to
grNotDetected, and graphresult returns grNotDetected.
 The main reason to call detectgraph directly is to override the graphics mode that detectgraph
recommends to initgraph.

4. delay()
 Declaration: void delay(unsigned milliseconds);
 Remarks:
 Suspends execution for interval (milliseconds). With a call to delay, the current program is suspended
from execution for the time specified by the argument milliseconds.
 It is not necessary to make a calibration call to delay before using it.

2160703 – Computer Graphics 1


Darshan Institute of Engineering & Technology Experiment-1

5. getpixel(), putpixel()
 Declaration:
unsigned far getpixel(int x, int y);
void far putpixel(int x, int y, int color);
 Remarks:
 getpixel gets the color of a specified pixel
 putpixel plots a pixel at a specified point

6. arc(), circle()
 Declaration:
void far arc(int x, int y, int stangle, int endangle, int radius);
void far circle(int x, int y, int radius);
 Remarks:
 arc draws a circular arc in the current drawing color.
 circle draws a circle in the current drawing color.
 (x,y) Center point of arc, circlew, or pie slice
 stangle Start angle in degrees
 endangle End angle in degrees
 radius Radius of arc, circle, and pieslice

7. getcolor(), setcolor()
 Declaration:
int far getcolor(void);
void far setcolor(int color);
 Remarks:
 getcolor returns the current drawing color.
 setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor.
 To select a drawing color with setcolor, you can pass either the color number or the equivalent color
name.

8. getmaxx() and getmaxy()


 Declaration:
int far getmaxx(void);
int far getmaxy(void);
 Remarks:
 getmaxx returns the maximum x value (screen-relative) for the current graphics driver and mode.
 getmaxy returns the maximum y value (screen-relative) for the current graphics driver and mode. For
example, on a CGA in 320 x 200 mode,getmaxx returns 319 and getmaxy returns 199.

2160703 – Computer Graphics 2


Darshan Institute of Engineering & Technology Experiment-1

9. line()
 Declaration: void far line(int x1, int y1, int x2, int y2);
 Remarks:
 line draws a line from (x1, y1) to (x2, y2) using the current color, line style, and thickness. It does not
update the current position (CP).

10. getx(), gety()


 Declaration:
int far getx(void);
int far gety(void);
 Remarks:
 getx returns the x-coordinate of the current graphics position.
 gety returns the y-coordinate of the current graphics position. The values are viewport-relative.

11. settextjustify()
 Sets text justification for graphics mode
 Declaration:
void far settextjustify(int horiz, int vert);
 Remarks:
 Text output after a call to settextjustify is justified around the current position (CP) horizontally and
vertically, as specified.
 The default justification settings are LEFT_TEXT (for horizontal) and TOP_TEXT (for vertical)

12. outtext(), outtextxy()


 Declaration:
void far outtext(char far *textstring);
void far outtextxy(int x, int y, char far *textstring);
 Remarks:
 outtext and outtextxy display a text string, using the current justification settings and the current font,
direction, and size.
 outtext outputs textstring at the current position (CP) outtextxy displays textstring in the viewport at the
position (x, y).

2160703 – Computer Graphics 3


Darshan Institute of Engineering & Technology Experiment-2

Experiment:-2.1 Write a C program to draw house shape using inbuilt function.


#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT ,gm;
int p[]={320,150,420,300,250,300,320,150},i;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
drawpoly(4,p);
fillellipse(100,100,60,60);
rectangle(400,300,280,400);
line(320,150,520,150);
line(520,150,620,300);
line(600,300,600,400);
line(320,400,600,400);
rectangle(320,350,350,400);
line(335,350,335,400);
rectangle(480,320,520,400);
rectangle(100,350,120,420);
fillellipse(110,320,30,30);
fillellipse(130,360,30,30);
fillellipse(100,360,30,30);
line(360,300,620,300);
for(i=330;i<520;i+=10)
{
line(i,150,i+100,300);
}
getch();
closegraph();
}
Experiment:-2.2 Write a C program to draw some beauiful shape (B yourself)
using inbuilt function.
#include<graphics.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm,NULL);
line(0,300,640,300);
setcolor(4);
2160703 – Computer Graphics 4
Darshan Institute of Engineering & Technology Experiment-2
circle(100,285,15);
circle(200,285,15);
circle(100,285,5);
circle(200,285,5);
line(65,285,85,285);
line(115,285,185,285);
line(215,285,235,285);
line(65,285,65,260);
line(235,285,235,260);
line(65,260,100,255);
line(235,260,200,255);
line(100,255,115,235);
line(200,255,185,235);
line(115,235,185,235);
line(106,255,118,238);
line(118,238,118,255);
line(106,255,118,255);
line(194,255,182,238);
line(182,238,182,255);
line(194,255,182,255);
line(121,238,121,255);
line(121,238,148,238);
line(121,255,148,255);
line(148,255,148,238);
line(179,238,179,255);
line(179,238,152,238);
line(179,255,152,255);
line(152,255,152,238);
setcolor(4);
getch();
closegraph();
}

2160703 – Computer Graphics 5


Darshan Institute of Engineering & Technology Experiment-3

Experiment:-3.1 Write a C program to implement DDA line drawing algorithm.


#include<stdio.h>
#include<graphics.h>
#include<math.h>

void lineDDA(int x1,int y1,int x2,int y2);

void main()
{
int x1,y1,x2,y2;

/* Read two end points of line


---------------------------------- */

printf("Enter the value of x1 :\t");


scanf("%d",&x1);
printf("Enter the value of y1 :\t");
scanf("%d",&y1);
printf("Enter the value of x2 :\t");
scanf("%d",&x2);
printf("Enter the value of y2 :\t");
scanf("%d",&y2);

/* Function Calling for line drawing


---------------------------------- */
lineDDA(x1,y1,x2,y2);
}

/*Function for DDA algorithm


---------------------------------- */
void lineDDA(int x1,int y1,int x2,int y2)
{
int i,gd,gm;
float xinc,yinc,steps,x,y;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");

xinc=abs(x2-x1);
yinc=abs(y2-y1);

2160703 – Computer Graphics 6


Darshan Institute of Engineering & Technology Experiment-3

if (xinc >= yinc)


{
steps = xinc;
}
else
{
steps = yinc;
}
xinc = (x2-x1)/(float)steps;
yinc = (y2-y1)/(float)steps;

/* Initialise starting point


-----------------------------*/
x=x1;
y=y1;

/* Initialise loop counter


-----------------------------*/
i = 1;

while(i <= steps)


{
putpixel(x,y,15);
x = x + xinc;
y = y + yinc;
i = i + 1;

/* Delay is purposely inserted to observe the line drawing process


-----------------------------*/
delay(100);
}
getch();
closegraph();

2160703 – Computer Graphics 7


Darshan Institute of Engineering & Technology Experiment-3

Experiment:-3.2 Write a C program to draw triangle using DDA line drawing


algorithm.
#include<stdio.h>
#include<graphics.h>
#include<math.h>

int ROUND( float a)


{
int b = a+0.5;
return b;
}

void lineDDA(int xa,int ya,int xb,int yb)


{
int dx=xb-xa,dy=yb-ya,steps,k;
float xincrement,yincrement,x=xa,y=ya;
if (abs(dx)>abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xincrement = (float)dx/(float)steps;
yincrement = (float)dy/(float)steps;

putpixel(ROUND(x),ROUND(y),RED);
for (int k = 0; k < steps; k++)
{
x+=xincrement;
y+=yincrement;
putpixel(ROUND(x),ROUND(y),RED);

2160703 – Computer Graphics 8


Darshan Institute of Engineering & Technology Experiment-3
void main()
{
int x1,y1,x2,y2,x3,y3;
int i,gd=DETECT,gm;

printf("enter x1:\n");
scanf("%d",&x1);
printf("enter y1:\n");
scanf("%d",&y1);
printf("enter x2:\n");
scanf("%d",&x2);
printf("enter y2:\n");
scanf("%d",&y2);
printf("enter x3:\n");
scanf("%d",&x3);
printf("enter y3:\n");
scanf("%d",&y3);

detectgraph(&gd,&gm);
initgraph(&gd,&gm,NULL);
lineDDA(x1,y1,x2,y2);
lineDDA(x2,y2,x3,y3);
lineDDA(x3,y3,x1,y1);
getch();
closegraph();
}

2160703 – Computer Graphics 9


Darshan Institute of Engineering & Technology Experiment-4

Experiment:-4.1 Write a C program to implement Bresenham’s line drawing


algorithm.
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void lineBRESENHAM(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2;
/* Read two end points of line
---------------------------------- */
printf("Enter the value of x1 :\t");
scanf("%d",&x1);
printf("Enter the value of y1 :\t");
scanf("%d",&y1);
printf("Enter the value of x2 :\t");
scanf("%d",&x2);
printf("Enter the value of y2 :\t");
scanf("%d",&y2);

/* Function Calling for line drawing


---------------------------------- */
lineBRESENHAM(x1,y1,x2,y2);
}

void lineBRESENHAM(int x1,int y1,int x2,int y2)


{
int gd,gm,i;
int dx,dy,x,y,p,two_dy,two_dy_dx,xend;
/* Initialise graphics mode
---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
dx=abs(x2-x1);
dy=abs(y2-y1);
/* Initialise decision variable
-------------------------------- */
p = 2 * dy-dx;
/* Calculating Constants
-------------------------------- */

2160703 – Computer Graphics 10


Darshan Institute of Engineering & Technology Experiment-4
two_dy=2*dy;
two_dy_dx=2*(dy-dx);
/* Initialise loop counter
-------------------------------- */
i = 1;
/* Initialise starting point by finding left side point
-------------------------------- */
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,RED);

while(x<xend)
{
x++;
if(p<0)
{
p=p+two_dy;
}
else
{
y++;
p=p+two_dy_dx;
}
putpixel(x,y,RED);
delay(200);
}
getch();
closegraph();

2160703 – Computer Graphics 11


Darshan Institute of Engineering & Technology Experiment-4

Experiment:-4.2 Write a C program to draw parallelogram using Bresenham’s


line drawing algorithm.
#include <stdio.h>
#include<graphics.h>
#include <math.h>

void bresen(int x1,int y1,int x2,int y2)


{
int k;
int dx=x2-x1;
int dy=y2-y1;
int twody=2*dy;
int twodymtwodx=twody-2*dx;
int x=x1;
int y=y1;
float p=twody-dx;
putpixel(x,y,RED);
for(k=0;k<dx;k++)
{
if (p<0)
{
p=p+twody;
}
else
{
p=p+twodymtwodx;
y++;
}
x++;
putpixel(x,y,RED);
}
}

void main()
{
int gd,gm,x1,y1,x2,y2,x3,y3,x4,y4;
printf("Enter x1:");
scanf("%d",&x1);
printf("Enter y1:");
scanf("%d",&y1);

2160703 – Computer Graphics 12


Darshan Institute of Engineering & Technology Experiment-4
printf("Enter x2:");
scanf("%d",&x2);
printf("Enter y2:");
scanf("%d",&y2);
printf("Enter x3:");
scanf("%d",&x3);
printf("Enter y3:");
scanf("%d",&y3);
printf("Enter x4:");
scanf("%d",&x4);
printf("Enter y4:");
scanf("%d",&y4);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
bresen(x1,y1,x2,y2);
bresen(x2,y2,x3,y3);
bresen(x4,y4,x3,y3);
bresen(x1,y1,x4,y4);
getch();
closegraph();
}

2160703 – Computer Graphics 13


Darshan Institute of Engineering & Technology Experiment-5

Experiment:-5.1 Write a C program to implement Midpoint circle drawing


algorithm.
#include<stdio.h>
#include<graphics.h>
#include<math.h>

void Midpointcirlce(int xc,int yc,int r);

void main()
{
int xc,yc,r;

printf("Enter the value of x center :\t");


scanf("%d",&xc);
printf("Enter the value of y center :\t");
scanf("%d",&yc);
printf("Enter the value of radious :\t");
scanf("%d",&r);

/* Function calling for circle generation


------------------------ */
Midpointcirlce(xc,yc,r);
}

void Midpointcirlce(int xc,int yc,int r)


{
int gd,gm,x,y,p;

/* initialise graphics
------------------------ */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");

x=0;
y=r;
p = 1-r;
do
{
putpixel(xc+x,yc+y,15);
putpixel(xc+y,yc+x,15);

2160703 – Computer Graphics 14


Darshan Institute of Engineering & Technology Experiment-5
putpixel(xc+x,yc-y,15);
putpixel(xc+y,yc-x,15);
putpixel(xc-x,yc-y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc-y,yc+x,15);
putpixel(xc-y,yc-x,15);

if (p < 0)
{
x = x+1;
y = y;
p = p + 2*x + 1;
}
else
{
x= x+1;
y= y-1;
p = p + 2*(x-y) + 1;
}
delay(200);
}while(x < y);
getch();
closegraph();
}
Experiment:-5.2 Write a C program to draw 5 concentric circle using Midpoint
circle drawing algorithm.
#include<stdio.h>
#include<graphics.h>

void drawcircle(int x0, int y0, int radius)


{
int x = radius;
int y = 0;
int err = 0;

while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);

2160703 – Computer Graphics 15


Darshan Institute of Engineering & Technology Experiment-5
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);

if (err <= 0)
{
y += 1;
err += 2*y + 1;
}

if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
delay(200);
}
delay(1000);
}

void main()
{
int gdriver=DETECT, gmode, error, x, y, r,x1,y1,r1;
setcolor(RED);
printf("Enter radius of circle: ");
scanf("%d", &r);
printf("Enter co-ordinates of center(x and y): ");
scanf("%d%d", &x, &y);
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
drawcircle(x,y,r);
drawcircle(x,y,r-10);
drawcircle(x,y,r-20);
drawcircle(x,y,r-30);
drawcircle(x,y,r-40);
getch();
closegraph();
}

2160703 – Computer Graphics 16


Darshan Institute of Engineering & Technology Experiment-6

Experiment:-6 Write a C program to implement Midpoint ellipse drawing


algorithm.
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#define ROUND(a) ((int)(a+0.5))

void ellipseplotpoints(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);
}

void ellipsemidpoint(int xc, int yc, int rx, int ry)


{
int rx2 = rx*rx;
int ry2 = ry*ry;
int drx2 = 2*rx2;
int dry2 = 2*ry2;
int p, x=0, y=ry, px=0, py=drx2*y;

ellipseplotpoints(xc, yc, x, y);

p = ROUND (ry2 - (rx2*ry) + (0.25*rx2));

while(px < py)


{
x++;
px += dry2;

if(p<0)
{
p += ry2 + px;
}
else
{
y--;
py -= drx2;
p += ry2 + px - py;
}
ellipseplotpoints(xc, yc, x, y);
delay(100);
}
p = ROUND (ry2 * (x + 0.5) * (x + 0.5) + rx2 *(y-1)*(y-1) - rx2*ry2);
2160703 – Computer Graphics 17
Darshan Institute of Engineering & Technology Experiment-6
while(y>0)
{
y--;
py -= drx2;

if(p>0)
{
p += rx2 - py;
}
else
{
x++;
px += drx2;
p += rx2 - py +px;
}
ellipseplotpoints(xc, yc, x ,y);
delay(100);
}
delay(1000);
}

void main()
{
int xcent, ycent, x, y,gd,gm;

printf("Enter value for x-center :-");


scanf("%d" ,&xcent);
printf("Enter value for y-center :-");
scanf("%d" ,&ycent);
printf("Enter value for radius from x :-");
scanf("%d" ,&x);
printf("Enter value for radius from y :-");
scanf("%d" ,&y);

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");

ellipsemidpoint(xcent, ycent, x, y);

closegraph();
}

2160703 – Computer Graphics 18


Darshan Institute of Engineering & Technology Experiment-7

Experiment:-7 Write a C program to implement Character Generation


algorithm for letter A and then modify matrix for some other letter of
alphabets.
#include<stdio.h>
#include<graphics.h>
void main()
{
int gd,gm,i,j;

/* Save character map of letter A


------------------------------------- */
int a[13][9] = {
{ 0, 0, 0, 0, 1, 0, 0, 0, 0},
{ 0, 0, 0, 1, 0, 1, 0, 0, 0},
{ 0, 0, 1, 0, 0, 0, 1, 0, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 1, 1, 1, 1, 1, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
{ 0, 1, 0, 0, 0, 0, 0, 1, 0},
};
/* Initialise graphics mode
---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
for(i=0;i<13;i++)
{
for(j=0;j<9;j++)
{
putpixel(200+j,200+i,15*a[i][j]);
}
}
getch();
closegraph();
}
2160703 – Computer Graphics 19
Darshan Institute of Engineering & Technology Experiment-8

Experiment:-8.1 Write a C program to implement Boundary fill algorithm.


#include<stdio.h>
#include<graphics.h>

boundary(int x, int y, int f_col, int b_col)


{
if (getpixel(x,y)!= b_col && getpixel(x,y)!= f_col)
{
putpixel(x,y,f_col);
boundary(x+1,y,f_col,b_col);
boundary(x-1,y,f_col,b_col);
boundary(x,y+1,f_col,b_col);
boundary(x,y-1,f_col,b_col);
}
}

void main()
{
int gd,gm;

/* Initialise graphics mode


---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
setcolor(RED);
rectangle(50,50,100,100);
boundary(55,55,BLUE,RED);
getch();
closegraph();
}
Experiment:-8.2 Write a C program to implement Flood fill algorithm.
#include<stdio.h>
#include<graphics.h>

flood_fill(x,y,old_col,new_col)
{
if (getpixel(x,y)==old_col)
{
putpixel(x,y,new_col);
flood_fill(x+1,y,old_col,new_col);

2160703 – Computer Graphics 20


Darshan Institute of Engineering & Technology Experiment-8
flood_fill(x-1,y,old_col,new_col);
flood_fill(x,y+1,old_col,new_col);
flood_fill(x,y-1,old_col,new_col);
flood_fill(x+1,y+1,old_col,new_col);
flood_fill(x-1,y-1,old_col,new_col);
flood_fill(x+1,y-1,old_col,new_col);
flood_fill(x-1,y+1,old_col,new_col);
}
}

boundary(int x, int y, int f_col, int b_col)


{
if (getpixel(x,y)!= b_col && getpixel(x,y)!= f_col)
{
putpixel(x,y,f_col);
boundary(x+1,y,f_col,b_col);
boundary(x-1,y,f_col,b_col);
boundary(x,y+1,f_col,b_col);
boundary(x,y-1,f_col,b_col);
}
}

void main()
{
int gd,gm;

/* Initialise graphics mode


---------------------------------- */
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
setcolor(RED);
rectangle(50,50,100,100);
boundary(55,55,BLUE,RED);
delay(1000);
flood_fill(55,55,BLUE,YELLOW);
getch();
closegraph();
}

2160703 – Computer Graphics 21


Darshan Institute of Engineering & Technology Experiment-9

Experiment:-9 Write a C program to implement Cohen Sutherland line clipping


algorithm.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
/* Defining structure for end point of line */
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline (PT p1,PT p2,int cl);
PT setcode(PT p);
int visibility (PT p1,PT p2);
PT resetendpt (PT p1,PT p2);
/* Function to draw window */
void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
/* Function to draw line between two points
---------------------------------------------*/
void drawline (PT p1,PT p2,int cl)
{
setcolor(cl);
line(p1.x,p1.y,p2.x,p2.y);
}
/* Function to set code of the coordinates
--------------------------------------------*/
PT setcode(PT p)
{
PT ptemp;
if(p.y<100)
{

2160703 – Computer Graphics 22


Darshan Institute of Engineering & Technology Experiment-9
ptemp.code[0]='1'; /* TOP */
}
else
{
ptemp.code[0]='0';
}
if(p.y>350)
{
ptemp.code[1]='1'; /* BOTTOM */
}
else
{
ptemp.code[1]='0';
}
if (p.x>450)
{
ptemp.code[2]='1'; /* RIGHT */
}
else
{
ptemp.code[2]='0';
}
if (p.x<150) /* LEFT */
{
ptemp.code[3]='1';
}
else
{
ptemp.code[3]='0';
}
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}

/* Function to determine visibility of line


--------------------------------------------*/
int visibility (PT p1,PT p2)
{
int i,flag=0;

2160703 – Computer Graphics 23


Darshan Institute of Engineering & Technology Experiment-9
for(i=0;i<4;i++)
{
if((p1.code[i]!='0')||(p2.code[i]!='0'))
{
flag=1;
}
}
if(flag==0)
{
return(0);
}
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) &&(p1.code[i]=='1'))
{
flag=0;
}
}
if(flag==0)
{
return(1);
}
return(2);
}

/* Function to find new end points


--------------------------------------*/
PT resetendpt (PT p1,PT p2)
{
PT temp;
int x,y,i;
float m=(float)(p2.y-p1.y)/(p2.x-p1.x),k;
if( p1.code[3]=='1') /* Cutting LEFT Edge */
{
x=150;
}
if(p1.code[2]=='1') /* Cutting RIGHT Edge */
{
x=450;
}

2160703 – Computer Graphics 24


Darshan Institute of Engineering & Technology Experiment-9
if((p1.code[3]=='1')||(p1.code[2]=='1'))
{
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
{
temp.code[i]=p1.code[i];
}
if(temp.y<=350&&temp.y>=100)
{
return(temp);
}
}
if(p1.code[0]=='1') /* Cutting TOP Edge */
{
y=100;
}
if(p1.code [1]=='1') /* Cutting BOTTOM Edge */
{
y=350;
}
if((p1.code[0]=='1')||(p1.code[1]=='1'))
{
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
{
temp.code[i]=p1.code[i];
}
return(temp);
}
else
{
return(p1);
}
}

2160703 – Computer Graphics 25


Darshan Institute of Engineering & Technology Experiment-9
main()
{
int gd=DETECT,gm,v;
PT p1,p2,ptemp;
printf("\n\n\t\tENTER END-POINT 1 (x,y): ");
scanf("%d%d",&p1.x,&p1.y);
printf("\n\n\t\tENTER END-POINT 2 (x,y): ");
scanf("%d%d",&p2.x,&p2.y);
initgraph(&gd,&gm,"");
cleardevice();
drawwindow();
drawline(p1,p2,15);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(1000);
switch(v)
{
case 0: cleardevice(); /* Line conpletely visible */
drawwindow();
drawline(p1,p2,15);
break;
case 1: cleardevice(); /* Line completely invisible */
drawwindow();
break;
case 2: cleardevice(); /* line partly visible */
p1=resetendpt (p1,p2);
p2=resetendpt(p2,p1);
drawwindow();
drawline(p1,p2,15);
break;
}
getch();
closegraph();
return(0);
}

2160703 – Computer Graphics 26


Darshan Institute of Engineering & Technology Experiment-10

Experiment:-10 Write a C program to implement Liang-Barsky line clipping


algorithm.
#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
int i,gd,gm;
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2;
float t1,t2,p[4],q[4],temp;
printf("Enter Line end point x1, y1:");
scanf("%d%d",&x1,&y1);
printf("Enter Line end point x2, y2:");
scanf("%d%d",&x2,&y2);
printf("Enter Cliping Rectangle xmin, xmax:");
scanf("%d%d",&xmin,&xmax);
printf("Enter Cliping Rectangle ymin, ymax:");
scanf("%d%d",&ymin,&ymax);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
p[0] = -(x2-x1);
p[1] = (x2-x1);
p[2] = -(y2-y1);
p[3] = (y2-y1);
q[0] = (x1-xmin);
q[1] = (xmax-x1);
q[2] = (y1-ymin);
q[3] = (ymax-y1);

for(i=0;i<4;i++)
{
if(p[i]==0)
{
//printf("line is parallel to one of the clipping boundary");
outtextxy(10,10,"line is parallel to one of the clipping boundary");
delay(500);
if(q[i] >= 0)
{

2160703 – Computer Graphics 27


Darshan Institute of Engineering & Technology Experiment-10
if(i < 2)
{
if (y1 < ymin)
{
y1 = ymin;
}
if (y2 > ymax)
{
y2 = ymax;
}
delay(1000);
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
}
if(i > 1)
{
if (x1 < xmin)
{
x1 = xmin;
}
if (x2 > xmax)
{
x2 = xmax;
}
delay(1000);
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
}
}
getch();
return(0);
}

}
t1 = 0;
t2 = 1;
for(i=0;i<4;i++)
{

2160703 – Computer Graphics 28


Darshan Institute of Engineering & Technology Experiment-10
temp = q[i]/p[i];
if(p[i] < 0)
{
if(t1 <= temp)
{
t1 = temp;
}

}
else
{
if(t2 > temp)
{
t2 = temp;
}

}
}
if(t1<t2)
{
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
}
delay(1000);
cleardevice();
outtextxy(10,10,"Result After Clipping");
rectangle(xmin,ymin,xmax,ymax);
line(xx1,yy1,xx2,yy2);
getch();
closegraph();
}

2160703 – Computer Graphics 29


Darshan Institute of Engineering & Technology Experiment-11

Experiment:-11.1 Write a C program to implement basic 2D translation.


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include<math.h>
void main()
{
int gm,gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3;
int xt,yt;
float t;

printf("\t Program for 2D Tranlsation");


printf("\n\t Enter the points of triangle");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("\n Enter the translation factor in x and y direction");
scanf("%d%d",&xt,&yt);

initgraph(&gd,&gm,"");
setcolor(1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
delay(2000);
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
closegraph();
}

2160703 – Computer Graphics 30


Darshan Institute of Engineering & Technology Experiment-11

Experiment:-11.2 Write a C program to implement basic 2D rotation.


/* while compiling program having use of function which are written in math.h we need to externally
link math.h by adding -lm in compile command*/
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include<math.h>
void main()
{
int gm,gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3;
int r;
float t,m;
printf("\t Program for 2D Rotation");
printf("\n\t Enter the points of triangle");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("\n Enter the angle of rotation");
scanf("%d",&r);

initgraph(&gd,&gm,"c:\tc\bg:");
setcolor(1);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);

t=(3.14*r)/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));

delay(1000);
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
closegraph();
}

2160703 – Computer Graphics 31


Darshan Institute of Engineering & Technology Experiment-11

Experiment:-11.3 Write a C program to implement basic 2D scaling.


#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gm,gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3;
int sx,sy;

printf("\t Program for 2D Scaling");


printf("\n\t Enter the points of triangle");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("\n Enter the scalling factor in x and y direction");
scanf("%d%d",&sx,&sy);

initgraph(&gd,&gm,"c:\tc\bg:");
setcolor(1);

line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);

nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;

delay(1000);

line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
closegraph();
}

2160703 – Computer Graphics 32


Darshan Institute of Engineering & Technology Experiment-12

Experiment:-12 Write a C program to implement 2D reflection and shearing.


#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
int gd=DETECT,gm;
int n,i,p[20],q[20];
int convertx(int x);
int converty(int y);
void axis(void);
void init(void);
void shear(void);
void reflect(void);
void main()
{
int ch;
printf("\b");
printf("Enter the number of vertices:");
scanf("%d",&n);
printf("Enter the co-ordinate of polygon (Enter the first vertex at the end again)");
for(i=0;i<2*n+2;i++)
{
scanf("%d",&p[i]);
}

do
{
printf("\n \t\t2D-Transformation \n1.reflection\n2.shearing\n3.exit");
printf("\n\nEnter yuor choice:\n");
scanf("%d",&ch);

switch(ch)
{
case 1:
reflect();
break;
case 2:
shear();
break;
case 3: break;

2160703 – Computer Graphics 33


Darshan Institute of Engineering & Technology Experiment-12
default:
printf("Wrong choice enter again");
break;
}
}while(ch!=3);
}

void axis(void)
{
cleardevice();
setlinestyle(SOLID_LINE,1,1);
line(320,0,320,getmaxy());
line(0,240,getmaxx(),240);
outtextxy(325,245,"0");
line(316,40,324,40);
outtextxy(325,41,"400");
line(316,440,324,440);
outtextxy(325,441,"-400");
line(20,236,20,244);
outtextxy(19,250,"-600");
line(620,236,620,244);
outtextxy(608,250,"600");
}

void init()
{
cleardevice();
axis();
setcolor(YELLOW);
setlinestyle(SOLID_LINE,1,1);
for(i=0;i<2*n+2;i+=2)
{
q[i]=convertx(p[i]);
}
for(i=1;i<2*n+2;i+=2)
{
q[i]=converty(p[i]);
}

for(i=0;i<2*n;i+=2)

2160703 – Computer Graphics 34


Darshan Institute of Engineering & Technology Experiment-12
{
line(q[i],q[i+1],q[i+2],q[i+3]);
}
setcolor(WHITE);
}

int convertx(int x)
{
x=x/2;
return(x+=320);
}

int converty(int y)
{
return(y=240-y);
}

void shear()
{
int sh[20],sho[20];
float shx,shy,c;
printf("(1)share relate to x-axis\n");
printf("(2)share relate to y-axis\n");
printf("\n Enter your choice:");
scanf("%f",&c);
if(c==1)
{
printf("\nEnter the shear factor(shx):");
scanf("%f",&shx);
for(i=0;i<2*n+2;i+=2)
{
sh[i]=p[i]+(shx*p[i+1]);
}
for(i=1;i<2*n+2;i+=2)
{
sh[i]=p[i];
}
}
else
{

2160703 – Computer Graphics 35


Darshan Institute of Engineering & Technology Experiment-12
printf("\enter the shear factor(shy):");
scanf("%f",&shy);
for(i=1;i<2*n+2;i+=2)
{
sh[i]=(shy*p[i-1])+p[i];
}
for(i=0;i<2*n+2;i+=2)
{
sh[i]=p[i];
}
}
for(i=0;i<2*n+2;i+=2)
{
sho[i]=convertx(sh[i]);
}
for(i=1;i<2*n+2;i+=2)
{
sho[i]=converty(sh[i]);
}
initgraph(&gd,&gm,"");
axis();
init();
setlinestyle(DOTTED_LINE,1,1);

for(i=0;i<2*n;i+=2)
{
line(sho[i],sho[i+1],sho[i+2],sho[i+3]);
}
getch();
}

void reflect()
{
int i,c,rf[20],rft[20];
printf("\n 1.x-axis reflection\n 2.y-axis reflection");
printf("\n Enter the choice:");
scanf("%d",&c);

if(c==1)
{

2160703 – Computer Graphics 36


Darshan Institute of Engineering & Technology Experiment-12
for(i=1;i<2*n+2;i+=2)
{
rf[i]=p[i]*(-1);
}
for(i=0;i<2*n+2;i+=2)
{
rf[i]=p[i];
}
}
else
{
for(i=0;i<2*n+2;i+=2)
{
rf[i]=p[i]*(-1);
}
for(i=1;i<2*n+2;i+=2)
{
rf[i]=p[i];
}
}
for(i=0;i<2*n+2;i+=2)
{
rft[i]=convertx(rf[i]);
}
for(i=1;i<2*n+2;i+=2)
{
rft[i]=converty(rf[i]);
}
initgraph(&gd,&gm,"");
axis();
init();
setlinestyle(DOTTED_LINE,1,1);

for(i=0;i<2*n;i+=2)
{
line(rft[i],rft[i+1],rft[i+2],rft[i+3]);
}
getch();
closegraph();
}

2160703 – Computer Graphics 37

You might also like