-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path01.BresenhamLineAlgorithm.cpp
More file actions
135 lines (113 loc) · 2.49 KB
/
01.BresenhamLineAlgorithm.cpp
File metadata and controls
135 lines (113 loc) · 2.49 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
130
131
132
133
134
135
/*
1. Implement Brenham’s line drawing algorithm for all types of slope.
*/
#include<stdio.h>
#include<math.h>
#include<glut.h>
int xStart, yStart, xEnd, yEnd;
void myInit();
void display();
void setPixel(int x, int y);
void bresenhamLine(int x1, int y1, int x2, int y2);
void drawLineLow(int x1, int y1, int x2, int y2);
void drawLineHigh(int x1, int y1, int x2, int y2);
void myInit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
glMatrixMode(GL_MODELVIEW);
}
void setPixel(int x, int y)
{
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glFlush();
}
void bresenhamLine(int x1, int y1, int x2, int y2)
{
if (abs(y2 - y1) < abs(x2 - x1))
{
if (x1 > x2)
drawLineLow(x2, y2, x1, y1);
else
drawLineLow(x1, y1, x2, y2);
}
else
{
if (y1 > y2)
drawLineHigh(x2, y2, x1, y1);
else
drawLineHigh(x1, y1, x2, y2);
}
}
void drawLineLow(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1;
int dy = y2 - y1;
int iy = 1;
if (dy < 0)
{
iy = -1;
dy = -dy;
}
int P = 2 * dy - dx;
int y = y1;
for (int x = x1; x <= x2; x++)
{
setPixel(x, y);
if (P >= 0)
{
y = y + iy;
P = P - 2 * dx;
}
P = P + 2 * dy;
}
}
void drawLineHigh(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1;
int dy = y2 - y1;
int ix = 1;
if (dx < 0)
{
ix = -1;
dx = -dx;
}
int P = 2 * dx - dy;
int x = x1;
for (int y = y1; y <= y2; y++)
{
setPixel(x, y);
if (P >= 0)
{
x = x + ix;
P = P - 2 * dy;
}
P = P + 2 * dx;
}
}
void display()
{
glClearColor(1, 1, 1, 1); //Specifies a background RGB color for a display window.
glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f(1, 0, 0); // Set color to red.
glPointSize(2);
bresenhamLine(xStart, yStart, xEnd, yEnd);
glFlush(); // Process all OpenGL routines as quickly as possible.
}
int main()
{
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); // Set display mode.
glutInitWindowPosition(100, 100); // Set top-left display-window position.
glutInitWindowSize(500, 500); // Set display-window width and height.
glutCreateWindow("Bresenham's Line Drawing Algorthm"); // Create display window.
myInit(); // Execute initialization procedure.
printf("Enter co-ordinates of first point: ");
scanf_s("%d %d", &xStart, &yStart);
printf("Enter co-ordinates of second point: ");
scanf_s("%d %d", &xEnd, &yEnd);
glutDisplayFunc(display); //Invokes a function to create a picture within the current display window.
glutMainLoop(); //Executes the computer-graphics program.
}