-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDDA_Bresenham_Algo.cpp
More file actions
129 lines (105 loc) · 3.26 KB
/
DDA_Bresenham_Algo.cpp
File metadata and controls
129 lines (105 loc) · 3.26 KB
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
// 1.DDA Algorithm
#include <GL\glut.h>
#include <iostream>
#include <windows.h>
using namespace std; int x_start = 10; int y_start = 20; int x_end = 200; int y_end = 100;
void myInit()
{
glClearColor(1.0, 0.5, 0.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluOrtho2D(0.0, 400.0, 0.0, 400.0);
}
void draw_pixel(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void lineDDA(int x_start, int y_start, int x_end, int y_end)
{
int dx = x_end - x_start, dy = y_end - y_start, steps, k; float xIncrement, yIncrement, x = x_start, y = y_start; if (abs(dx) > abs(dy)) steps = abs(dx); else
steps = abs(dy);
xIncrement = dx / (float)steps; yIncrement = dy / (float)steps;
draw_pixel(round(x), round(y));
for (k = 0; k < steps; k++) { x += xIncrement; y += yIncrement;
draw_pixel(round(x), round(y));
}
}
void myDisplay()
{
lineDDA(x_start, y_start, x_end, y_end); glFlush();
}
int main(int argc, char** argv)
{
cout<<"Enter (x_start, y_start, x_end, y_end)\n"; cin >> x_start >> y_start >> x_end >> y_end;
glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("DDA Line Drawing Algorithm");
glutDisplayFunc(myDisplay); myInit();
glutMainLoop();
return 0;
}
// Bresenham Line Drawing Algorithm
/*
#include <GL\glut.h>
#include <iostream>
#include <windows.h>
using namespace std; int x_start = 10; int y_start = 20; int x_end = 200; int y_end = 100;
void myInit() {
glClearColor(1.0, 0.5, 0.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluOrtho2D(0.0, 400.0, 0.0, 400.0);
}
void draw_pixel(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void bresenham(int x_start, int y_start, int x_end, int y_end)
{
int dx, dy, i, e; int incx, incy, inc1, inc2; int x, y;
dx = x_end - x_start; dy = y_end - y_start; if (dx < 0)
dx = -dx;
if (dy < 0)
dy = -dy; incx = 1; if (x_end < x_start) incx = -1; incy = 1;
if (y_end < y_start)
incy = -1; x = x_start; y = y_start; if (dx > dy)
{
draw_pixel(x, y); e = 2 * dy - dx; inc1 = 2 * (dy - dx); inc2 = 2 * dy;
for (i = 0; i < dx; i++)
{
if (e >= 0)
{
y += incy; e += inc1;
}
else
e += inc2; x += incx; draw_pixel(x, y);
}
}
else
{
draw_pixel(x, y); e = 2 * dx - dy; inc1 = 2 * (dx - dy); inc2 = 2 * dx; for (i = 0; i < dy; i++)
{
if (e >= 0)
{
x += incx; e += inc1;
}
else
e += inc2; y += incy;
draw_pixel(x, y);
}
}
}
void myDisplay()
{
bresenham(x_start, y_start, x_end, y_end); glFlush();
}
int main(int argc, char** argv)
{
cout<<"Enter (x_start, y_start, x_end, y_end)\n"; cin >> x_start >> y_start >> x_end >> y_end; glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Bresenham's Line Drawing");
glutDisplayFunc(myDisplay); myInit();
glutMainLoop();
return 0;
}
*/