Practical 1
Q1.Study of library functions from graphics.h
1)INITGRAPH
DECLARATION
* void far initgraph(int far *graph driver)
EXPLANATION
* To start the graphic system, you must first call initgraph.
*Initgraph initializes the graphic system by loading a graphics driver from disk
(or validating a registered driver) then putting the system into graphics mode.
*Initgraph also resets all graphics settings
(color,palette,currentposition,viewport, etc) to their defaults then resets graph.
2)GETPIXEL, PUTPIXEL
DECLARATION
* Unsigned far getpixel(int x, int y)
* void far putpixel(int x, int y, int color)
EXPLANATION
*Getpixel gets the color of the pixel located at (x,y)
*Putpixel plots a point in the color defined at (x, y).
3)CLOSE GRAPH
DECLARATION
*void far closegraph(void);
EXPLANATION
*Close graph deallocates all memory allocated by the graphic system.
*It then restores the screen to the mode it was in before you called initgraph.
4) ARC, CIRCLE, PIESLICE
DECLARATION
*void far arc(int x, int y, intstangle, intendangle, int radius);
*void far circle(int x, int y, int radius);
*void far pieslice(int x, int y, intstangle, intendangle, int radius);
EXPLANATION
*Arc draws a circular arc in the current drawing color
*Circle draws a circle in the current drawing color
*Pieslice draws a pieslice in the current drawing color, then fills it using the
current fillpattern and fill color.
5) ELLIPSE, FILLELIPSE, SECTOR
DECLARATION
*void far ellipse(int x, int y, intstangle, intendangle, intxradius, intyradius)
*void far fillellipse(int x, int y, intxradius, intyradius)
*void farsectoe(int x, int y, intstangle,intendangle, intxradius, intyradius)
EXPLANATION
*Ellipse draws an elliptical arc in the current drawing color.
*Fillellipse draws an elliptical arc in the current drawing color and than fills it
with fill color and fill pattern.
*Sector draws an elliptical pie slice in the current drawing color and than fills it
using the pattern and color defined by set fillstyle or setfillpattern.
6) FLOODFILL
*Flood-fills a bounded region.
DECLARATION
*void far floodfill(int x, int y, int border)
EXPLANATION
*Floodfills an enclosed area on bitmap device.
*The area bounded by the colorborder is flooded with the current fill pattern and
fill color.
*If the seed is within an enclosedarea, the inside will be filled.If the seed is
outside the enclosed area, the exterior will be filled.
*Use fillpoly instead of floodfill wherever possible so you can maintain code
compatibility with future versions.
*Floodfilldoesnot work with the IBM-8514 driver.
7) GETCOLOR, SETCOLOR
*Getcolor returns the current drawing color.
*Setcolor returns the current drawing color.
DECLARATION
*int far getcolor(void);
*void far setcolor(int color)
EXPLANATION
*Getcolor returns the current drawing color.
*Setcolor sets the current drawing color to color, which can range from 0 to
getmaxcolor.
*To set a drawing color with setcolor ,you can pass either the color number or
theequivalent color name.
8) LINE, LINEREL, LINETO
DECLARATION
*void far lineto(int x, int y)
EXPLANATION
*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).
*Linerel draws a line from the CP to a point that is relative distance
(dx, dy) from the CP, then advances the CP by (dx, dy).
*Lineto draws a line from the CP to
(x, y), then moves the CP to (x,y).
9)RECTANGLE
DECLARATION
*void far rectangle(int left, int top, int right, int bottom)
EXPLANATION
*It draws a rectangle in the current line style, thickness and drawing color.
*(left, top)is the upperleft cornerofthe rectangle, and (right, bottom)is its lower
right corner.
10)MOVEREL,MOVETO
DECLARATION
*void moverel(int x, int y);
*void moveto(int x, int y);
EXPLANATION
*moveto function changes the current position (CP) to (x, y)
*moverel function moves the current position to a relative distance.
11)IMAGESIZE,GETIMAGE,PUTIMAGE
DECLARATION
*intimagesize(int left, int top, int right, int bottom);
*void getimage(int left, int top, int right, int bottom, void *bitmap);
*void putimage(int left, int top, void *ptr, int op);
EXPLANATION
*imagesize function returns the number of bytes required to store a bitimage.
This function is used when we are using getimage.
*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.
*putimage function outputs a bit image onto the screen.
*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 correspondingsource pixel
in memory.
11)GETX,GETY
DECLARATION
*intgetx();
* intgety();
EXPLANATION
*getx function returns the X coordinate of current position.
*gety function returns the y coordinate of current position.
12)GETMAXX,GETMAXY
DECLARATION
*intgetmaxx();
*intgetmaxy();
EXPLANATION
*getmaxy function returns the maximum Y coordinate for current graphics
mode and driver.
*getmaxx function returns the maximum X coordinate for current graphics
mode and driver.
13)FILLPOLY,DRAWPOLY
DECLARATION
*void drawpoly( intnum, int *polypoints );
*void drawpoly( intnum, int *polypoints );
EXPLANATION
*Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon,
hexagon etc.
*Fillpoly function draws and fills a polygon. It require same arguments as
drawpoly.
*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.
*To understand more clearly we will draw a triangle using drawpoly, consider
for example the array :-
int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};
points array contains coordinates of triangle which are (320, 150), (420, 300)
and (250, 300). Note that last point(320, 150) in array is same as first.
14)SETLINESTYLE
DECLARATION
*void setlinestyle( intlinestyle, unsigned upattern, int thickness );
EXPLANATION
*ItdrawsSOLID_LINE,DOTTED_LINE,CENTER_LINE,DASHED_LINE,US
ERBIT_LINE.
15)GETBKCOLOR,SETBKCOLOR
DECLARATION
*intgetbkcolor();
* void setbkcolor(intcolor);
EXPLANATION
*getbkcolor function returns the current background color
*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.
16)GETMODENAME
DECLARATION
*char * far getmodename(int mode_number);
EXPLNATION
*Returns the name of a specified graphics mode
*getmodename accepts a graphics mode number as input and returns a
stringcontaining the name of the corresponding graphics mode.
*The mode names are embedded in each driver.
Q2.Write a program to draw a rectangle.
CODE
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode= graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %sn", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code*/
}
left = getmaxx() / 2 - 50;
top = getmaxy() / 2 - 50;
right = getmaxx() / 2 + 50;
bottom = getmaxy() / 2 + 50;
rectangle(50,50,100,100);
/* draw a rectangle */
/* clean up */
getch();
closegraph();
return 0;
}
OUTPUT:
Q3.Write a program to draw a smiley.
Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:TurboC++DiskTurboC3BIN");
/* read result of initialization */
errorcode= graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %sn", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code*/
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw the circle */
circle(midx, midy, radius);
circle(midx-50, midy-20, 20);
circle(midx+50, midy-20, 20);
line(midx, midy+20, midx, midy-10);
arc(midx, midy+20, 225, 315, 50);
/* clean up */
getch();
closegraph();
return 0;
}
OUTPUT:
Q3.Write a program to draw a star.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:turboc3bgi");
/* read result of initialization */
errorcode= graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %sn", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code*/
}
moveto(60,90);
lineto(100,150);
lineto(30,150);
lineto(60,90);
moveto(60,170);
lineto(30,110);
lineto(100,110);
lineto(60,170);
/* clean up */
getch();
closegraph();
return 0;
}
OUTPUT:
Practical 2
Aim :- Implement DDA Line Drawing Algorithm
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void draw(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
printf(“n Enter the x and y value for starting point:n”);
scanf(“%d%d”,&x1,&y1);
printf(“n Enter the x and y value for ending point:n”);
scanf(“%d%d”,&x2,&y2);
clrscr();
initgraph(&gdriver,&gmode,””);
draw(x1,y1,x2,y2);
getch();
closegraph();
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(x,y,15);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,15);
}
}
OUTPUT:
Practical 3
Aim :- Implement Bresenham's Line Drawing Algorithm
CODE:
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,e,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("nntEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("nntEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx=(x2 - x1);
dy=(y2 - y1);
e=2*(dy)-(dx);
x=x1;
y=y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:tcbgi");
putpixel(x,y,WHITE);
while(x<=x2)
{
if(e<0)
{
x=x+1;
e=e+2*(dy);
}
else
{
x=x+1;
y=y+1;
e=e+2*(dy-dx);
}
putpixel(x,y,15);
}
getch();
closegraph();
}
OUTPUT:
Practical 4
Aim: Write a program to draw dash, dotted and thick line.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
intgdriver = DETECT, gmode, errorcode;
float wx,wy,x1,y1,x2,y2;
inti,thicknkess;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode= graphresult();
/* an error occurred */
if (errorcode != grOk)
k {
printf("Graphics error: %sn", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
printf("enter the co-ordinates:");
printf("nx1:");
scanf("%f",&x1);
printf("y1:");
scanf("%f",&y1);
printf("x2:");
scanf("%f",&x2);
printf("y2:");
scanf("%f",&y2);
printf("enter the required thickness:");
scanf("%d",&thickness);
cleardevice();
line(x1,y1,x2,y2);
if((y2-y1)/(x2-x1)<1)
{
wy=(thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2)/2*fabs(x2-x1));
for(i=0;i<wy;i++)
{
line(x1,y1-i,x2,y2-i);
line(x1,y1+i,x2,y2+i);
}
}
else
{
wx=(thickness-1)*sqrt(pow((y2-y1),2)+pow((x2-x1),2)/2*fabs(y2-y1));
for(i=0;i<wx;i++)
{
line(x1-i,y1,x2-i,y2);
line(x1+i,y1,x2+i,y2);
}
}
printf("this is the line of thickness %d units.n",thickness);
/* clean up */
getch();
closegraph();
return 0;
}
OUTPUT:
CODE:DASHED AND DOTTED LINE
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int sign(inta,int b)
{
if(a>b)
return 1;
if(a<b)
return -1;
if(a==b)
return 0;
}
void main()
{
intgdriver = DETECT, gmode, errorcode;
int x1,y1,x2,y2,dx,dy,p,i,x,y,s1,s2;
intlg,ld,g=0,d=0;
initgraph(&gdriver, &gmode, "");
printf("n Enter value of x1 & y1 ::");
scanf("%d %d",&x1,&y1);
printf("nk Enter value of x2 & y2 ::");
scanf("%d %d",&x2,&y2);
printf("n Enter the length of dot:");
scanf("%d", &ld);
printf("n Enter the length of gap :");
scanf("%d", &lg);
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy - dx;
x=x1;
y=y1;
s1=sign(x2,x1);
s2=sign(y2,y1);
putpixel(x,y,3);
for(i=0;i<=dx;i++)
{
if(p<0)
{
x=x+1;
y=y;
if(d<ld)
{
putpixel(x,y,3);
d++;
}
else if(g<lg)
{
g++;
}
else
{
g=0;
d=0;
}
p=p+(2*dy);
}
else
{
x=x+1;
y=y+1;
if(d<ld)
{
putpixel(x,y,3);
d++;
}
else if(g<lg)
{ g++;
}
else
{
g=0;
d=0;
}
p=p+2*(dy-dx);
}
}
getch(); }
OUTPUT:
Practical 5
Aim :- Implement PolygonFilling Algorithms.
[A] Boundary Fill
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
void drawply();
void boundryfill(int x1, int x2, int y1, int y2);
main()
{
int gd=DETECT,gm,x,y,p[8],x1,x2,y1,y2;
initgraph(&gd,&gm," ");
cleardevice();
drawply();
getch();
x1=300;
x2=371;
y1=240;
y2=170;
while(x1!=x2&&y1!=y2)
{
x1++;
x2--;
y1--;
y2++;
boundryfill(x1,x2,y1,y2);
}
outtextxy(300,50,"Boundry fill");
getch();
closegraph();
return(0);
}
void drawply()
{
setcolor (WHITE);
line(300,240,371,240);
line(371,240,371,170);
line(371,170,300,170);
line(300,170,300,240);
}
void boundryfill(int x1, int x2, int y1, int y2)
{
int i;
for(i=x2;i>=x1;i--)
{
putpixel(i,y1,WHITE);
delay(0.5);
}
for(i=y1; i>=y2;i--)
{
putpixel(x1,i,WHITE);
delay(0.5);
}
for(i=x1;i<=x2;i++)
{
putpixel(i,y2,WHITE);
delay(0.5);
}
for(i=y2; i<=y1;i++)
{
putpixel(x2,i,WHITE);
delay(0.5);
}
}
OUTPUT:
[B] Flood Fill
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void flood(int seed_x,int seed_y,int foreground_col,int background_col)
{
if (getpixel(seed_x,seed_y)!= background_col&& getpixel(seed_x,seed_y)!=
foreground_col)
{
putpixel(seed_x,seed_y,foreground_col);
flood(seed_x+1,seed_y,foreground_col,background_col);
flood(seed_x-1,seed_y,foreground_col,background_col);
flood(seed_x,seed_y+1,foreground_col,background_col);
flood(seed_x,seed_y-1,foreground_col,background_col);
}
}
void main()
{
int gd=DETECT,gm,gerror;
initgraph(&gd,&gm,"");
rectangle(50,50,100,100);
flood(55,55,15,15);
kgetch();
closegraph();
}
OUTPUT:
Practical 6
Aim :- Implement Mid-Point Circle GenerationAlgorithm.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circlept(int xc,int yc,int R);
void main()
{
int xc,yc,R;
int gdriver=DETECT,gmode,gerror;
clrscr();
printf("Enter the center of the circle:n");
printf("Xc =");
scanf("%d",&xc);
printf("Yc =");
scanf("%d",&yc);
printf("Enter the radius of the circle :");
scanf("%d",&R);
clrscr();
initgraph(&gdriver,&gmode,"");
circlept(xc,yc,R);
getch();
closegraph();
}
void circlept(int xc,int yc,int r)
{
int x=0,y=r,p=(5/4)-r;
clrscr();
while(x<y)
{
if(p<0)
{
x=x+1;
y=y;
p=p+(2*x)+1;
}
else
{
x=x+1;
y=y-1;
p=p+(2*x)+1-(2*y);
}
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc-y,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);
putpixel(xc+y,yc+x,15);
putpixel(xc-y,yc+x,15);
}
}
OUTPUT:
k
Practical 7
Aim :- Implement Mid-Point Ellipse GenerationAlgorithm.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define round(a) ((int)(a+0.5))
void main()
{
int gdriver=DETECT,gmode;
float d1,d2k,rx,ry,dx,dy;
int x,y;
clrscr();
initgraph(&gdriver,&gmode,"");
cleardevice();
printf("n Enter rx & ry : ");
scanf("%f %f",&rx,&ry);
x=0;
y=ry;
d1=round((ry*ry)-(rx*rx*ry)+(0.25*rx*rx));
dx=2*ry*ry*x;
dy=2*rx*rx*y;
do
{
putpixel(100+x,100+y,15);
putpixel(100-x,100-y,15);
putpixel(100+x,100-y,15);
putpixel(100-x,100+y,15);
if(d1<0)
{
x=x+1;
y=y;
dx=dx+(2*ry*ry);
d1=d1+dx+(ry*ry);
}
else
{
x=x+1;
y=y-1;
dx=dx+(2*ry*ry);
dy=dy-(2*rx*rx);
d1=d1+dx-dy+(ry*ry);
}
delay(10);
}
while(dx<dy);
d2=round((ry*ry)*(x+0.5)*(x+0.5)+(rx*rx)*(y-1)*(y-1)-(rx*rx)*(ry*ry));
d2=(int)(d2+0.5);
do
{
putpixel(100+x,100+y,15);
putpixel(100-x,100-y,15);
putpixel(100+x,100-y,15);
putpixel(100-x,100+y,15);
if(d2>0)
{
x=x;
y=y-1;
dy=dy-(2*rx*rx);
d2=d2-dy+(rx*rx);
}
else
{
x=x+1;
y=y-1;
dy=dy-(2*rx*rx);
dx=dx+(2*ry*ry);
d2=d2+dx-dy+(rx*rx);
}
}
while(y>0);
getch();
closegraph();}
OUTPUT:
Practical : 8
Aim :-Generate Bar Chart And Pie Chart.
[A] Bar Chart
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setcolor(WHITE);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"Bar Chart");
setlinestyle(SOLID_LINE,0,2);
line(100,420,100,60);
line(100,420,600,420);
line(90,70,100,60);
line(110,70,100,60);
line(590,410,600,420);
line(590,430,600,420);
outtextxy(95,35,"Y");
outtextxy(610,405,"X");
outtextxy(85,415,"O");
setfillstyle(LINE_FILL, WHITE);
bar(150,100,200,419);
setfillstyle(XHATCH_FILL, WHITE);
bar(225,150,275,419);
setfillstyle(WIDE_DOT_FILL, WHITE);
bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL, WHITE);
bar(375,125,425,419);
setfillstyle(HATCH_FILL, WHITE);
bar(450,175,500,419);
getch();
}
OUTPUT:
[B] Pie Chart
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm, midx, midy;
initgraph(&gd, &gm, "");
setcolor(WHITE);
rectangle(0,40,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,10,"Pie Chart");
midx = getmaxx()/2;
midy = getmaxy()/2;
setfillstyle(LINE_FILL, WHITE);
pieslice(midx, midy, 0, 75, 100);
outtextxy(midx+100, midy - 75, "20.83%");
setfillstyle(XHATCH_FILL, WHITE);
pieslice(midx, midy, 75, 225, 100);
outtextxy(midx-175, midy - 75, "41.67%");
setfillstyle(WIDE_DOT_FILL, WHITE);
pieslice(midx, midy, 225, 360, 100);
outtextxy(midx+75, midy + 75, "37.50%");
getch();
}
OUTPUT:
Practical: 9
Aim :2D Transformation -translation, rotation, scaling, fixed point
rotation, fixed point- scaling.
TRANSLATION:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float ans[3][10];
void multi(float tmat[3][3],float coormat[3][10],int size);
void main()
{
float coormat[3][10],x[10],y[10];
int gd=DETECT,gm,no,tx,ty,sum=0,i,j,k;
float tmat[3][3]={{1,0,0},{0,1,0},{0,0,1}};
clrscr();
initgraph(&gd,&gm,"..bgi");
printf("enter (tx,ty):"),scanf("%d%d",&tx,&ty);
tmat[0][2]=tx;
tmat[1][2]=ty;
printf("Translation matrix is:n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%4.1ft",tmat[i][j]);
printf("n");
}
printf("enter no. of vertices:n"),scanf("%d",&no);
for(i=0;i<no;i++)
printf("enter (x%d,y%d):n",i+1,i+1),scanf("%f%f",&x[i],&y[i]);
for(i=0;i<no;i++)
coormat[0][i]=x[i];
for(i=0;i<no;i++)
coormat[1][i]=y[i];
for(i=0;i<no;i++)
coormat[2][i]=1;
printf("co ordinate matrix is:n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2ft",coormat[i][j]);
printf("n");
}//printing starting line
for(j=0;j<no-1;j++)
line(coormat[0][j],coormat[1][j],coormat[0][j+1],coormat[1][j+1]);
line(coormat[0][0],coormat[1][0],coormat[0][j],coormat[1][j]);
multi(tmat,coormat,no);
printf("answer matrix is:n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2ft",ans[i][j]);
printf("n");
}
for(j=0;j<no-1;j++)
line(ans[0][j],ans[1][j],ans[0][j+1],ans[1][j+1]);
line(ans[0][0],ans[1][0],ans[0][j],ans[1][j]);
getch();
}
void multi(float tmat[3][3],float coormat[3][10],int no)
{
int i,j,k,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum+=tmat[i][k]*coormat[k][j];
}
ans[i][j]=sum;
}
}
}
OUTPUT:
ROTATITON:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float ans[3][10];
void multi(float tmat[3][3],float coormat[3][10],int size);
void main()
{
float coormat[3][10],x[10],y[10],temp;
int gd=DETECT,gm,no,sx,sy,sum=0,i,j,k,theta;
float tmat[3][3]={{1,0,0},{0,1,0},{0,0,1}};
clrscr();
initgraph(&gd,&gm,"..bgi");
printf("enter theta:"),scanf("%d",&theta);
temp=(float)theta*3.14/180;
tmat[0][0]=cos(temp);
tmat[1][1]=cos(temp);
tmat[0][1]=-1*sin(temp);
tmat[1][0]=-1*sin(temp);
printf("Translation matrix is:n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%4.1ft",tmat[i][j]);
printf("n");
}
printf("enter no. of vertices:n"),scanf("%d",&no);
for(i=0;i<no;i++)
printf("enter (x%d,y%d):n",i+1,i+1),scanf("%f%f",&x[i],&y[i]);
for(i=0;i<no;i++)
coormat[0][i]=x[i];
for(i=0;i<no;i++)
coormat[1][i]=y[i];
for(i=0;i<no;i++)
coormat[2][i]=1;
printf("co ordinate matrix is:n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2ft",coormat[i][j]);
printf("n");
}
//printing starting line
for(j=0;j<no-1;j++)
line(coormat[0][j],coormat[1][j],coormat[0][j+1],coormat[1][j+1]);
line(coormat[0][0],coormat[1][0],coormat[0][j],coormat[1][j]);
multi(tmat,coormat,no);
printf("answer matrix is:n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2ft",ans[i][j]);
printf("n");
}
for(j=0;j<no-1;j++)
line(ans[0][j],ans[1][j],ans[0][j+1],ans[1][j+1]);
line(ans[0][0],ans[1][0],ans[0][j],ans[1][j]);
getch();
}
void multi(float tmat[3][3],float coormat[3][10],int no)
{
int i,j,k,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum+=tmat[i][k]*coormat[k][j];
}
ans[i][j]=sum;
}
}
}
OUTPUT:
SCALING:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float ans[3][10];
void multi(float tmat[3][3],float coormat[3][10],int size);
void main()
{
float coormat[3][10],x[10],y[10];
int gd=DETECT,gm,no,sx,sy,sum=0,i,j,k;
float tmat[3][3]={{1,0,0},{0,1,0},{0,0,1}};
clrscr();
initgraph(&gd,&gm,"..bgi");
printf("enter (sx,sy):"),scanf("%d%d",&sx,&sy);
tmat[0][0]=sx;
tmat[1][1]=sy;
printf("Translation matrix is:n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%4.1ft",tmat[i][j]);
printf("n");
}
printf("enter no. of vertices:n"),scanf("%d",&no);
for(i=0;i<no;i++)
printf("enter (x%d,y%d):n",i+1,i+1),scanf("%f%f",&x[i],&y[i]);
for(i=0;i<no;i++)
coormat[0][i]=x[i];
for(i=0;i<no;i++)
coormat[1][i]=y[i];
for(i=0;i<no;i++)
coormat[2][i]=1;
printf("co ordinate matrix is:n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2ft",coormat[i][j]);
printf("n");
} //printing starting line
for(j=0;j<no-1;j++)
line(coormat[0][j],coormat[1][j],coormat[0][j+1],coormat[1][j+1]);
line(coormat[0][0],coormat[1][0],coormat[0][j],coormat[1][j]);
multi(tmat,coormat,no);
printf("answer matrix is:n");
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
printf("%4.2ft",ans[i][j]);
printf("n"); }
for(j=0;j<no-1;j++)
line(ans[0][j],ans[1][j],ans[0][j+1],ans[1][j+1]);
line(ans[0][0],ans[1][0],ans[0][j],ans[1][j]);
getch();
}
void multi(float tmat[3][3],float coormat[3][10],int no)
{
int i,j,k,sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<no;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum+=tmat[i][k]*coormat[k][j];
}
ans[i][j]=sum;
}
}
}
OUTPUT:
2D SCALLING AND ROTATION WITH FIXED POINT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define PI 22/7
float mat2[3][3]={{1,0,0},{0,1,0},{0,0,1}};
float sx,sy,angle,mat1[10][3],x[10],y[10];
int i,j,n;
void pivoting(float,float);
void pivot_scaling();
void pivot_rotation();
void mat_multi();
void identity();
void main()
{
int gd=DETECT,gm,ch;
float px,py;
clrscr();
initgraph(&gd,&gm,"c:tcbgi");
printf("No. of Vertices: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" x[%d] y[%d]--> ",(i+1),(i+1));
scanf("%f%f",&x[i],&y[i]);
mat1[i][0]=x[i];
mat1[i][1]=y[i];
mat1[i][2]=1;
}
for(i=0;i<n;i++) {
if(i==(n-1))
line(mat1[i][0],mat1[i][1],mat1[0][0],mat1[0][1]);
else
line(mat1[i][0],mat1[i][1],mat1[i+1][0],mat1[i+1][1]); }
getch();
printf("nn1:Scaling With Pivot Pointn2:Rotation With Pivot PointnYour
Choise -->");
scanf("%d",&ch);
switch(ch)
{
case 1:
scanf("%f",&px, printf("Pivot Point for X--> "));
scanf("%f",&py, printf("Pivot Point for Y--> "));
pivoting(-px,-py);
identity();
pivot_scaling();
identity();
pivoting(px,py);
printf("Afterr Scalling Matrix --> n");
for(i=0;i<n;i++) {
for(j=0;j<3;j++)
printf(" %5.2f ",mat1[i][j]);
printf("n"); }
for(i=0;i<n;i++) {
if(i==(n-1))
line(mat1[i][0],mat1[i][1],mat1[0][0],mat1[0][1]);
else
line(mat1[i][0],mat1[i][1],mat1[i+1][0],mat1[i+1][1]);
}
break;
case 2:
scanf("%f",&px, printf("Pivot Point for X--> "));
scanf("%f",&py, printf("Pivot Point for Y--> "));
pivoting(-px,-py);
identity();
pivot_rotation();
identity();
pivoting(px,py);
printf("After Rotation Matrix--> n");
for(i=0;i<n;i++) {
for(j=0;j<3;j++)
printf(" %5.2f ",mat1[i][j]);
printf("n"); }
for(i=0;i<n;i++) {
if(i==(n-1))
line(mat1[i][0],mat1[i][1],mat1[0][0],mat1[0][1]);
else
line(mat1[i][0],mat1[i][1],mat1[i+1][0],mat1[i+1][1]); }
break;
default:
printf("Wrong Choice!");
}
getch();
}
void identity()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(i==j)
mat2[i][j]=1;
else
mat2[i][j]=0;
}
}
void mat_multi()
{
int i,j,k,m;
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
m=0;
for(k=0;k<3;k++)
m+=mat1[i][k]*mat2[k][j];
mat1[i][j]=m;
}
}
}
void pivoting(float px,float py)
{
mat2[2][0]=px;
mat2[2][1]=py;
mat_multi();
}
void pivot_scaling()
{
scanf("%f",&sx, printf("Scaling Factor for X-direction--> "));
scanf("%f",&sy, printf("Scaling Factor for Y-direction--> "));
mat2[0][0]=sx;
mat2[1][1]=sy;
mat_multi();
}
void pivot_rotation()
{
printf("Rotation Angle--> ");
scanf("%f",&angle);
angle=(PI*angle/180);
mat2[0][0]=cos(angle);
mat2[1][1]=cos(angle);
mat2[0][1]=sin(angle);
mat2[1][0]=-1*sin(angle);
mat_multi();
}
OUTPUT :
Practical: 10
Aim: 2D Transformation- 2D Reflection, 2D Shearing.
REFLECTION :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
float rmat[3][3],cor[3][10];
int n;
void mul(float c[3][10], float c1[3][3], int n);
void polygon(float cor[3][10], int n);
void id(float rmat[3][3]);
void id(float rmat[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
rmat[i][j]=1;
else
rmat[i][j]=0;
}}}
void polygon(float cor[3][10],int n)
{
int j;
cor[0][n]=cor[0][0];
cor[1][n]=cor[1][0];
for(j=0;j<n;j++)
line(320+cor[0][j],240-cor[1][j],320+cor[0][j+1],240-cor[1][j+1]);
}
void mul(float c[3][10], float c1[3][3],int n)
{
float ans[3][10];
int i,j,k;
float sum;
for(i=0;i<3;i++)
{
for(j=0;j<n;j++)
{
sum=0;
for(k=0;k<3;k++)
sum=sum+c1[i][k]*c[k][j];
ans[i][j]=sum;
}}
polygon(ans,n);
}
void main()
{
int ch,gd,gm,x,y,i,j;
gd=DETECT;
initgraph(&gd,&gm,"d:tcbgi");
cleardevice();
printf("ENTER NUMBER OF VERTICES-->>");
scanf("%d",&n);
printf("nENTER CO-ORDINATE VALUES -->>");
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
if(j==2)
cor[j][i]=1;
else
scanf("%f",&cor[j][i]);
}}
label:
moveto(0,0);
getch();
cleardevice();
line(0,240,640,240);
line(320,0,320,480);
polygon(cor,n);
printf("1.REFLECTION ABOUT X-AXIS n2.REFLECTION ABOUT Y-
AXIS n3.REFLECTION ABOUT BOTH AXISn4.REFLECTION AT
Y=Xn5.REFLECTION ABOUT Y=-Xn6.EXIT");
printf("nENTER YOUR CHOICE-->>");
scanf("%d",&ch);
switch(ch)
{
case 1:
id(rmat);
rmat[1][1]=-1;
mul(cor,rmat,n);
break;
case 2:
id(rmat);
rmat[0][0]=-1;
mul(cor,rmat,n);
break;
case 3:
id(rmat);
rmat[0][0]=-1;
rmat[1][1]=-1;
mul(cor,rmat,n);
break;
case 4:
id(rmat);
rmat[0][0]=0;
rmat[1][0]=1;
rmat[0][1]=1;
rmat[1][1]=0;
mul(cor,rmat,n);
break;
case 5:
id(rmat);
rmat[0][0]=0;
rmat[1][0]=-1;
rmat[0][1]=-1;
rmat[1][1]=0;
mul(cor,rmat,n);
break;
case 6:
exit(0);
}
getch();
}
OUTPUT:
SHEARING:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
float smat[3][3],cor[3][10];
int n;
void mul(float c[3][10], float c1[3][3], int n);
void polygon(float cor[3][10], int n);
void id(float smat[3][3]);
void polygon(float cor[3][10],int n)
{
int j;
cor[0][n]=cor[0][0];
cor[1][n]=cor[1][0];
for(j=0;j<n;j++)
line(cor[0][j],cor[1][j],cor[0][j+1],cor[1][j+1]);
}
void mul(float c[3][10], float c1[3][3],int n)
{
float ans[3][10];
int i,j,k;
float sum;
for(i=0;i<3;i++)
{
for(j=0;j<n;j++)
{
sum=0;
for(k=0;k<3;k++)
sum=sum+c1[i][k]*c[k][j];
ans[i][j]=sum;
}
}
polygon(ans,n);
}
void id(float smat[3][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
smat[i][j]=1;
else
smat[i][j]=0;
}
}
}
void main()
{
int ch,gd=DETECT,gm,x,y,i,j;
float sh,ref;
initgraph(&gd,&gm,"d:tcbgi");
cleardevice();
printf("enter no. of vertices:");
scanf("%d",&n);
printf("nenter co-ord. values:");
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
if(j==2)
cor[j][i]=1;
else
scanf("%f",&cor[j][i]);
}
}
label:getch();
cleardevice();
polygon(cor,n);
while(1)
{
printf("1.SHEARING WITH X-REF n2.SHERAING WITH Y-
REFn3.EXIT");
printf("nENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("nENTER SHX & Y-REF-->>:");
scanf("%f %f",&sh,&ref);
id(smat);
smat[0][1]=sh;
smat[0][2]=(-1)*sh*ref;
mul(cor,smat,n);
goto label;
break;
case 2:
printf("nENTER VALUES FOR SHY & Y-REF-->>");
scanf("%f %f",&sh,&ref);
id(smat);
smat[1][0]=sh;
smat[1][2]=(-1)*sh*ref;
mul(cor,smat,n);
goto label;
break;
case 3:
exit(0);
}
getch();
}
}
OUTPUT:
Practical : 11
Aim :-Implement Cohen-Sutherland Line Clipping Algorithm.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
int w1,w2,w3,w4;
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);
void main()
{
int gd=DETECT, gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm," ");
cleardevice();
printf("nnttENTER END-POINT 1 (x,y): ");
scanf("%d,%d",&p1.x,&p1.y);
printf("nnttENTER END-POINT 2 (x,y): ");
scanf("%d,%d",&p2.x,&p2.y);
printf("nnttENTER WINDOW (left,top,right,bottom) : ");
scanf("%d,%d,%d,%d",&w1,&w2,&w3,&w4);
cleardevice();
drawwindow();
getch();
drawline(p1,p2,15);
getch();
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0: cleardevice();
drawwindow();
drawline(p1,p2,15);
break;
case 1: cleardevice();
drawwindow();
break;
case 2: cleardevice();
p1=resetendpt (p1,p2);
p2=resetendpt(p2,p1);
drawwindow();
drawline(p1,p2,15);
break;
}
getch();
closegraph();
}
void drawwindow()
{
setcolor(WHITE);
rectangle(w1,w2,w3,w4);
}
void drawline (PT p1,PT p2,int cl)
{
setcolor(cl);
line(p1.x,p1.y,p2.x,p2.y);
}
PT setcode(PT p)
{
PT ptemp;
if(p.y<w2)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>w4)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if (p.x>w3)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if (p.x<w1)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
int visibility (PT p1,PT p2)
{
int i,flag=0;
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);
}
PT resetendpt (PT p1,PT p2)
{
PT temp;
int x,y,i;
float m,k;
if( p1.code[3]=='1')
x=w1;
if(p1.code[2]=='1')
x=w3;
if((p1.code[3]=='1')||(p1.code[2]=='1'))
{
m=(float) (p2.y-p1.y)/(p2.x-p1.x);
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<=w4&&temp.y>=w2)
return(temp);
}
if(p1.code[0]=='1')
y=w2;
if(p1.code [1]=='1')
y=w4;
if((p1.code[0]=='1')||(p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
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);
}
OUTPUT:
(1) Entering line end points and window corners.
(2) Window is drawn using given window corners
(3) Line is drawn using given end points.
(4) Line is clipped .
Practical : 12
Aim :-Implement Sutherland-Hodgeman Polygon Clipping
Algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
typedef struct
{
float x;
float y;
}PT;
int n;
void left(PT p1,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].x < p1.x && p[i+1].x >= p1.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p1.x-p[i].x)+p[i].y;
}
else
{
pp[j].y = p[i].y;
}
pp[j].x = p1.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].x > p1.x && p[i+1].x >= p1.x)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].x > p1.x && p[i+1].x <= p1.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p1.x-p[i].x)+p[i].y;
}
else
{
pp[j].y = p[i].y;
}
pp[j].x = p1.x;
j++;
} }
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
n=j;
}
void right(PT p2,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].x > p2.x && p[i+1].x <= p2.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p2.x-p[i].x)+p[i].y;
}
else
{
pp[j].y = p[i].y;
}
pp[j].x = p2.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].x < p2.x && p[i+1].x <= p2.x)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].x < p2.x && p[i+1].x >= p2.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p2.x-p[i].x)+p[i].y;
}
else
{
pp[j].y = p[i].y;
}
pp[j].x = p2.x;
j++;
} }
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
n=j;
}
void top(PT p1,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].y < p1.y && p[i+1].y >= p1.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p1.y-p[i].y)+p[i].x;
}
else
{
pp[j].x = p[i].x;
}
pp[j].y = p1.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].y > p1.y && p[i+1].y >= p1.y)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].y > p1.y && p[i+1].y <= p1.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p1.y-p[i].y)+p[i].x;
}
else
{
pp[j].x = p[i].x;
}
pp[j].y = p1.y;
j++;
} }
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
n=j;
}
void bottom(PT p2,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].y > p2.y && p[i+1].y <= p2.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p2.y-p[i].y)+p[i].x;
}
else
{
pp[j].x = p[i].x;
}
pp[j].y = p2.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].y < p2.y && p[i+1].y <= p2.y)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].y < p2.y && p[i+1].y >= p2.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p2.y-p[i].y)+p[i].x;
}
else
{
pp[j].x = p[i].x;
}
pp[j].y = p2.y;
j++;
} }
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
n=j;
}
void drawpolygon(PT x[20],int n)
{
int i;
for(i=0;i<n-1;i++)
{
line(x[i].x,x[i].y,x[i+1].x,x[i+1].y);
}
line(x[i].x,x[i].y,x[0].x,x[0].y);
}
void main()
{
int i,j,gd=DETECT,gm;
PT d,p1,p2,p[20],pi1,pi2,pp[20];
initgraph(&gd,&gm,"");
printf("Enter coordinates (left,top) of point1 : ");
scanf("%f,%f",&p1.x,&p1.y);
printf("Enter coordinates (right,bottom) of point2 : ");
scanf("%f,%f",&p2.x,&p2.y);
printf("Enter the number of vertex : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter coordinates of vertex%d : ",i+1);
scanf("%f,%f",&p[i].x,&p[i].y);
}
p[i].x = p[0].x;
p[i].y = p[0].y;
cleardevice();
drawpolygon(p,n);
rectangle(p1.x,p1.y,p2.x,p2.y);
getch();
left(p1,p,pp);
right(p2,p,pp);
top(p1,p,pp);
bottom(p2,p,pp);
cleardevice();
rectangle(p1.x,p1.y,p2.x,p2.y);
drawpolygon(p,n);
getch();
closegraph();
}
OUTPUT:
(1) Entering window corners and vertices of polygon.
(2) Window is drawn using given window corners.polygonis drawn using
given vertices.
(3) Polygon is clipped against given window.
By- Snel Koli

