C Program to display a line graph using midpoint line algorithm.

Program to display a line graph using midpoint line algorithm. The input to the program is a set of data corresponding to the X and Y-axes. Data parts are to be displayed as asterisk (*) according to the input specification.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
 
#define MAX 20
 
void swap(int* a,int* b)
{
int t=*a;
*a=*b;
*b=t;
}
 
void midpointline(int x1,int y1,int x2,int y2)
{
int dx,dy,d,incry,incre,incrne,slopegt1=0;
dx=abs(x1-x2);dy=abs(y1-y2);
if(dy>dx)
{
swap(&x1,&y1);
swap(&x2,&y2);
swap(&dx,&dy);
slopegt1=1;
}
if(x1>x2)
{
swap(&x1,&x2);
swap(&y1,&y2);
}
if(y1>y2)
incry=-1;
else
incry=1;
d=2*dy-dx;
incre=2*dy;
incrne=2*(dy-dx);
while(x1<x2)
{
if(d<=0)
d+=incre;
else
{
d+=incrne;
y1+=incry;
}
x1++;
if(slopegt1)
putpixel(y1,x1,WHITE);
else
putpixel(x1,y1,WHITE);
}
}
 
void main()
{
int n,i;
int pt[MAX][2];
int gd=DETECT,gm;
 
printf("Enter the number of points:");
scanf("%d",&n);
 
printf("Enter the x and y coordinates:");
for(i=0;i<n;i++)
{
scanf("%d %d",&pt[i][0],&pt[i][1]);
pt[i][1]=480 - pt[i][1];
}
 
initgraph(&gd,&gm,"..\\bgi");
 
line(1,0,1,480);		// X - Axis
line(0,479,639,479);		// Y - Axis
 
outtextxy(pt[0][0]-2,pt[0][1]-3,"*");
 
for(i=0;i<n-1;i++)
{
midpointline(pt[i][0],pt[i][1],pt[i+1][0],pt[i+1][1]);
outtextxy(pt[i+1][0]-2,pt[i+1][1]-3,"*");
}
getch();
closegraph();
}
Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

2 thoughts on “C Program to display a line graph using midpoint line algorithm.

  1. Thanks for the source code, but can i have midpoint code in JAVA?
    Are there any package that i need to build the source correctly?
    Thank you very much.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in