Files 3 2021 February NotesHubDocument 1613295767
Files 3 2021 February NotesHubDocument 1613295767
Files 3 2021 February NotesHubDocument 1613295767
PRACTICAL FILE
SUBMITTED BY:
NAME :ARYAMAN
ROLL NO : 017 20902719
BRANCH :CSE 2ND YEAR
THOERY:
1.closegraph()
Function which 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.
Syntax :
void closegraph();
ex.
int main()
Initgraph initializes the graphics system by loading a graphics driver from disk (or validating a
registered driver), and putting the system into graphics mode. To start the graphics system, first call
the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode.
Syntax :
int main()
3.cleardevice()
Function which 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 color.
void cleardevice();
ex.
// cleardevice function
cleardevice();
4.putpixel()
Syntax :
5.getpixel()
function which returns the color of pixel present at location (x, y).
Syntax :
int color;
color = getpixel(0, 0);
6.getmaxx()
function which returns the maximum X coordinate for current graphics mode and driver.
Syntax :
int getmaxx();
ex.
sprintf(arr, "Maximum X coordinate for current "
"graphics mode And driver = %d", getmaxx());
7.getmaxy()
function which returns the maximum Y coordinate for current graphics mode and driver.
Syntax :
int getmaxy();
ex.
sprintf(arr, "Maximum Y coordinate for current "
8.line()
function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end
points of the line.
Syntax :
9.lineto()
Syntax :
lineto(250, 100);
10.gotoxy()
places cursor at a desired location on screen i.e., we can change cursor position at a specific position
Syntax :
Ex.int x, y;
x = 10;
y = 10;
gotoxy(x, y);
11.circle()
Function which draws a circle with center at (x, y) and given radius.
Syntax :
circle(x, y, radius); where,(x, y) is center of the circle.'radius' is the Radius of the circle.
12.arc()
function which draws an arc with center at (x, y) and given radius. start_angle is the starting point of
angle and end_angle is the ending point of the angle. The value of the angle can vary from 0 to 360
degree.
Syntax :
void arc(int x, int y, int start_angle,
int end_angle, int radius);
where,(x, y) is the center of the arc.start_angle is the starting angle and end_angle is the ending
angle.'radius' is the Radius of the arc
ex.
13.pieslice()
Draws and fills a pie slice with center at (x, y) and given radius r. The slice travels from s_angle to
e_angle which are starting and ending angles for the pie slice. The angles for pie‐slice are given in
degrees and are measured counterclockwise.
Syntax :
ex.
pieslice(300, 300, 0, 120, 150);
14.ellipse()
In this function x, y is the location of the ellipse. x_radius and y_radius decide the radius of form x and
y.start_angle is the starting point of angle and end_angle is the ending point of angle. The value of
angle can vary from 0 to 360 degree.
Syntax :
void ellipse(int x, int y, int start_angle, int end_angle, int x_radius, int y_radius)
15.sector()
Function which draws and fills an elliptical pie slice with (x, y) as center, (s_angle, e_angle) as
starting and ending angle and (x_radius, y_radius) as x and y radius of sector.
Syntax :
void sector(int x, int y, int s_angle, int e_angle, int x_radius, int y_radius);where,(x, y) is center of the
sector.(s_angle, e_angle) are starting
and ending angles.(x_radius, y_radius) are x and y radius of sector.
ex.
16.rectangle()
It is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw
the rectangle. left specifies the X‐coordinate of top left corner, top specifies the Y‐coordinate of top
left corner, right specifies the X‐coordinate of right bottom corner, bottom specifies the Y‐coordinate
of right bottom corner.
Syntax :
17.drawpoly()
function which is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.
Syntax :
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <dos.h>
int main()
{
int gd = DETECT, gm,color;
char a[10];
int poly[] = {190, 80, 210, 240,300, 270, 190, 80};
int x_circle = 350,y_circle = 350,radius=30,font=6,direction=0,fontsize=3;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setbkcolor(BROWN);
setfillstyle(SOLID_FILL,RED);
circle(x_circle,y_circle,radius);
floodfill(x_circle,y_circle,WHITE);
settextstyle(font, direction, fontsize);
outtextxy(80,50,"Outtextxy running");
outtext("Outtext running");
setcolor(BLUE);
circle(250,400,radius);
fillellipse(450, 200, 30, 50);
delay(4000);
fillpoly(4,poly);
setlinestyle(DASHED_LINE, 0, 1);
line(100,100,175,175);
color=getcolor();
sprintf(a,"The value of the current drawing color is %d",color);
outtextxy(80,80,a);
getch();
closegraph();
return 0;
}
void main()
{
int gd=DETECT,gm,k,l,count=0;
int x=10,y=200,x1=675,y1=380;
int stangle=35,endangle=140,radius=90;
initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
setbkcolor(BLUE);
while(!kbhit())
{
cleardevice();
setbkcolor(BLUE);
if(x<640)
{
x+=5;
y+=1;
arc(x,y,stangle,endangle+35,radius);
arc(x,y‐110,190,323,radius+2);
circle(x+40,y‐60,5);
line(x‐90,y‐90,x‐90,y‐8);
}
else
{
x1‐=5;
y1‐=1;
arc(x1,y1,stangle‐30,endangle+4,radius);
arc(x1,y1‐110,217,350,radius+2);
circle(x1‐40,y1‐60,5);
line(x1+90,y1‐90,x1+90,y1‐10);
}
setcolor(YELLOW);
delay(90);
}
closegraph();
}
int ldisp=0;
void Rain(int x)
{
int i,rx,ry;
for(i=0;i<400;i++)
{
rx=rand() % ScreenWidth;
ry=rand() % ScreenHeight;
if(ry<GroundY‐4)
{
if(ry<GroundY‐120 || (ry>GroundY‐120 && (rx<x‐20 || rx>x+60)))
line(rx,ry,rx+0.5,ry+4);
}
}
}
void main()
{
THEORY:
DDA Algorithm:
Step=abs(dy)
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1,x2,y2,x3,y3;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setbkcolor(BLACK);
outtextxy(10,300,"Lines drawn using DDA Algorithm");
x0 = 50 , y0 = 150, x1 = 450, y1 = 250; //SLOPE < 1
x2= 125,y2=100,x3=175,y3=275; //SLOPE > 1
dx = (float)(x1 ‐ x0);
dy = (float)(y1 ‐ y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, WHITE);
x += dx;
y += dy;
i=i+1;
}
dx = (float)(x3 ‐ x2);
dy = (float)(y3 ‐ y2);
if(dx>=dy)
{
steps = dx;
}
else
{
/*Case 1: Take endpoints of a line such that both endpoints of have same
coordinates (x0=x1=y0=y1).
Case 2: Take endpoints (x0, y0, x1, y1) of a line such that X coordinates of
both endpoints are same (x0= x1) but different values of Y coordinates.
Case 3: Take endpoints (x0, y0, x1, y1) of a line such that Y coordinates of
both endpoints are same (y0= y1) but different values of X coordinates.
Case 4: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Positive and greater than 1.
Case 5: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Negative and greater than 1.
Case 6: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Positive and less than 1.
Case 7: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Negative and less than 1(m<1).*/
void main()
{
int gd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setbkcolor(BLACK);
outtextxy(10,300,"Lines drawn using DDA Algorithm");
for(int j =0;j<6;j++){
clrscr();
printf("Enter x0,x1,y0,y1 for line:\n");
scanf("%d %d %d %d",&x0,&x1,&y0,&y1);
dx = (float)(x1 ‐ x0);
dy = (float)(y1 ‐ y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y,RED);
x += dx;
y += dy;
i=i+1;
}
getch();
}
closegraph();
}
CASE 3:
CASE 5:
CASE 7:
Ans:
Step 1 : Except the two end points of Line from User.
Step 2 : Calculate the slope(m) of the required Line.
Step 3 : Identify the value of slope(m).
Step 3.1 : If slope(m) is Less than 1 i.e: m < 1
* Step 3.1.1 : Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value
for the decision parameter as ‐
* p0 = 2dy − dx
* Step 3.1.2 : At each Xk along the line, starting at k = 0, perform the following test −
* If pk < 0, the next point to plot is (xk + 1,yk) and
pk+1 = pk + 2dy
else
* plot (xk,yk + 1)
* pk+1 = pk + 2dy − 2dx
* Step 3.1.3 : Repeat step 4(dx ‐ 1) times.
Step 3.2 : If slope(m) is greater than or equal to 1 i.e: m >= 1
* Step 3.2.1 : Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value
for the decision parameter as ‐
* p0 = 2dx − dy
* Step 3.2.2 : At each Yk along the line, starting at k = 0, perform the following test −
* If pk < 0, the next point to plot is (xk,yk + 1) and
pk+1 = pk + 2dx
else
* plot (xk + 1,yk)
* pk+1 = pk + 2dx − 2dy
* Step 3.2.3 : Repeat step 4(dy ‐ 1) times.
Step 3.3 : Exit.
#include<stdio.h>
#include<graphics.h>
#include<conio.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;
}
getch();
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
outtextxy(350,30,"Lines drawn using Bresenham's Algo");
//FOR LINE OF SLOPE M<1
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);
//FOR LINE OF SLOPE M>1
}
/*
Case 1: Take endpoints of a line such that both endpoints of have same
coordinates (x0=x1=y0=y1).
Case 2: Take endpoints (x0, y0, x1, y1) of a line such that X
coordinates of both endpoints are same (x0= x1) but different values of
Y coordinates.
Case 3: Take endpoints (x0, y0, x1, y1) of a line such that Y
coordinates of both endpoints are same (y0= y1) but different values of
X coordinates.
Case 4: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Positive and greater than 1.
Case 5: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Negative and greater than 1.
Case 6: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Positive and less than 1.
Case 7: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Negative and less than 1(m<1).*/
for(int i=0;i<6;i++){
clrscr();
printf("Enter co‐ordinates of first point(x0,y0):\n");
scanf("%d %d", &x0, &y0);
printf("Enter co‐ordinates of second point(x1,y1):\n");
scanf("%d %d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}
CASE 3:
CASE 5:
CASE 7:
The best approximation of the true circle will be described by those pixels in the raster that
falls the least distance from the true circle. We want to generate the points from
90° to 45°. Assume that the last scan‐converted pixel is P1 as shown in fig. Each new point
closest to the true circle can be found by taking either of two actions.
Since D (Si) will always be +ve & D (Ti) will always be ‐ve, a decision variable d may be
defined as follows:
Therefore,
di=(xi‐1+1)2+ yi‐12 ‐r2+(xi‐1+1)2+(yi‐1 ‐1)2‐r2
If it is assumed that the circle is centered at the origin, then at the first step x = 0 & y = r.
Therefore,
di=(0+1)2+r2 ‐r2+(0+1)2+(r‐1)2‐r2
=1+1+r2‐2r+1‐r2
= 3 ‐ 2r
Algorithm:
Step1: Start Algorithm
Step4: Calculate d = 3 ‐ 2r
Step7: Plot eight points by using concepts of eight‐way symmetry. The center is at (p, q).
Current active pixel is (x, y).
putpixel (x+p, y+q)
putpixel (y+p, x+q)
putpixel (‐y+p, x+q)
putpixel (‐x+p, y+q)
putpixel (‐x+p, ‐y+q)
putpixel (‐y+p, ‐x+q)
putpixel (y+p, ‐x+q)
putpixel (x+p, ‐y‐q)
Step9: Go to step 6
while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)‐(4*y)+10;
y=y‐1;
}
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
}
}
int main(void)
{
/* request auto detection */
int xc,yc,r,gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
getch();
closegraph();
return 0;
}
Theory:
It is based on the following function for testing the spatial relationship between the arbitrary point (x,
y) and a circle of radius r centered at the origin:
Now, consider the coordinates of the point halfway between pixel T and pixel S
If Pi is+ve ⟹midpoint is outside the circle (or on the circle)and we choose pixel S.
We have yi+1=yi
We have yi+1=yi‐1
We can put ≅1
∴r is an integer
So, P1=1‐r
Step3: End
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
class bresen
{
float x, y,a, b, r, p;
public:
void get ();
void cal ();
};
void main ()
{
bresen b;
b.get ();
b.cal ();
getch ();
}
Void bresen :: get ()
{
cout<<"ENTER CENTER AND RADIUS";
cout<< "ENTER (a, b)";
cin>>a>>b;
cout<<"ENTER r";
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
int x,y,r,midx,midy;
void axis();
void translation()
{
int tx,ty,xn1,yn1,xn2,yn2;
printf("\n Enter the translation:");
scanf("%d%d",&tx,&ty);
cleardevice();
outtextxy(400,100,"TRANSLATION");
axis();
xn1=x+tx;
yn1=y+ty;
circle(xn1,yn1,r);
getch();
}
void translateLine ( int P[][2], int T[])
{
setcolor (2);
line(P[0][0], P[0][1], P[1][0], P[1][1]);
P[0][0] = P[0][0] + T[0];
P[0][1] = P[0][1] + T[1];
P[1][0] = P[1][0] + T[0];
P[1][1] = P[1][1] + T[1];
setcolor(3);
line(P[0][0], P[0][1], P[1][0], P[1][1]);
closegraph();
}
void translateRectangle ( int P[][2], int T[])
{
setcolor (2);
rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);
// calculating translated coordinates
P[0][0] = P[0][0] + T[0];
P[0][1] = P[0][1] + T[1];
P[1][0] = P[1][0] + T[0];
P[1][1] = P[1][1] + T[1];
rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);
}
void get()
{
printf("\n Enter the Raidus(r): ");
scanf("%d",&r);
outtextxy(200,100,"ORIGINAL OBJECT");
axis();
getch();
}
void axis()
{
midx=x= getmaxx() / 2;
midy=y= getmaxy() / 2;
line(0,midy,midx*2,midy);
line(midx,0,midx,midy*2);
circle(midx,midy,r);
}
void findNewCoordinate(int s[][2], int p[][1])
{
int temp[2][1] = { 0 };
p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}
findNewCoordinate(s, p);
x[i] = p[0][0];
y[i] = p[1][0];
}
void main()
{
int ch,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int P[2][2] = {5, 8, 12, 18}; // coordinates of point
int T[] = {2, 1}; // translation factor
int x[] = { 100, 200, 300 };
int y[] = { 200, 100, 200 };
int sx = 2, sy = 2;
scale(x, y, sx,sy);
translateRectangle (P, T);
translateLine (P, T);
get();
do
{
cleardevice();
outtextxy(10,10,"1)TRANSLATION");
outtextxy(10,20,"2)SCALING");
outtextxy(10,30,"6)EXIT");
outtextxy(10,40,"ENTER UR CHOICE:");
scanf("%d",&ch);
switch(ch)
Output:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
int main()
{
intgd=0,gm,x1,y1,x2,y2;
double s,c, angle;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(RED);
printf("Enter coordinates of line: ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
cleardevice();
setbkcolor(WHITE);
line(x1,y1,x2,y2);
getch();
setbkcolor(BLACK);
printf("Enter rotation angle: ");
scanf("%lf", &angle);
setbkcolor(WHITE);
c = cos(angle *3.14/180);
s = sin(angle *3.14/180);
x1 = floor(x1 * c + y1 * s);
y1 = floor(‐x1 * s + y1 * c);
x2 = floor(x2 * c + y2 * s);
y2 = floor(‐x2 * s + y2 * c);
cleardevice();
line(x1, y1 ,x2, y2);
getch();
closegraph();
return 0;
}
#include <graphics.h>
#include <ctype.h>
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <process.h>
int check(int,char);
void Initialize(void);void drawEllipse1(int color);
void drawEllipse1(int color);
void drawEllipse2(int color);
void main() {
int color, color1;
char ch;
Initialize();
ch = 'R';color1=1;color=1;
do {
if (ch == 'R' || ch == 'L') {
if (ch == 'L') {
if (color1 == 4) color = 1;
else color = ++color1;
}
drawEllipse1(color);
color1 = color;
color = check(color, ch);
drawEllipse2(color);
}
ch = toupper(getche());
} while (ch!='X');
closegraph();
}
void Initialize(void) {
int driver = DETECT,mode, errorcode;
initgraph(&driver,&mode, "C:\\TURBOC3\\BGI");
errorcode = graphresult();
if (errorcode != grOk) {
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("<Copy egavga.bgi to C:\\TURBOC3\\BGI");
getch();
exit(1);
}
}
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<dos.h>
int main()
{
int i,x,y;
int vertex[10][3],n;
clrscr();
cout<<"\nEnter the no. of vertex : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the points (x,y): ";
cin>>x>>y;
vertex[i][0]=x;
vertex[i][1]=y;
vertex[i][2]=1;
}
shear(vertex,n);
getch();
return 0;
}
void init(int vertex[10][3],int n)
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(10);
line(0,240,640,240);
line(320,0,320,480);
setcolor(3);
line(450,20,490,20);
setcolor(15);
line(450,50,490,50);
setcolor(6);
outtextxy(500,20,"Original");
outtextxy(500,50,"Transformed");
setcolor(3);
for(i=0;i<n‐1;i++)
}
void mul(int mat[3][3],int vertex[10][3],int n)
{
int i,j,k;
int res[10][3];
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
res[i][j]=0;
for(k=0;k<3;k++)
{
res[i][j] = res[i][j] + vertex[i][k]*mat[k][j];
}
}
}
setcolor(15);
for(i=0;i<n‐1;i++)
{
line(320+res[i][0],240‐res[i][1],320+res[i+1][0],240‐res[i+1][1]);
}
line(320+res[n‐1][0],240‐res[n‐1][1],320+res[0][0],240‐res[0][1]);
}
void shear(int vertex[10][3],int n)
{
int opt;
int shear_array[3][3];
cout<<"\n1.x‐shear\n2.y‐shear\nYour Choice: ";
cin>>opt;
switch(opt)
{
case 1: int xsh;
cout<<"\nEnter the x shear : ";
cin>>xsh;
shear_array[0][0]=1;
shear_array[1][0]=xsh;
shear_array[2][0]=0;
shear_array[0][1]=0;
shear_array[1][1]=1;
shear_array[2][1]=0;
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,ang,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
setfillstyle(3,25);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
outtextxy(100,100,"ORIGINAL OBJECT");
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
bar3d(midx+100,midy‐20,midx+60,midy‐90,20,5);
axis();
outtextxy(100,20,"TRANSLATION");
printf("\n\n Enter the Translation vector: ");
scanf("%d%d",&x,&y);
bar3d(midx+100,midy‐20,midx+60,midy‐90,20,5);
bar3d(midx+(x+100),midy‐(y+20),midx+(x+60),midy‐(y+90),20,5);
axis();
outtextxy(100,20,"SCALING");
printf("\n Enter the Scaling Factor: ");
scanf("%d%d%d",&x,&y,&z);
bar3d(midx+100,midy‐20,midx+60,midy‐90,20,5);
bar3d(midx+(x*100),midy‐(y*20),midx+(x*60),midy‐(y*90),20*z,5);
axis();
outtextxy(100,20,"ROTATION");
printf("\n Enter the Rotation angle: ");
scanf("%d",&ang);
x1=100*cos(ang*3.14/180)‐20*sin(ang*3.14/180);
y1=100*sin(ang*3.14/180)+20*sin(ang*3.14/180);
x2=60*cos(ang*3.14/180)‐90*sin(ang*3.14/180);
y2=60*sin(ang*3.14/180)+90*sin(ang*3.14/180);
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,ang,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
setfillstyle(3,25);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
outtextxy(100,100,"ORIGINAL OBJECT");
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
bar3d(midx+100,midy‐20,midx+60,midy‐90,20,5);
axis();
outtextxy(100,20,"TRANSLATION");
printf("\n\n Enter the Translation vector: ");
scanf("%d%d",&x,&y);
bar3d(midx+100,midy‐20,midx+60,midy‐90,20,5);
bar3d(midx+(x+100),midy‐(y+20),midx+(x+60),midy‐(y+90),20,5);
axis();
outtextxy(100,20,"SCALING");
printf("\n Enter the Scaling Factor: ");
scanf("%d%d%d",&x,&y,&z);
bar3d(midx+100,midy‐20,midx+60,midy‐90,20,5);
bar3d(midx+(x*100),midy‐(y*20),midx+(x*60),midy‐(y*90),20*z,5);
axis();
outtextxy(100,20,"ROTATION");
printf("\n Enter the Rotation angle: ");
scanf("%d",&ang);
x1=100*cos(ang*3.14/180)‐20*sin(ang*3.14/180);
y1=100*sin(ang*3.14/180)+20*sin(ang*3.14/180);
This is one of the oldest and most popular line clipping algorithm. To speed up the process
this algorithm performs initial tests that reduce number of intersections that must be
calculated. It does so by using a 4 bit code called as region code or outcodes. These codes
identify location of the end point of line.
Each bit position indicates a direction, starting from the rightmost position of each bit
indicates left, right, bottom, top respectively.
Algorithm
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
delay(5000);
closegraph();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
if(p.y<100)
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
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
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);
}
if(p1.code[3]=='1')
x=150;
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(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
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);
}
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>
#include <graphics.h>
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline (PT p1,PT p2);
PT setcode(PT p);
int visibility (PT p1,PT p2);
PT resetendpt (PT p1,PT p2);
main()
{
int gd=DETECT, gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
cleardevice();
printf(“ENTER END‐POINT 1 (x,y): “);
scanf(“%d%d”,&p1.x,&p1.y);
printf(“\nENTER END‐POINT 2 (x,y): “);
scanf(“%d%d”,&p2.x,&p2.y);
cleardevice();
drawwindow();
PT setcode(PT p)