-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDA_Algo.c
173 lines (158 loc) · 3.36 KB
/
DDA_Algo.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<stdio.h>
#include<string.h>
#include<GL/glut.h>
#include<math.h>
#define PI 3.1415926535897932384626433832795
#define MAX_CLICK 2
#define INFINITE 99999999
int k=0;
int count=0;
int numbPoint=0;
double vertex[][2];
double m[99];
double c[99];
char buffer[10];
void type(char *string, double X, double Y)
{
char *c;
glColor3f(1.0, 1.0, 1.0);
glRasterPos2f(X+1,Y);
for (c=string; *c != '\0'; c++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *c);
}
}
void myName()
{
type("Ayush Jain", -495, 490);
type("CSE-CCVT B-1",-495, 475);
type("R110217039",-495, 460);
}
// function to initialize
void myInit (void)
{
// making background color black as first
// 3 arguments all are 0.0
gluOrtho2D(-500,500,-500,500);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
myName();
}
int max(double a, double b){
return a>b?0:1;
}
int min(double a, double b){
return a<b?0:1;
}
void printXY(double X, double Y)
{
gcvt(X,5,buffer);
char str[20]={"( "};
strcat(str, buffer);
strcat(str, ", ");
gcvt(Y,5,buffer);
strcat(str, buffer);
strcat(str, " )");
type(str, X, Y);
}
void printM(double M, double X, double Y)
{
gcvt(M,5,buffer);
char str[20]={"m (slope) = "};
strcat(str, buffer);
type(str, X, Y);
}
void DrawLine()
{
double P[2][2];
int maxPoint, minPoint;
for(int i=0;k<numbPoint;k++,i++)
{
P[i][0] = vertex[k][0];
P[i][1] = vertex[k][1];
}
double m = (P[1][1] - P[0][1])/(P[1][0] - P[0][0]);
printf("%lf\n", m);
printM(m,(P[1][0]+P[0][0])/2,(P[1][1]+P[0][1])/2);
if(m > -1 && m < 1)
{
maxPoint = max(P[0][0], P[1][0]);
minPoint = min(P[0][0], P[1][0]);
for(;P[minPoint][0]<P[maxPoint][0]; P[minPoint][0]+=1)
{
P[minPoint][1]+=m;
glBegin(GL_POINTS);
glColor3f(0.0, 1.0, 1.0);
//printf("(%lf, %lf)\n",P[minPoint][0],P[minPoint][1]);
glVertex2f(P[minPoint][0],P[minPoint][1]);
glEnd();
glFlush();
}
}
if((m >=1 && m < INFINITE) || (m<= -1 && m > -INFINITE))
{
maxPoint = max(P[0][1], P[1][1]);
minPoint = min(P[0][1], P[1][1]);
for(;P[minPoint][1]<P[maxPoint][1]; P[minPoint][1]+=1)
{
P[minPoint][0]+=(1/m);
glBegin(GL_POINTS);
glColor3f(0.0, 1.0, 1.0);
//printf("(%lf, %lf)\n",P[minPoint][0],P[minPoint][1]);
glVertex2f(P[minPoint][0],P[minPoint][1]);
glEnd();
glFlush();
}
}
}
void store(double X, double Y)
{
vertex[numbPoint][0] = X;
vertex[numbPoint][1] = Y;
printf("(%.2lf, %.2lf)\n", vertex[numbPoint][0], vertex[numbPoint][1]);
++numbPoint;
printf("Number of Points: %d\n",numbPoint);
printXY(X, Y);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
glVertex2f(X,Y);
glEnd();
glFlush();
}
void mouseClick(int button, int state, int X, int Y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if(count<=MAX_CLICK)
{
store((X-500),(500-Y));
count++;
if(count == MAX_CLICK){
DrawLine();
count=0;
printf("Number of Line(s): %d\n",numbPoint/2);
}
}
}
}
static void Key(unsigned char key, int x, int y)
{
switch (key) {
case 27:
glutDestroyWindow(1);
break;
}
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// giving window size in X- and Y- direction
glutInitWindowSize(1000, 1000);
// Giving name to window
glutCreateWindow("|| Implementing DDA Algorithm ||");
myInit();
glutKeyboardFunc(Key);
glutMouseFunc(mouseClick);
glutMainLoop();
}