Professional Documents
Culture Documents
Algorithm
Bresenham's Line Drawing Algorithm:
1. Start.
2. Declare variables x,y,x1,y1,x2,y2,p,dx,dy and also declare gdriver=DETECT,gmode.
3. Initialize the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left end-points in (x1,y1).
5. Load (x1,y1) into the frame buffer; that is, plot the first point put x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1,and obtain the initial value of decision parameter p as:
p=(2dy-dx).
7. Starting from first point (x,y) perform the following test:
8. Repeat step 9 while(x<=x2).
Code-
#include<iostream.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
cout<<"Enter co-ordinates of first point: ";
cin>>x0>>y0;
cout<<"Enter co-ordinates of second point: ";
cin>>x1>>y1;
drawline(x0, y0, x1, y1);
return 0;
}
Output-
Experiment No:3
Title-Circle Algorithm
Objective- To implement Bresenham’s algorithms for Circle drawing.
Algorithm
1. Set initial values of (xc, yc) and (x, y)
2. Set decision parameter d to d = 3 – (2 * r).
3. call drawCircle(int xc, int yc, int x, int y) function.
4. Repeat steps 5 to 8 until x < = y
5. Increment value of x.
6. If d < 0, set d = d + (4*x) + 6
7. Else, set d = d + 4 * (x – y) + 10 and decrement y by 1.
8. call drawCircle(int xc, int yc, int x, int y) function
Code-
#include<iostream.h>
#include<graphics.h>
void drawcircle(int x0, int y0, int radius)
{
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
{
y += 1;
err += 2*y + 1;
}
if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
cout<<"Enter radius of circle: ";
cin>>r;
cout<<"Enter co-ordinates of center(x and y): ";
cin>>x>>y;
drawcircle(x, y, r);
return 0;
}
Output-
Experiment No:4
Title- Circle Drawing Algorithms
Objective-To study and Implement Midpoint Circle Algorithm
Algorithms
1. Start.
2. Declare variables x, y, p and also declare gdriver= DETECT, gmode.
3. Initialize the graphic mode with the path location in TC folder.
4. Input the radius r and center xc , yc of the circle .
5. Load x=0, y=r, initial decision parameter p=1-r.so the plot first point is (x, y).
6. Repeat Step 7 to 9 while (x≤y).
7. If (p>0), then plot(x=x+1, y=y-1) do p=p+2*(x-y)+1. Else plot(x=x+1, y) p=p+2*x+1.
8. Determine symmetry in seven octants.
9. Place pixels using put pixel at points (x+ xc , y+ yc) in specified colour by shifting the origin to
center (xc , yc) on both x-axis and y-axis.
10. Close Graph.
11. Stop
Code-
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void draw_circle(int,int,int);
void symmetry(int,int,int,int);
void main()
{
int xc,yc,R;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
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);
draw_circle(xc,yc,R);
getch();
closegraph();
}
void draw_circle(int xc,int yc,int rad)
{
int x = 0;
int y = rad;
int p = 1-rad;
symmetry(x,y,xc,yc);
for(x=0;y>x;x++)
{
if(p<0)
p += 2*x + 3;
else
{
p += 2*(x-y) + 5;
y--;
}
symmetry(x,y,xc,yc);
delay(0);
}
}
void symmetry(int x,int y,int xc,int yc)
{
putpixel(xc+x,yc-y,14);
delay(10);
putpixel(xc+y,yc-x, 1);//For pixel (y,x)
delay(10);
putpixel(xc+y,yc+x, 2);
delay(10);
putpixel(xc+x,yc+y,3);
delay(10);
putpixel(xc-x,yc+y,4);
delay(10);
putpixel(xc-y,yc+x, 5);
delay(10);
putpixel(xc-y,yc-x, 6);
delay(10);
putpixel(xc-x,yc-y, 8);
delay(10);
}
Output-
Experiment No:5
Title- Mid Point Ellipse Algorithm
Objective- To study and Implement Midpoint Ellipse Algorithm
Algorithm
1. Include the graphics header file and obtain graphics mode and driver.
2. Input rx,ry and ellipse center(xc,yc) and obtain the first point on an ellipse centered at origin
as (x0,y0)=(0,ry).
3. Calculate the initial value of the decision parameter in region 1 as p10= ry2- rx2 ry+(1/4)rx2.
4. At each xk position in region 1,starting at k=0,perform the following text: a)If p1k0, the next
point along the ellipse centered on (0,0) is (xk,yk-1) and p2 k+1=p2 k-2rx2 y k+1+ rx2
b)Otherwise, the next point along the ellipse is (xk+1,yk-1) and p2 k+1=p2 k+2 ry2 xk+1-2
rx2yk+1+ rx2 using the same incremental calculations for x and y as in region 1.
8. Plot the pixel to display the ellipse using put pixel function. Midpoint Ellipse Drawing
algorithm.
Code-
#include<graphics.h>
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int xc,yc,x,y;float p;
long rx,ry;
cin>>xc>>yc;
cin>>rx>>ry;
//Region 1
p=ry*ry-rx*rx*ry+rx*rx/4;
x=0;y=ry;
if(p < 0)
x++;
p = p+2*ry*ry*x+ry*ry;
else
x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y-ry*ry;
}
putpixel(xc+x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc-x,yc-y,RED);
//Region 2
p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;
while(y > 0)
if(p <= 0)
x++;y--;
p = p+2*ry*ry*x-2*rx*rx*y+rx*rx;
else
y--;
p = p-2*rx*rx*y+rx*rx;
putpixel(xc+x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc-x,yc-y,RED);
getch();
closegraph();
Output-
Experiment No: 6,7
Title- 2D Transformations
Objective- To study and Implement 2D Transformations such as translation, rotation, scaling,
reflection and sharing. Transformations are used to position objects, to shape objects, to
change viewing positions, and even to change how something is viewed without deformation.
Algorithm
1. Start
2. Draw the image with default parameters.
3. Get the choice from the user.
4. Get the parameters for transformation.
5. Perform the transformation.
6. Draw the image.
7. Stop
Code-
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
void main()
clrscr();
float tx1;
x1 = y2 = 100;
x2 = y1 = 150;
rectangle(x1,y1,x2,y2);
cout<<"\nChoice :";
cin>>c;
switch(c)
case 1:
cin>>tx>>ty;
break;
case 2 :
tx1=tx1*(3.14/180);
break;
case 3 :
cin>>sx>>sy;
break;
default :
getch();
closegraph();
Output-
Experiment No: 8
Title-Line Clipping(2 D Clipping)
Objective- To implement Cohen-Sutherland 2D Line clipping This method speeds up the
processing of line segments by performing initial tests that reduce the number of intersections
that must be calculated. Every line endpoint is assigned to four digit, binary code called‖ region
code‖. Region code identifies the location of the pint relative to the boundaries of the clipping
rectangle
Algorithm
1. Read 2 end points of line as p1(x1,y1) and p2(x2,y2)
2. Read 2 corner points of the clipping window (left-top and right-bottom) as
(wx1,wy1) and (wx2,wy2)
3. Assign the region codes for 2 endpoints p1 and p2 using following steps:-
initialize code with 0000
Code-
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.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);
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)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
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);
}
void drawline(PT p1,PT p2)
{
line(p1.x,p1.y,p2.x,p2.y);
}
PT setcode(PT p) //for setting the 4 bit code
{
PT ptemp;
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
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
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=150;
if(p1.code[2]=='1')
x=450;
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);
}
Output
Before Clipping
After Clipping
Experiment No: 9
Title- Line Clipping(2 D Clipping)
Objective- To implement Liang-Barsky 2D Line clipping
Algorithm
p1 = -dx, q1 = x1 – xwmin
p2 = dx, q2 = xwmax – x1
p3 = -dy, q3 = y1 – ywmin
p4 = dy, q4 = ywmax – y1
else, check whether line is horizontal or vertical and then check the line
endpoints with the corresponding boundaries.
5. Initialize t1 & t2 as
t1 = 0 & t2 = 1
xx2 = x1 + t2dx
yy1 = y1 + t1dy
yy2 = y1 + t2dy
10. Stop.
Code-
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
int i,gd=DETECT,gm;
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
float t1,t2,p[4],q[4],temp;
x1=120;
y1=120;
x2=300;
y2=300;
xmin=100;
ymin=100;
xmax=250;
ymax=250;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(xmin,ymin,xmax,ymax);
dx=x2-x1;
dy=y2-y1;
p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;
q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{
if(p[i]==0)
{
cout<<"line is parallel to one of the clipping boundary";
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
y1=ymin;
}
if(y2>ymax)
{
y2=ymax;
}
line(x1,y1,x2,y2);
}
if(i>1)
{
if(x1<xmin)
{
x1=xmin;
}
if(x2>xmax)
{
x2=xmax;
}
line(x1,y1,x2,y2);
}
}
}
}
t1=0;
t2=1;
for(i=0;i<4;i++)
{
temp=q[i]/p[i];
if(p[i]<0)
{
if(t1<=temp)
t1=temp;
}
else
{
if(t2>temp)
t2=temp;
}
}
if(t1<t2)
{
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
}
delay(5000);
closegraph();
}
Output-
Experiment No: 10
Title Polygon-
Clipping
Objective- Pre-
requisite To study
and Implement
Polygon Clipping
Algorithm using
Sutherland Hodgman
Algorithm
Algorithm
1. Read coordinates of all vertices of the Polygon.
2. Read coordinates of the dipping window
3. Consider the left edge of the window
4. Compare the vertices of each edge of the polygon, individually with the clipping plane.
5. Save the resulting intersections and vertices in the new list of vertices according to four
possible relationships between the edge and the clipping boundary.
6. Repeat the steps 4 and 5 for remaining edges or the clipping window. Each time the
resultant list of vertices is successively passed to process the next edge of the clipping
window.
7. Stop
Code-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
arr[k]=x2;
arr[k+1]=y2;
k+=2;
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
void main()
int gdriver=DETECT,gmode,n,poly[20];
float xi,yi,xf,yf,polyy[20];
clrscr();
cin>>xmin>>ymin;
cout<<"xmax,ymax :";
cin>>xmax>>ymax;
cin>>n;
cin>>polyy[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\t\tUNCLIPPED POLYGON";
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i < 2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
cout<<"\tCLIPPED POLYGON";
getch();
closegraph();
OUTPUT:
Experiment No: 11
Objective Window- A region of the scene selected for viewing (also called clipping window)
Viewport : A region on display device form aping to window Translate lower-left corner of
window to origin Scale width and height of window to match viewport’s Translate corner at
origin to lower-left corner in viewport.
Algorithm-
1. Read the window & view port co-ordinates.
2. Read the number of polygon vertices
3. Draw the filled polygon inside the window.
4. Draw the view port
5. Map the polygon which is in the window to the view port.
6. Display the polygon inside the view port.
Code
#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"stdlib.h"
void main()
{
float xwmin,xwmax,ywmax,ywmin;
float xvmin,xvmax,yvmax,yvmin;
float x[10],y[10],yv,xv,sx,sy;
int gd=DETECT,gm,i;
clrscr();
printf("\n enter window port coordinates:\n(xwmin,ywmin,xwmax,ywmax): ");
scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);
printf("\n enter view port coordinates:\n(xvmin,yvmin,xvmax,yvmax): ");
scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax);
printf("\n enter vertices for triangle: ");
for(i=0;i < 3;i++)
{
printf("\n enter(x%d,y%d):",i,i);
scanf("%f%f",&x[i],&y[i]);
}
sx=((xvmax-xvmin)/(xwmax-xwmin));
sy=((yvmax-yvmin)/(ywmax-xwmin));
initgraph(&gd,&gm," ");
outtextxy(80,30,"window port");
rectangle(xwmin,ywmin,xwmax,ywmax);
for(i=0;i <2;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
cleardevice();
for(i=0;i <3;i++)
{
x[i]=xvmin+((x[i]-xwmin)*sx);
y[i]=yvmin+((y[i]-ywmin)*sy);
}
outtextxy(150,10,"view port");
rectangle(xvmin,yvmin,xvmax,yvmax);
for(i=0;i <2;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[2],y[2],x[0],y[0]);
getch();
}
Output
Experiment No:12
Title- 3D TRANSFORMATIONS
Algorithm
1. Start.
7. Stop
Code-
#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 x,y,z,o,x1,x2,y1,y2;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
//setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,10,1);
scanf("%d%d",&x,&y);
//axis();
printf("After translation:");
bar3d(midx+x+50,midy-(y+100),midx+x+60,midy-(y+90),10,1);
getch();
closegraph();
OUTPUT:
Experiment No:13
Objective-The main Objective is to project the 3D image using the parallel line projection
Algorithm
2. Draw the front, side & top elevation of the 3D image by projecting the 3D image using the
parallel line projection.
3. Display the 3D image along with the front, side & top view.
Code-
#include "stdio.h"
#include "stdlib.h"
#include"graphics.h"
#include"conio.h"
void main()
int gd=DETECT,gm;
int x[20],y[20],i,s,d;
initgraph(&gd,&gm,"");
scanf("%d",&s);
for(i=0;i< s;i++)>
{
printf("(x%d,y%d) :",i,i);
scanf("%d%d",&x[i],&y[i]);
printf("Depth :");
scanf("%d",&d);
draw3d(s,x,y,d);
getch();
setcolor(14);
for(i=0;i< s-1;i++)
line(x[i]+200,y[i],x[i+1]+200,y[i+1]);
line(x[i]+200,y[i],x[0]+200,y[0]);
getch();//top view
for(i=0;i< s-1;i++)
line(x[i],300,x[i+1],300);
line(x[i],300+d*2,x[i+1],300+d*2);
line(x[i],300,x[i],300+d*2);
line(x[i+1],300,x[i+1],300+d*2);
}
getch();//side view
for(i=0;i< s-1;i++)
line(10,y[i],10,y[i+1]);
line(10+d*2,y[i],10+d*2,y[i+1]);
line(10,y[i],10+d*2,y[i]);
line(10,y[i+1],10+d*2,y[i+1]);
getch();
closegraph();
int i,j,k=0;
for(j=0;j< 2;j++)
for(i=0;i< s-1;i++)>
line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k);
line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);
k=d;
for(i=0;i< s;i++)
line(x[i],y[i],x[i]+d,y[i]-d);
Output: