-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDA-Line-Drawing-Algorithm.cpp
109 lines (91 loc) · 2.2 KB
/
DDA-Line-Drawing-Algorithm.cpp
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
/*
Md. Abdul Mutalib
B.Sc (Engg.) in CSE
North East University Bangladesh
Connect with me:
Github: https://github.com/mamutalib
*/
#include<stdio.h>
#include <GL/gl.h>
#include <GL/glut.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define nl '\n'
#define fn for(int i = 0; i<n; i++)
#define ft for(int i = 0; i<t; i++)
#define wt while(t--)
#define wn while(n--)
#define fs first
#define f first
#define s second
#define pf printf
#define sc scanf
float x1, y1, x2, y2, m, i, j;
float dx, dy;
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glEnd();
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POINTS);
if (m > 0 && m <= 1) {
while (x1 <= x2 && y1 <= y2) {
x1 = x1 + 1;
y1 = y1 + m;
glVertex3f(x1 / 100, y1 / 100, 0.0);
printf("%f %f \n", x1, y1);
}
} else if (m > 1) {
while (x1 <= x2 && y1 <= y2) {
x1 = x1 + (1 / m);
y1 = y1 + 1;
glVertex3f(x1 / 100, y1 / 100, 0.0);
printf("%f %f", x1, y1);
}
} else if (m > -1 && m <= 0) {
while (x1 >= x2 && y1 >= y2) {
x1 = x1 - 1;
y1 = y1 - m;
glVertex3f(x1 / 100, y1 / 100, 0.0);
printf("%f %f", x1, y1);
}
} else if (m < -1)
{
while (x1 >= x2 && y1 >= y2) {
x1 = x1 - (1 / m);
y1 = y1 - 1;
glVertex3f(x1 / 100, y1 / 100, 0.0);
printf("%f %f", x1, y1);
}
}
glEnd();
glFlush();
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char ** argv) {
printf("Enter value of X1 :");
scanf("%f", & x1);
printf("Enter value of y1 :");
scanf("%f", & y1);
printf("Enter value of X2 :");
scanf("%f", & x2);
printf("Enter value of Y2 :");
scanf("%f", & y2);
dx = x2 - x1;
dy = y2 - y1;
m = dy / dx;
glutInit( & argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA Line Drawing Algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}