Computer graphics

  • 1.
    Practical 1 Q1.Study oflibrary functions from graphics.h 1)INITGRAPH DECLARATION * void far initgraph(int far *graph driver) EXPLANATION * To start the graphic system, you must first call initgraph. *Initgraph initializes the graphic system by loading a graphics driver from disk (or validating a registered driver) then putting the system into graphics mode. *Initgraph also resets all graphics settings (color,palette,currentposition,viewport, etc) to their defaults then resets graph. 2)GETPIXEL, PUTPIXEL DECLARATION * Unsigned far getpixel(int x, int y) * void far putpixel(int x, int y, int color) EXPLANATION *Getpixel gets the color of the pixel located at (x,y) *Putpixel plots a point in the color defined at (x, y).
  • 2.
    3)CLOSE GRAPH DECLARATION *void farclosegraph(void); EXPLANATION *Close graph deallocates all memory allocated by the graphic system. *It then restores the screen to the mode it was in before you called initgraph. 4) ARC, CIRCLE, PIESLICE DECLARATION *void far arc(int x, int y, intstangle, intendangle, int radius); *void far circle(int x, int y, int radius); *void far pieslice(int x, int y, intstangle, intendangle, int radius); EXPLANATION *Arc draws a circular arc in the current drawing color *Circle draws a circle in the current drawing color *Pieslice draws a pieslice in the current drawing color, then fills it using the current fillpattern and fill color. 5) ELLIPSE, FILLELIPSE, SECTOR DECLARATION *void far ellipse(int x, int y, intstangle, intendangle, intxradius, intyradius) *void far fillellipse(int x, int y, intxradius, intyradius) *void farsectoe(int x, int y, intstangle,intendangle, intxradius, intyradius) EXPLANATION *Ellipse draws an elliptical arc in the current drawing color. *Fillellipse draws an elliptical arc in the current drawing color and than fills it with fill color and fill pattern. *Sector draws an elliptical pie slice in the current drawing color and than fills it using the pattern and color defined by set fillstyle or setfillpattern.
  • 3.
    6) FLOODFILL *Flood-fills abounded region. DECLARATION *void far floodfill(int x, int y, int border) EXPLANATION *Floodfills an enclosed area on bitmap device. *The area bounded by the colorborder is flooded with the current fill pattern and fill color. *If the seed is within an enclosedarea, the inside will be filled.If the seed is outside the enclosed area, the exterior will be filled. *Use fillpoly instead of floodfill wherever possible so you can maintain code compatibility with future versions. *Floodfilldoesnot work with the IBM-8514 driver. 7) GETCOLOR, SETCOLOR *Getcolor returns the current drawing color. *Setcolor returns the current drawing color. DECLARATION *int far getcolor(void); *void far setcolor(int color) EXPLANATION *Getcolor returns the current drawing color. *Setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. *To set a drawing color with setcolor ,you can pass either the color number or theequivalent color name.
  • 4.
    8) LINE, LINEREL,LINETO DECLARATION *void far lineto(int x, int y) EXPLANATION *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). *Linerel draws a line from the CP to a point that is relative distance (dx, dy) from the CP, then advances the CP by (dx, dy). *Lineto draws a line from the CP to (x, y), then moves the CP to (x,y). 9)RECTANGLE DECLARATION *void far rectangle(int left, int top, int right, int bottom) EXPLANATION *It draws a rectangle in the current line style, thickness and drawing color. *(left, top)is the upperleft cornerofthe rectangle, and (right, bottom)is its lower right corner. 10)MOVEREL,MOVETO DECLARATION *void moverel(int x, int y); *void moveto(int x, int y); EXPLANATION *moveto function changes the current position (CP) to (x, y) *moverel function moves the current position to a relative distance. 11)IMAGESIZE,GETIMAGE,PUTIMAGE DECLARATION
  • 5.
    *intimagesize(int left, inttop, int right, int bottom); *void getimage(int left, int top, int right, int bottom, void *bitmap); *void putimage(int left, int top, void *ptr, int op); EXPLANATION *imagesize function returns the number of bytes required to store a bitimage. This function is used when we are using getimage. *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. *putimage function outputs a bit image onto the screen. *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 correspondingsource pixel in memory. 11)GETX,GETY DECLARATION *intgetx(); * intgety(); EXPLANATION *getx function returns the X coordinate of current position. *gety function returns the y coordinate of current position. 12)GETMAXX,GETMAXY DECLARATION *intgetmaxx(); *intgetmaxy(); EXPLANATION *getmaxy function returns the maximum Y coordinate for current graphics mode and driver.
  • 6.
    *getmaxx function returnsthe maximum X coordinate for current graphics mode and driver. 13)FILLPOLY,DRAWPOLY DECLARATION *void drawpoly( intnum, int *polypoints ); *void drawpoly( intnum, int *polypoints ); EXPLANATION *Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc. *Fillpoly function draws and fills a polygon. It require same arguments as drawpoly. *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. *To understand more clearly we will draw a triangle using drawpoly, consider for example the array :- int points[] = { 320, 150, 420, 300, 250, 300, 320, 150}; points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note that last point(320, 150) in array is same as first. 14)SETLINESTYLE DECLARATION *void setlinestyle( intlinestyle, unsigned upattern, int thickness ); EXPLANATION *ItdrawsSOLID_LINE,DOTTED_LINE,CENTER_LINE,DASHED_LINE,US ERBIT_LINE. 15)GETBKCOLOR,SETBKCOLOR DECLARATION *intgetbkcolor(); * void setbkcolor(intcolor); EXPLANATION *getbkcolor function returns the current background color *setbkcolor function changes current background color e.g. setbkcolor(YELLLOW) changes the current background color to YELLOW.
  • 7.
    Remember that defaultdrawing color is WHITE and background color is BLACK. 16)GETMODENAME DECLARATION *char * far getmodename(int mode_number); EXPLNATION *Returns the name of a specified graphics mode *getmodename accepts a graphics mode number as input and returns a stringcontaining the name of the corresponding graphics mode. *The mode names are embedded in each driver.
  • 8.
    Q2.Write a programto draw a rectangle. CODE #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int left, top, right, bottom; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode= graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %sn", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code*/ } left = getmaxx() / 2 - 50; top = getmaxy() / 2 - 50; right = getmaxx() / 2 + 50;
  • 9.
    bottom = getmaxy()/ 2 + 50; rectangle(50,50,100,100); /* draw a rectangle */ /* clean up */ getch(); closegraph(); return 0; } OUTPUT:
  • 10.
    Q3.Write a programto draw a smiley. Code: #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int midx, midy; int radius = 100; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "C:TurboC++DiskTurboC3BIN"); /* read result of initialization */ errorcode= graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %sn", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code*/ }
  • 11.
    midx = getmaxx()/ 2; midy = getmaxy() / 2; setcolor(getmaxcolor()); /* draw the circle */ circle(midx, midy, radius); circle(midx-50, midy-20, 20); circle(midx+50, midy-20, 20); line(midx, midy+20, midx, midy-10); arc(midx, midy+20, 225, 315, 50); /* clean up */ getch(); closegraph(); return 0; }
  • 12.
  • 13.
    Q3.Write a programto draw a star. CODE: #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int left, top, right, bottom; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "C:turboc3bgi"); /* read result of initialization */ errorcode= graphresult(); if (errorcode != grOk) /* an error occurred */ { printf("Graphics error: %sn", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code*/ } moveto(60,90); lineto(100,150);
  • 14.
  • 15.
  • 16.
    Practical 2 Aim :-Implement DDA Line Drawing Algorithm CODE: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void draw(int x1,int y1,int x2,int y2); void main() { int x1,y1,x2,y2; int gdriver=DETECT,gmode,gerror; printf(“n Enter the x and y value for starting point:n”); scanf(“%d%d”,&x1,&y1); printf(“n Enter the x and y value for ending point:n”); scanf(“%d%d”,&x2,&y2); clrscr(); initgraph(&gdriver,&gmode,””); draw(x1,y1,x2,y2); getch(); closegraph(); } void draw(int x1,int y1,int x2,int y2) { float x,y,xinc,yinc,dx,dy;
  • 17.
  • 18.
  • 19.
    Practical 3 Aim :-Implement Bresenham's Line Drawing Algorithm CODE: # include <stdio.h> # include <conio.h> # include <graphics.h> void main() { int dx,dy,x,y,e,x1,y1,x2,y2; int gd,gm; clrscr(); printf("nntEnter the co-ordinates of first point : ");
  • 20.
    scanf("%d %d",&x1,&y1); printf("nntEnter theco-ordinates of second point : "); scanf("%d %d",&x2,&y2); dx=(x2 - x1); dy=(y2 - y1); e=2*(dy)-(dx); x=x1; y=y1; detectgraph(&gd,&gm); initgraph(&gd,&gm,"e:tcbgi"); putpixel(x,y,WHITE); while(x<=x2) { if(e<0) { x=x+1; e=e+2*(dy); } else { x=x+1; y=y+1; e=e+2*(dy-dx); } putpixel(x,y,15); } getch(); closegraph(); }
  • 21.
  • 22.
  • 23.
    Aim: Write aprogram to draw dash, dotted and thick line. CODE: #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ intgdriver = DETECT, gmode, errorcode; float wx,wy,x1,y1,x2,y2; inti,thicknkess; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode= graphresult(); /* an error occurred */ if (errorcode != grOk) k { printf("Graphics error: %sn", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } printf("enter the co-ordinates:"); printf("nx1:"); scanf("%f",&x1); printf("y1:"); scanf("%f",&y1); printf("x2:"); scanf("%f",&x2); printf("y2:"); scanf("%f",&y2); printf("enter the required thickness:"); scanf("%d",&thickness);
  • 24.
  • 25.
  • 26.
    #include <graphics.h> #include <stdlib.h> #include<stdio.h> #include <conio.h> int sign(inta,int b) { if(a>b) return 1; if(a<b) return -1; if(a==b) return 0; } void main() { intgdriver = DETECT, gmode, errorcode; int x1,y1,x2,y2,dx,dy,p,i,x,y,s1,s2; intlg,ld,g=0,d=0; initgraph(&gdriver, &gmode, ""); printf("n Enter value of x1 & y1 ::"); scanf("%d %d",&x1,&y1); printf("nk Enter value of x2 & y2 ::"); scanf("%d %d",&x2,&y2); printf("n Enter the length of dot:"); scanf("%d", &ld); printf("n Enter the length of gap :"); scanf("%d", &lg); dx=abs(x2-x1); dy=abs(y2-y1); p=2*dy - dx; x=x1; y=y1; s1=sign(x2,x1); s2=sign(y2,y1); putpixel(x,y,3); for(i=0;i<=dx;i++) { if(p<0)
  • 27.
  • 29.
    Practical 5 Aim :-Implement PolygonFilling Algorithms. [A] Boundary Fill CODE: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> void drawply(); void boundryfill(int x1, int x2, int y1, int y2); main() { int gd=DETECT,gm,x,y,p[8],x1,x2,y1,y2; initgraph(&gd,&gm," "); cleardevice(); drawply(); getch(); x1=300; x2=371; y1=240; y2=170;
  • 30.
    while(x1!=x2&&y1!=y2) { x1++; x2--; y1--; y2++; boundryfill(x1,x2,y1,y2); } outtextxy(300,50,"Boundry fill"); getch(); closegraph(); return(0); } void drawply() { setcolor(WHITE); line(300,240,371,240); line(371,240,371,170); line(371,170,300,170); line(300,170,300,240); } void boundryfill(int x1, int x2, int y1, int y2) { int i; for(i=x2;i>=x1;i--) { putpixel(i,y1,WHITE); delay(0.5); } for(i=y1; i>=y2;i--) { putpixel(x1,i,WHITE); delay(0.5); } for(i=x1;i<=x2;i++)
  • 31.
  • 32.
    [B] Flood Fill CODE: #include<stdio.h> #include<conio.h> #include<graphics.h> voidflood(int seed_x,int seed_y,int foreground_col,int background_col) { if (getpixel(seed_x,seed_y)!= background_col&& getpixel(seed_x,seed_y)!= foreground_col) { putpixel(seed_x,seed_y,foreground_col); flood(seed_x+1,seed_y,foreground_col,background_col); flood(seed_x-1,seed_y,foreground_col,background_col); flood(seed_x,seed_y+1,foreground_col,background_col); flood(seed_x,seed_y-1,foreground_col,background_col); } } void main() { int gd=DETECT,gm,gerror; initgraph(&gd,&gm,""); rectangle(50,50,100,100); flood(55,55,15,15); kgetch(); closegraph();
  • 33.
  • 34.
    Practical 6 Aim :-Implement Mid-Point Circle GenerationAlgorithm. CODE: #include<stdio.h> #include<conio.h> #include<graphics.h> void circlept(int xc,int yc,int R); void main() { int xc,yc,R; int gdriver=DETECT,gmode,gerror; clrscr(); printf("Enter the center of the circle:n"); printf("Xc ="); scanf("%d",&xc); printf("Yc ="); scanf("%d",&yc);
  • 35.
    printf("Enter the radiusof the circle :"); scanf("%d",&R); clrscr(); initgraph(&gdriver,&gmode,""); circlept(xc,yc,R); getch(); closegraph(); } void circlept(int xc,int yc,int r) { int x=0,y=r,p=(5/4)-r; clrscr(); while(x<y) { if(p<0) { x=x+1; y=y; p=p+(2*x)+1; } else { x=x+1; y=y-1; p=p+(2*x)+1-(2*y); } putpixel(xc+x,yc-y,15); putpixel(xc-x,yc-y,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); putpixel(xc+y,yc+x,15); putpixel(xc-y,yc+x,15); } }
  • 36.
  • 38.
    Practical 7 Aim :-Implement Mid-Point Ellipse GenerationAlgorithm. CODE: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #define round(a) ((int)(a+0.5)) void main() { int gdriver=DETECT,gmode; float d1,d2k,rx,ry,dx,dy; int x,y; clrscr(); initgraph(&gdriver,&gmode,""); cleardevice(); printf("n Enter rx & ry : "); scanf("%f %f",&rx,&ry); x=0; y=ry; d1=round((ry*ry)-(rx*rx*ry)+(0.25*rx*rx)); dx=2*ry*ry*x; dy=2*rx*rx*y; do { putpixel(100+x,100+y,15); putpixel(100-x,100-y,15); putpixel(100+x,100-y,15); putpixel(100-x,100+y,15); if(d1<0) { x=x+1; y=y; dx=dx+(2*ry*ry);
  • 39.
  • 41.
    Practical : 8 Aim:-Generate Bar Chart And Pie Chart. [A] Bar Chart #include<stdio.h> #include<graphics.h> #include<conio.h> void main() { int gd = DETECT, gm; initgraph(&gd, &gm, ""); setcolor(WHITE); rectangle(0,30,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,0,"Bar Chart"); setlinestyle(SOLID_LINE,0,2); line(100,420,100,60); line(100,420,600,420); line(90,70,100,60); line(110,70,100,60); line(590,410,600,420); line(590,430,600,420); outtextxy(95,35,"Y"); outtextxy(610,405,"X"); outtextxy(85,415,"O"); setfillstyle(LINE_FILL, WHITE); bar(150,100,200,419);
  • 42.
    setfillstyle(XHATCH_FILL, WHITE); bar(225,150,275,419); setfillstyle(WIDE_DOT_FILL, WHITE); bar(300,200,350,419); setfillstyle(INTERLEAVE_FILL,WHITE); bar(375,125,425,419); setfillstyle(HATCH_FILL, WHITE); bar(450,175,500,419); getch(); }
  • 43.
  • 44.
    [B] Pie Chart #include<stdio.h> #include<graphics.h> #include<conio.h> voidmain() { int gd = DETECT, gm, midx, midy; initgraph(&gd, &gm, ""); setcolor(WHITE); rectangle(0,40,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,10,"Pie Chart"); midx = getmaxx()/2; midy = getmaxy()/2; setfillstyle(LINE_FILL, WHITE); pieslice(midx, midy, 0, 75, 100); outtextxy(midx+100, midy - 75, "20.83%"); setfillstyle(XHATCH_FILL, WHITE); pieslice(midx, midy, 75, 225, 100); outtextxy(midx-175, midy - 75, "41.67%"); setfillstyle(WIDE_DOT_FILL, WHITE); pieslice(midx, midy, 225, 360, 100); outtextxy(midx+75, midy + 75, "37.50%"); getch(); }
  • 45.
  • 46.
    Practical: 9 Aim :2DTransformation -translation, rotation, scaling, fixed point rotation, fixed point- scaling. TRANSLATION: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> float ans[3][10]; void multi(float tmat[3][3],float coormat[3][10],int size); void main() { float coormat[3][10],x[10],y[10]; int gd=DETECT,gm,no,tx,ty,sum=0,i,j,k; float tmat[3][3]={{1,0,0},{0,1,0},{0,0,1}}; clrscr(); initgraph(&gd,&gm,"..bgi"); printf("enter (tx,ty):"),scanf("%d%d",&tx,&ty); tmat[0][2]=tx; tmat[1][2]=ty; printf("Translation matrix is:n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%4.1ft",tmat[i][j]); printf("n"); } printf("enter no. of vertices:n"),scanf("%d",&no); for(i=0;i<no;i++) printf("enter (x%d,y%d):n",i+1,i+1),scanf("%f%f",&x[i],&y[i]); for(i=0;i<no;i++) coormat[0][i]=x[i]; for(i=0;i<no;i++) coormat[1][i]=y[i]; for(i=0;i<no;i++) coormat[2][i]=1; printf("co ordinate matrix is:n");
  • 47.
    for(i=0;i<3;i++) { for(j=0;j<no;j++) printf("%4.2ft",coormat[i][j]); printf("n"); }//printing starting line for(j=0;j<no-1;j++) line(coormat[0][j],coormat[1][j],coormat[0][j+1],coormat[1][j+1]); line(coormat[0][0],coormat[1][0],coormat[0][j],coormat[1][j]); multi(tmat,coormat,no); printf("answermatrix is:n"); for(i=0;i<3;i++) { for(j=0;j<no;j++) printf("%4.2ft",ans[i][j]); printf("n"); } for(j=0;j<no-1;j++) line(ans[0][j],ans[1][j],ans[0][j+1],ans[1][j+1]); line(ans[0][0],ans[1][0],ans[0][j],ans[1][j]); getch(); } void multi(float tmat[3][3],float coormat[3][10],int no) { int i,j,k,sum=0; for(i=0;i<3;i++) { for(j=0;j<no;j++) { sum=0; for(k=0;k<3;k++) { sum+=tmat[i][k]*coormat[k][j]; } ans[i][j]=sum; } } }
  • 48.
  • 49.
    ROTATITON: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> float ans[3][10]; void multi(floattmat[3][3],float coormat[3][10],int size); void main() { float coormat[3][10],x[10],y[10],temp; int gd=DETECT,gm,no,sx,sy,sum=0,i,j,k,theta; float tmat[3][3]={{1,0,0},{0,1,0},{0,0,1}}; clrscr(); initgraph(&gd,&gm,"..bgi"); printf("enter theta:"),scanf("%d",&theta); temp=(float)theta*3.14/180; tmat[0][0]=cos(temp); tmat[1][1]=cos(temp); tmat[0][1]=-1*sin(temp); tmat[1][0]=-1*sin(temp); printf("Translation matrix is:n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%4.1ft",tmat[i][j]); printf("n"); } printf("enter no. of vertices:n"),scanf("%d",&no); for(i=0;i<no;i++) printf("enter (x%d,y%d):n",i+1,i+1),scanf("%f%f",&x[i],&y[i]); for(i=0;i<no;i++) coormat[0][i]=x[i]; for(i=0;i<no;i++) coormat[1][i]=y[i]; for(i=0;i<no;i++) coormat[2][i]=1; printf("co ordinate matrix is:n");
  • 50.
    for(i=0;i<3;i++) { for(j=0;j<no;j++) printf("%4.2ft",coormat[i][j]); printf("n"); } //printing starting line for(j=0;j<no-1;j++) line(coormat[0][j],coormat[1][j],coormat[0][j+1],coormat[1][j+1]); line(coormat[0][0],coormat[1][0],coormat[0][j],coormat[1][j]); multi(tmat,coormat,no); printf("answermatrix is:n"); for(i=0;i<3;i++) { for(j=0;j<no;j++) printf("%4.2ft",ans[i][j]); printf("n"); } for(j=0;j<no-1;j++) line(ans[0][j],ans[1][j],ans[0][j+1],ans[1][j+1]); line(ans[0][0],ans[1][0],ans[0][j],ans[1][j]); getch(); } void multi(float tmat[3][3],float coormat[3][10],int no) { int i,j,k,sum=0; for(i=0;i<3;i++) { for(j=0;j<no;j++) { sum=0; for(k=0;k<3;k++) { sum+=tmat[i][k]*coormat[k][j]; } ans[i][j]=sum; } } }
  • 51.
  • 52.
    SCALING: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> float ans[3][10]; void multi(floattmat[3][3],float coormat[3][10],int size); void main() { float coormat[3][10],x[10],y[10]; int gd=DETECT,gm,no,sx,sy,sum=0,i,j,k; float tmat[3][3]={{1,0,0},{0,1,0},{0,0,1}}; clrscr(); initgraph(&gd,&gm,"..bgi"); printf("enter (sx,sy):"),scanf("%d%d",&sx,&sy); tmat[0][0]=sx; tmat[1][1]=sy; printf("Translation matrix is:n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%4.1ft",tmat[i][j]); printf("n"); } printf("enter no. of vertices:n"),scanf("%d",&no); for(i=0;i<no;i++) printf("enter (x%d,y%d):n",i+1,i+1),scanf("%f%f",&x[i],&y[i]); for(i=0;i<no;i++) coormat[0][i]=x[i]; for(i=0;i<no;i++) coormat[1][i]=y[i]; for(i=0;i<no;i++) coormat[2][i]=1; printf("co ordinate matrix is:n"); for(i=0;i<3;i++) { for(j=0;j<no;j++) printf("%4.2ft",coormat[i][j]); printf("n"); } //printing starting line
  • 53.
    for(j=0;j<no-1;j++) line(coormat[0][j],coormat[1][j],coormat[0][j+1],coormat[1][j+1]); line(coormat[0][0],coormat[1][0],coormat[0][j],coormat[1][j]); multi(tmat,coormat,no); printf("answer matrix is:n"); for(i=0;i<3;i++) { for(j=0;j<no;j++) printf("%4.2ft",ans[i][j]); printf("n");} for(j=0;j<no-1;j++) line(ans[0][j],ans[1][j],ans[0][j+1],ans[1][j+1]); line(ans[0][0],ans[1][0],ans[0][j],ans[1][j]); getch(); } void multi(float tmat[3][3],float coormat[3][10],int no) { int i,j,k,sum=0; for(i=0;i<3;i++) { for(j=0;j<no;j++) { sum=0; for(k=0;k<3;k++) { sum+=tmat[i][k]*coormat[k][j]; } ans[i][j]=sum; } } }
  • 54.
  • 55.
    2D SCALLING ANDROTATION WITH FIXED POINT: #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> #define PI 22/7 float mat2[3][3]={{1,0,0},{0,1,0},{0,0,1}}; float sx,sy,angle,mat1[10][3],x[10],y[10]; int i,j,n; void pivoting(float,float); void pivot_scaling(); void pivot_rotation(); void mat_multi(); void identity(); void main() { int gd=DETECT,gm,ch; float px,py; clrscr(); initgraph(&gd,&gm,"c:tcbgi"); printf("No. of Vertices: "); scanf("%d",&n); for(i=0;i<n;i++) { printf(" x[%d] y[%d]--> ",(i+1),(i+1)); scanf("%f%f",&x[i],&y[i]); mat1[i][0]=x[i]; mat1[i][1]=y[i]; mat1[i][2]=1; } for(i=0;i<n;i++) { if(i==(n-1)) line(mat1[i][0],mat1[i][1],mat1[0][0],mat1[0][1]); else line(mat1[i][0],mat1[i][1],mat1[i+1][0],mat1[i+1][1]); } getch(); printf("nn1:Scaling With Pivot Pointn2:Rotation With Pivot PointnYour Choise -->"); scanf("%d",&ch);
  • 56.
    switch(ch) { case 1: scanf("%f",&px, printf("PivotPoint for X--> ")); scanf("%f",&py, printf("Pivot Point for Y--> ")); pivoting(-px,-py); identity(); pivot_scaling(); identity(); pivoting(px,py); printf("Afterr Scalling Matrix --> n"); for(i=0;i<n;i++) { for(j=0;j<3;j++) printf(" %5.2f ",mat1[i][j]); printf("n"); } for(i=0;i<n;i++) { if(i==(n-1)) line(mat1[i][0],mat1[i][1],mat1[0][0],mat1[0][1]); else line(mat1[i][0],mat1[i][1],mat1[i+1][0],mat1[i+1][1]); } break; case 2: scanf("%f",&px, printf("Pivot Point for X--> ")); scanf("%f",&py, printf("Pivot Point for Y--> ")); pivoting(-px,-py); identity(); pivot_rotation(); identity(); pivoting(px,py); printf("After Rotation Matrix--> n"); for(i=0;i<n;i++) { for(j=0;j<3;j++) printf(" %5.2f ",mat1[i][j]); printf("n"); } for(i=0;i<n;i++) { if(i==(n-1)) line(mat1[i][0],mat1[i][1],mat1[0][0],mat1[0][1]); else line(mat1[i][0],mat1[i][1],mat1[i+1][0],mat1[i+1][1]); } break;
  • 57.
    default: printf("Wrong Choice!"); } getch(); } void identity() { inti,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) if(i==j) mat2[i][j]=1; else mat2[i][j]=0; } } void mat_multi() { int i,j,k,m; for(i=0;i<n;i++) { for(j=0;j<3;j++) { m=0; for(k=0;k<3;k++) m+=mat1[i][k]*mat2[k][j]; mat1[i][j]=m; } } } void pivoting(float px,float py) { mat2[2][0]=px; mat2[2][1]=py; mat_multi(); }
  • 58.
    void pivot_scaling() { scanf("%f",&sx, printf("ScalingFactor for X-direction--> ")); scanf("%f",&sy, printf("Scaling Factor for Y-direction--> ")); mat2[0][0]=sx; mat2[1][1]=sy; mat_multi(); } void pivot_rotation() { printf("Rotation Angle--> "); scanf("%f",&angle); angle=(PI*angle/180); mat2[0][0]=cos(angle); mat2[1][1]=cos(angle); mat2[0][1]=sin(angle); mat2[1][0]=-1*sin(angle); mat_multi(); }
  • 59.
  • 60.
    Practical: 10 Aim: 2DTransformation- 2D Reflection, 2D Shearing. REFLECTION : #include<stdio.h> #include<conio.h> #include<graphics.h> float rmat[3][3],cor[3][10]; int n; void mul(float c[3][10], float c1[3][3], int n); void polygon(float cor[3][10], int n); void id(float rmat[3][3]); void id(float rmat[3][3]) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i==j) rmat[i][j]=1; else rmat[i][j]=0; }}} void polygon(float cor[3][10],int n) { int j; cor[0][n]=cor[0][0]; cor[1][n]=cor[1][0]; for(j=0;j<n;j++) line(320+cor[0][j],240-cor[1][j],320+cor[0][j+1],240-cor[1][j+1]); } void mul(float c[3][10], float c1[3][3],int n) { float ans[3][10]; int i,j,k; float sum;
  • 61.
    for(i=0;i<3;i++) { for(j=0;j<n;j++) { sum=0; for(k=0;k<3;k++) sum=sum+c1[i][k]*c[k][j]; ans[i][j]=sum; }} polygon(ans,n); } void main() { int ch,gd,gm,x,y,i,j; gd=DETECT; initgraph(&gd,&gm,"d:tcbgi"); cleardevice(); printf("ENTERNUMBER OF VERTICES-->>"); scanf("%d",&n); printf("nENTER CO-ORDINATE VALUES -->>"); for(i=0;i<n;i++) { for(j=0;j<3;j++) { if(j==2) cor[j][i]=1; else scanf("%f",&cor[j][i]); }} label: moveto(0,0); getch(); cleardevice(); line(0,240,640,240); line(320,0,320,480); polygon(cor,n);
  • 62.
    printf("1.REFLECTION ABOUT X-AXISn2.REFLECTION ABOUT Y- AXIS n3.REFLECTION ABOUT BOTH AXISn4.REFLECTION AT Y=Xn5.REFLECTION ABOUT Y=-Xn6.EXIT"); printf("nENTER YOUR CHOICE-->>"); scanf("%d",&ch); switch(ch) { case 1: id(rmat); rmat[1][1]=-1; mul(cor,rmat,n); break; case 2: id(rmat); rmat[0][0]=-1; mul(cor,rmat,n); break; case 3: id(rmat); rmat[0][0]=-1; rmat[1][1]=-1; mul(cor,rmat,n); break; case 4: id(rmat); rmat[0][0]=0; rmat[1][0]=1; rmat[0][1]=1; rmat[1][1]=0; mul(cor,rmat,n); break; case 5: id(rmat); rmat[0][0]=0; rmat[1][0]=-1; rmat[0][1]=-1;
  • 63.
  • 64.
    SHEARING: #include<stdio.h> #include<conio.h> #include<graphics.h> float smat[3][3],cor[3][10]; int n; voidmul(float c[3][10], float c1[3][3], int n); void polygon(float cor[3][10], int n); void id(float smat[3][3]); void polygon(float cor[3][10],int n) { int j; cor[0][n]=cor[0][0]; cor[1][n]=cor[1][0]; for(j=0;j<n;j++) line(cor[0][j],cor[1][j],cor[0][j+1],cor[1][j+1]); } void mul(float c[3][10], float c1[3][3],int n) { float ans[3][10]; int i,j,k; float sum; for(i=0;i<3;i++) { for(j=0;j<n;j++) { sum=0; for(k=0;k<3;k++) sum=sum+c1[i][k]*c[k][j]; ans[i][j]=sum; } } polygon(ans,n); }
  • 65.
    void id(float smat[3][3]) { inti,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i==j) smat[i][j]=1; else smat[i][j]=0; } } } void main() { int ch,gd=DETECT,gm,x,y,i,j; float sh,ref; initgraph(&gd,&gm,"d:tcbgi"); cleardevice(); printf("enter no. of vertices:"); scanf("%d",&n); printf("nenter co-ord. values:"); for(i=0;i<n;i++) { for(j=0;j<3;j++) { if(j==2) cor[j][i]=1; else scanf("%f",&cor[j][i]); } }
  • 66.
    label:getch(); cleardevice(); polygon(cor,n); while(1) { printf("1.SHEARING WITH X-REFn2.SHERAING WITH Y- REFn3.EXIT"); printf("nENTER YOUR CHOICE:"); scanf("%d",&ch); switch(ch) { case 1: printf("nENTER SHX & Y-REF-->>:"); scanf("%f %f",&sh,&ref); id(smat); smat[0][1]=sh; smat[0][2]=(-1)*sh*ref; mul(cor,smat,n); goto label; break; case 2: printf("nENTER VALUES FOR SHY & Y-REF-->>"); scanf("%f %f",&sh,&ref); id(smat); smat[1][0]=sh; smat[1][2]=(-1)*sh*ref; mul(cor,smat,n); goto label; break; case 3: exit(0); } getch(); } }
  • 67.
  • 69.
    Practical : 11 Aim:-Implement Cohen-Sutherland Line Clipping Algorithm. #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<dos.h> #include<math.h> #include<graphics.h> int w1,w2,w3,w4; 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); void main() { int gd=DETECT, gm,v; PT p1,p2,ptemp; initgraph(&gd,&gm," "); cleardevice(); printf("nnttENTER END-POINT 1 (x,y): "); scanf("%d,%d",&p1.x,&p1.y); printf("nnttENTER END-POINT 2 (x,y): "); scanf("%d,%d",&p2.x,&p2.y); printf("nnttENTER WINDOW (left,top,right,bottom) : "); scanf("%d,%d,%d,%d",&w1,&w2,&w3,&w4);
  • 70.
    cleardevice(); drawwindow(); getch(); drawline(p1,p2,15); getch(); p1=setcode(p1); p2=setcode(p2); v=visibility(p1,p2); switch(v) { case 0: cleardevice(); drawwindow(); drawline(p1,p2,15); break; case1: cleardevice(); drawwindow(); break; case 2: cleardevice(); p1=resetendpt (p1,p2); p2=resetendpt(p2,p1); drawwindow(); drawline(p1,p2,15); break; } getch(); closegraph(); }
  • 71.
    void drawwindow() { setcolor(WHITE); rectangle(w1,w2,w3,w4); } void drawline(PT p1,PT p2,int cl) { setcolor(cl); line(p1.x,p1.y,p2.x,p2.y); } PT setcode(PT p) { PT ptemp; if(p.y<w2) ptemp.code[0]='1'; else ptemp.code[0]='0'; if(p.y>w4) ptemp.code[1]='1'; else ptemp.code[1]='0'; if (p.x>w3) ptemp.code[2]='1'; else ptemp.code[2]='0'; if (p.x<w1) ptemp.code[3]='1'; else ptemp.code[3]='0'; ptemp.x=p.x; ptemp.y=p.y; return(ptemp); }
  • 72.
    int visibility (PTp1,PT p2) { int i,flag=0; 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); } PT resetendpt (PT p1,PT p2) { PT temp; int x,y,i; float m,k; if( p1.code[3]=='1') x=w1; if(p1.code[2]=='1') x=w3; if((p1.code[3]=='1')||(p1.code[2]=='1')) { m=(float) (p2.y-p1.y)/(p2.x-p1.x); k=(p1.y+(m*(x-p1.x))); temp.y=k; temp.x=x;
  • 73.
  • 74.
    OUTPUT: (1) Entering lineend points and window corners.
  • 75.
    (2) Window isdrawn using given window corners (3) Line is drawn using given end points.
  • 76.
    (4) Line isclipped .
  • 77.
    Practical : 12 Aim:-Implement Sutherland-Hodgeman Polygon Clipping Algorithm. #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> typedef struct { float x; float y; }PT; int n; void left(PT p1,PT p[20],PT pp[20]) { int i,j=0; for(i=0;i<n;i++) { if(p[i].x < p1.x && p[i+1].x >= p1.x) { if(p[i+1].x-p[i].x!=0) { pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p1.x-p[i].x)+p[i].y; } else { pp[j].y = p[i].y; } pp[j].x = p1.x;
  • 78.
    j++; pp[j].x=p[i+1].x; pp[j].y=p[i+1].y; j++; } if(p[i].x > p1.x&& p[i+1].x >= p1.x) { pp[j].y = p[i+1].y; pp[j].x = p[i+1].x; j++; } if(p[i].x > p1.x && p[i+1].x <= p1.x) { if(p[i+1].x-p[i].x!=0) { pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p1.x-p[i].x)+p[i].y; } else { pp[j].y = p[i].y; } pp[j].x = p1.x; j++; } } for(i=0;i<j;i++) { p[i].x = pp[i].x; p[i].y = pp[i].y; } p[i].x = pp[0].x; p[i].y = pp[0].y; n=j; } void right(PT p2,PT p[20],PT pp[20])
  • 79.
    { int i,j=0; for(i=0;i<n;i++) { if(p[i].x >p2.x && p[i+1].x <= p2.x) { if(p[i+1].x-p[i].x!=0) { pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p2.x-p[i].x)+p[i].y; } else { pp[j].y = p[i].y; } pp[j].x = p2.x; j++; pp[j].x=p[i+1].x; pp[j].y=p[i+1].y; j++; } if(p[i].x < p2.x && p[i+1].x <= p2.x) { pp[j].y = p[i+1].y; pp[j].x = p[i+1].x; j++; } if(p[i].x < p2.x && p[i+1].x >= p2.x) { if(p[i+1].x-p[i].x!=0) { pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p2.x-p[i].x)+p[i].y; } else
  • 80.
    { pp[j].y = p[i].y; } pp[j].x= p2.x; j++; } } for(i=0;i<j;i++) { p[i].x = pp[i].x; p[i].y = pp[i].y; } p[i].x = pp[0].x; p[i].y = pp[0].y; n=j; } void top(PT p1,PT p[20],PT pp[20]) { int i,j=0; for(i=0;i<n;i++) { if(p[i].y < p1.y && p[i+1].y >= p1.y) { if(p[i+1].y-p[i].y!=0) { pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p1.y-p[i].y)+p[i].x; } else { pp[j].x = p[i].x; } pp[j].y = p1.y;
  • 81.
    j++; pp[j].x=p[i+1].x; pp[j].y=p[i+1].y; j++; } if(p[i].y > p1.y&& p[i+1].y >= p1.y) { pp[j].y = p[i+1].y; pp[j].x = p[i+1].x; j++; } if(p[i].y > p1.y && p[i+1].y <= p1.y) { if(p[i+1].y-p[i].y!=0) { pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p1.y-p[i].y)+p[i].x; } else { pp[j].x = p[i].x; } pp[j].y = p1.y; j++; } } for(i=0;i<j;i++) { p[i].x = pp[i].x; p[i].y = pp[i].y; } p[i].x = pp[0].x; p[i].y = pp[0].y; n=j; } void bottom(PT p2,PT p[20],PT pp[20])
  • 82.
    { int i,j=0; for(i=0;i<n;i++) { if(p[i].y >p2.y && p[i+1].y <= p2.y) { if(p[i+1].y-p[i].y!=0) { pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p2.y-p[i].y)+p[i].x; } else { pp[j].x = p[i].x; } pp[j].y = p2.y; j++; pp[j].x=p[i+1].x; pp[j].y=p[i+1].y; j++; } if(p[i].y < p2.y && p[i+1].y <= p2.y) { pp[j].y = p[i+1].y; pp[j].x = p[i+1].x; j++; } if(p[i].y < p2.y && p[i+1].y >= p2.y) { if(p[i+1].y-p[i].y!=0) { pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p2.y-p[i].y)+p[i].x; } else
  • 83.
    { pp[j].x = p[i].x; } pp[j].y= p2.y; j++; } } for(i=0;i<j;i++) { p[i].x = pp[i].x; p[i].y = pp[i].y; } p[i].x = pp[0].x; p[i].y = pp[0].y; n=j; } void drawpolygon(PT x[20],int n) { int i; for(i=0;i<n-1;i++) { line(x[i].x,x[i].y,x[i+1].x,x[i+1].y); } line(x[i].x,x[i].y,x[0].x,x[0].y); } void main() { int i,j,gd=DETECT,gm; PT d,p1,p2,p[20],pi1,pi2,pp[20]; initgraph(&gd,&gm,""); printf("Enter coordinates (left,top) of point1 : "); scanf("%f,%f",&p1.x,&p1.y); printf("Enter coordinates (right,bottom) of point2 : "); scanf("%f,%f",&p2.x,&p2.y); printf("Enter the number of vertex : "); scanf("%d",&n); for(i=0;i<n;i++)
  • 84.
    { printf("Enter coordinates ofvertex%d : ",i+1); scanf("%f,%f",&p[i].x,&p[i].y); } p[i].x = p[0].x; p[i].y = p[0].y; cleardevice(); drawpolygon(p,n); rectangle(p1.x,p1.y,p2.x,p2.y); getch(); left(p1,p,pp); right(p2,p,pp); top(p1,p,pp); bottom(p2,p,pp); cleardevice(); rectangle(p1.x,p1.y,p2.x,p2.y); drawpolygon(p,n); getch(); closegraph(); } OUTPUT:
  • 85.
    (1) Entering windowcorners and vertices of polygon. (2) Window is drawn using given window corners.polygonis drawn using given vertices.
  • 86.
    (3) Polygon isclipped against given window. By- Snel Koli