You are on page 1of 32

1.

# Implement the program to change the color and


background of the output of a program in C
#include <graphics.h>
#include <stdio.h>
int main()
{
int gd = DETECT, gm;
char arr[100];
initgraph(&gd, &gm, "");
setbkcolor(BLUE);
sprintf(arr, " HELLO WORLD ",getbkcolor());
outtextxy(10, 10, arr);

getch();
closegraph();

return 0;
}

OUTPUT-
# Write all the graphics function in computer graphics.
C graphics using graphics.h functions can be used to draw different shapes,
display text in different fonts, change colors and many more. 
Using functions of graphics.h in Turbo C compiler you can make graphics
programs, animations, projects, and games. You can draw circles, lines,
rectangles, bars and many other geometrical figures. You can change their colors
using the available functions and fill them. Following is a list of functions of
graphics.h header file. 

C Graphics Functions
Arc function:
Declaration: void arc(int x, int y, int stangle, int endangle, int radius);
"arc" function is used to draw an arc with center(x, y) and stangle specifies
starting angle, end angle specifies the end angle and last parameter specifies the
radius of the arc. arc function can also be used to draw a circle but for that
starting angle and end angle should be 0 and 360 respectively.

Bar function :
Declaration: void bar(int left, int top, int right, int bottom);

Bar function is used to draw a 2-dimensional, rectangular filled in bar.


Coordinates of left top and right bottom corner are required to draw the bar. Left
specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top
left corner, right specifies the X-coordinate of right bottom corner, bottom
specifies the Y-coordinate of right bottom corner.

Circle function:
Declaration: void circle(int x, int y, int radius);
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.

Cleardevice function:
Declaration: void cleardevice();

cleardevice function clears the screen in graphics mode and sets the current
position to (0,0). Clearing the screen consists of filling the screen with current
background colour.

closegraph function:
Declaration: void closegraph();
closegraph function closes the graphics mode, deallocates all memory allocated
by graphics system and restores the screen to the mode it was in before you
called initgraph.

drawpoly function:
Declaration: void drawpoly( int num, int *polypoints );
Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon,
hexagon etc.
num indicates (n+1) number of points where n is the number of vertices in a
polygon, polypoints points to a sequence of (n*2) integers . Each pair of integers
gives x and y coordinates of a point on the polygon. We specify (n+1) points as
first point coordinates should be equal to (n+1)th to draw a complete figure.

ellipse function:
Declarations of ellipse function :-
void ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius);
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.

fillellipse function:
Declaration of fillellipse function :-
void fillellipse(int x, int y, int xradius, int yradius);

x and y are coordinates of center of the ellipse, xradius and yradius are x and y
radius of ellipse respectively.

Fillpoly function:
Declaration: void drawpoly( int num, int *polypoints );
Fillpoly function draws and fills a polygon. It require same arguments
as drawpoly.
fillpoly fills using current fill pattern and color which can be changed
using setfillstyle.

getbkcolor function:
Declaration: int getbkcolor();
Function getbkcolor returns the current background-color.
getcolor function:
Declaration : int getcolor();
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.

getimage function:
Declaration:- void getimage(int left, int top, int right, int bottom, void *bitmap);
getimage function saves a bit image of specified region into memory, region can
be any rectangle.
getimage copies an image from screen to memory. Left, top, right, and bottom
define the area of the screen from which the rectangle is to be copied, bitmap
points to the area in memory where the bit image is stored.

getmaxcolor function:
Declaration: int getmaxcolor();
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.

getmaxx function:
Declaration: int getmaxx();
getmaxx function returns the maximum X coordinate for current graphics mode
and driver.
getpixel function:
Declaration: int getpixel(int x, int y);
getpixel function returns the color of pixel present at location(x, y).

getx function:
Declaration: int getx();
The function getx returns the X coordinate of the current position.

gety function:
Declaration: int gety();
gety function returns the y coordinate of current position.

graphdefaults function:
Declaration: void graphdefaults();
It resets the following graphics settings
Sets the viewport to the entire screen.
Moves the current position to (0,0).
Sets the default palette colors, background color, and drawing color.
Sets the default fill style and pattern.
Sets the default text font and justification.

imagesize function:
Declaration:- unsigned int imagesize(int left, int top, int right, int bottom);
imagesize function returns the number of bytes required to store a bitimage. This
function is used when we are using getimage.
putimage function:
Declaration:- void putimage(int left, int top, void *ptr, int op);
putimage puts the bit image previously saved with getimage back onto the
screen, with the upper left corner of the image placed at (left, top). ptr points to
the area in memory where the source image is stored. The op argument specifies
a operator that controls how the color for each destination pixel on screen is
computed, based on pixel already on screen and the corresponding source pixel
in memory.

