0% found this document useful (0 votes)
45 views2 pages

program To Draw A Line Using Mid Point Algorithm

This program uses the midpoint line algorithm to draw a line on a graph from user-input start and end points. It initializes the graphics mode, gets the x and y coordinates of the start and end points from the user, calculates the change in x and y and the initial value of p, then uses a for loop to iterate from the start x to the end x, calculating p on each iteration and drawing the pixel at the current x and y to trace out the line on the graph.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

program To Draw A Line Using Mid Point Algorithm

This program uses the midpoint line algorithm to draw a line on a graph from user-input start and end points. It initializes the graphics mode, gets the x and y coordinates of the start and end points from the user, calculates the change in x and y and the initial value of p, then uses a for loop to iterate from the start x to the end x, calculating p on each iteration and drawing the pixel at the current x and y to trace out the line on the graph.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Program to draw a line using mid point algorithm.

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd = DETECT, gm;
int x0, x1, y0, y1,dx,dy;
float p;
initgraph(&gd,&gm,"c:/tc/bgi");
cout<<"Enter the end points of the line(x1,y1 and x2,y2): "<<endl;
cout<<"Enter starting point"<<endl;
cout<<"x1: ";
cin>>x0;
cout<<"y1: ";
cin>>y0;
cout<<"Enter ending point "<<endl;
cout<<"x2: ";
cin>>x1;
cout<<"y2: ";
cin>>y1;
clrscr();
dx=x1-x0;
dy=y1-y0;
p=2*dy-dx;
int y=y0;
for(int i=x0;i<=x1;i++)
{
if(p<0)
{ putpixel(i,y,10);
p=p+2*dy;
}
else
{ y++;
putpixel(i,y,10);
p=p+2*dy-2*dx;
}

}
getch();
closegraph();
}
OUTPUT:
Enter the end points of the line(x1,y1 and x2,y2):
Enter starting point
x1 : 20
y1 : 20
Enter ending point
x2 : 400
y2 : 400

You might also like