setviewport function:
Declaration: void setviewport(int left, int top, int right, int bottom, int clip);
setviewport function sets the current viewport for graphics output.setviewport
function is used to restrict drawing to a particular portion on the screen. For
example,setviewport(100 , 100, 200, 200, 1);will restrict our drawing activity
inside the rectangle(100,100, 200, 200).
left, top, right, bottom are the coordinates of main diagonal of rectangle in which
we wish to restrict our drawing. Also note that the point (left, top) becomes the
new origin.

textheight function:
Declaration: int textheight(char *string);
textheight function returns the height of a string in pixels.

Textwidth function:
Declaration: int textwidth(char *string);
Textwidth function returns the width of a string in pixels.
2.

DDA line drawing algorithm:


Program:
#include<stdio.h>
#include<graphics.h>
int abs (int n)
{
return ( (n>0) ? n : ( n * (-1)));
}
void DDA(int X0, int Y0, int X1, int Y1)
{
int dx = X1 - X0;
int dy = Y1 - Y0;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float Xinc = dx / (float) steps;
float Yinc = dy / (float) steps;
int i;
float X = X0;
float Y = Y0;
for (i = 0; i <= steps; i++)
{
putpixel (X,Y,RED);
X += Xinc;
Y += Yinc;
delay(10);
}
}
void main()
{
int gd = DETECT, gm;
initgraph (&gd, &gm, "C:\TURBOC3\BGI");
DDA(100,100,300,400);
getch();
}

OUTPUT-
3.

Bresenham Algorithm

Program:

#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}

Output:
4.

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

void puracir(x,y,x1,y1)
{

putpixel(x1+x,y1+y,RED);
putpixel(x1+x,-y1+y,YELLOW);

delay(10);

putpixel(-x1+x,-y1+y,GREEN);
putpixel(-x1+x,y1+y,11);

delay(10);

putpixel(y1+x,x1+y,12);
putpixel(y1+x,-x1+y,12);

delay(10);

putpixel(-y1+x,-x1+y,12);
putpixel(-y1+x,x1+y,12);
}

void cir(int x,int y,int r)


{

int x1=0;
int y1=r;
int d=3-2*r;

puracir(x,y,x1,y1);

while(x1<=y1)
{

if(d<0)
{
d=d+4*x1+6;
}
else
{
d=d+4*(x1-y1)+10;
y1--;
}
x1++;
outtextxy(x-50,y-20,"PRASHANT SHUKLA");
outtextxy(x-50,y,"18SCSE1010454");

puracir(x,y,x1,y1);
}
}
void main()
{
int x,y,r;
int gd = DETECT, gm;

initgraph (&gd, &gm, "C:\TURBOC3\BGI");


printf("Enter cordinates of center (xc,yc): \n");
scanf("%d",&x);
scanf("%d",&y);
printf("Enter the values of there radius: ");
scanf("%d",&r);
cir(x,y,r);
getch();
}

Output-
5.

CODE-
#include<conio.h>

#include<graphics.h>

void puracir(x,y,x1,y1)

putpixel(x1+x,y1+y,BLUE);

putpixel(x1+x,-y1+y,YELLOW);

delay(10);

putpixel(-x1+x,-y1+y,GREEN);

putpixel(-x1+x,y1+y,11);

delay(10);

putpixel(y1+x,x1+y,12);

putpixel(y1+x,-x1+y,12);

delay(10);

putpixel(-y1+x,-x1+y,12);

putpixel(-y1+x,x1+y,12);

void cir(int x,int y,int r)

int x1=0;

int y1=r;

int d=1-r;

puracir(x,y,x1,y1);

while(x1<=y1)

{
if(d<0)

d=d+2*x1+3;

else

d=d+2*(x1-y1)+6;

y1--;

x1++;

outtextxy(x-50,y-20,"MAYANK RAJ");

outtextxy(x-50,y,"18SCSE1010336");

puracir(x,y,x1,y1);

void main()

int x,y,r;

int gd = DETECT, gm;

initgraph (&gd, &gm, "C:\turboc3\bgi");

printf("Enter cordinates of center (xc,yc): \n");

scanf("%d",&x);

scanf("%d",&y);

printf("Enter the values of there radius: ");

scanf("%d",&r);

cir(x,y,r);
getch();

OUTPUT-
6.

CODE
#include<graphics.h>
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int gd = DETECT, gm;
int xc,yc,x,y;float p;
long rx,ry;
initgraph(&gd, &gm, "C:\TURBOC3\BIN");
cout<<"Enter coordinates of centre : ";
cin>>xc>>yc;
cout<<"Enter x,y radius of ellipse: ";
cin>>rx>>ry;
p=ry*ry-rx*rx*ry+rx*rx/4;
x=0;y=ry;
while(2.0*ry*ry*x <= 2.0*rx*rx*y)
{
if(p < 0)
{
x++;
p = p+2*ry*ry*x+ry*ry;
}
else
{
x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y-ry*ry;
}
putpixel(xc+x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc-x,yc-y,RED);
}
p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;
while(y > 0)
{
if(p <= 0)
{
x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y+rx*rx;
}
else
{
y--;
p = p-2*rx*rx*y+rx*rx;
}
putpixel(xc+x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc-x,yc-y,RED);
}
getch();
closegraph();
}

OUTPUT
7.

PROGRAM:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"c:\TURBOC3\bg:");
printf("\t Program for basic transactions");
printf("\n\t Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
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);
getch();

case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
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));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*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);
getch();

case 4:
break;
default:
printf("Enter the correct choice");
}
closegraph();

Output:
8.

Aim : Write a program to perform 2D Transformations reflection and shearing.

Description-
Theory-Transformation means changing some graphics into something else by
applying rules. We can have various types of transformations such as translation,
scaling up or down, rotation, shearing, etc. When a transformation takes place on
a 2D plane, it is called 2D transformation.
Transformations play an important role in computer graphics to reposition the
graphics on the screen and change their size or orientation.

Reflection-Reflection is a kind of rotation where the angle of rotation is 180


degree.
The reflected object is always formed on the other side of mirror.
The size of reflected object is same as the size of original object.
Consider a point object O has to be reflected in a 2D plane.
Let-
Initial coordinates of the object O = (Xold, Yold)
New coordinates of the reflected object O after reflection = (Xnew, Ynew)
Reflection On X-Axis:
This reflection is achieved by using the following reflection equations-
Xnew = Xold
Ynew = -Yold
In Matrix form, the above reflection equations may be represented as-
For homogeneous coordinates, the above reflection matrix may be represented
as a 3 x 3 matrix as-as
Reflection On Y-Axis:
This reflection is achieved by using the following reflection equations-
Xnew = -Xold
Ynew = Yold
In Matrix form, the above reflection equations may be represented as-
For homogeneous coordinates, the above reflection matrix may be represented
as a 3 x 3 matrix

Shearing- In Computer graphics,


2D Shearing is an ideal technique to change the shape of an existing object in a
two dimensional plane.
In a two dimensional plane, the object size can be changed along X direction as
well as Y direction.
1-Shearing in X direction
2-Shearing in Y direction

Consider a point object O has to be sheared in a 2D plane.


Let-
Initial coordinates of the object O = (Xold, Yold)
Shearing parameter towards X direction = Shx
Shearing parameter towards Y direction = Shy
New coordinates of the object O after shearing = (Xnew, Ynew)
Shearing in X Axis-
Shearing in X axis is achieved by using the following shearing equations-
Xnew = Xold + Shx x Yold
Ynew = Yold
In Matrix form, the above shearing equations may be represented as-
Shearing in Y Axis-
Shearing in Y axis is achieved by using the following shearing equations-
Xnew = Xold
Ynew = Yold + Shy x Xold

Algorithm-
1. Start
2. Initialize the graphics mode.
3. Construct a 2D object (use Drawpoly()) e.g. (x,y)
4. Shearing
a. X-shear
i. Get the shearing value shx
ii. x’=x + shx * y, y’=y
iii. Plot (x’,y’)
b. Y-shear
i. Get the shearing value shy
ii. x’=x , y’=y+ shy * x
c. Plot (x’,y’)
5. Reflection
a. About X axis
i. x’= x , y’ = -y
ii. Plot (x’,y’)
b. About Y axis
i. x’= -x , y’=y
ii. plot (x’,y’)
Program-

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<alloc.h>
#include<stdlib.h>
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,gd=DETECT,gm;
initgraph(&gd,&gm,"e:\\tc\\bgi");
axis();
printf("\b");
cleardevice();
printf("Enter the number of vertices:");
scanf("%d",&n);
printf("Enter the co-ordinate center the first vertex at the end");
for(i=0;i<2*n+2;i++)
scanf("%d",&p[i]);
clrscr();
init();
getch();
do
{
clrscr();
cleardevice();
printf("\n \t\t2D-Transformation \n1.reflection\n2.shearing\n");
printf("\n\nEnter yuor choice:\n");
scanf("%d",&ch);
clrscr();
cleardevice();
switch(ch)
{
case 1:
reflect();
break;
case 2:
shear();
break;
}
}
while(ch!=3);
closegraph();
}
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]);
drawpoly(n+1,q);
setcolor(WHITE);
}
int convertx(int x)
{
x=x/2;
return(x+=320);
}
int converty(int y)
{
y=y/2;
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)
{
init();
printf("\ Enter the shear factor(shx):");
scanf("%f",&shx);
for(i=1;i<2*n+2;i+=2)
sh[i]=p[i]+(shx*(p[i+1]));
for(i=0;i<2*n+2;i+=2)
sh[i]=p[i];
}
else
{
init();
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];
}
setlinestyle(DOTTED_LINE,1,1);
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]);
drawpoly(n+1,sho);
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);
clrscr();
cleardevice();
if(c==1)
{
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];
}
init();
setlinestyle(DOTTED_LINE,1,1);
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]);
drawpoly(n+1,rft);
getch();
}

OUTPUT-

You might also like