-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbresLine.cpp~
More file actions
134 lines (134 loc) · 3.2 KB
/
bresLine.cpp~
File metadata and controls
134 lines (134 loc) · 3.2 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
#include <GL/glut.h>
#include <math.h>
int ww = 600, wh = 400;
int first = 0;
int xi, yi, xf, yf;
void putPixel (int x, int y)
{
glColor3f (0.3, 0.2, 0.0); // activate the pixel by setting the point color to white
glBegin (GL_POINTS);
glVertex2i (x, y); // set the point
glEnd ();
glFlush (); // process all openGL routines as quickly as possible
}
void display()
{
glClearColor(0.4, 0.7, 0.5, 1.0);
glColor3f(0.2, 0.3, 0.3);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void bresenhamAlg (int x0, int y0, int x1, int y1)
{
int dx = abs (x1 - x0);
int dy = abs (y1 - y0);
int x, y;
if (dx >= dy)
{
int d = 2*dy-dx;
int ds = 2*dy;
int dt = 2*(dy-dx);
if (x0 < x1)
{
x = x0;
y = y0;
}
else
{
x = x1;
y = y1;
x1 = x0;
y1 = y0;
}
putPixel (x, y);
while (x < x1)
{
if (d < 0)
d += ds;
else {
if (y < y1) {
y++;
d += dt;
}
else {
y--;
d += dt;
}
}
x++;
putPixel (x, y);
}
}
else {
int d = 2*dx-dy;
int ds = 2*dx;
int dt = 2*(dx-dy);
if (y0 < y1) {
x = x0;
y = y0;
}
else {
x = x1;
y = y1;
y1 = y0;
x1 = x0;
}
putPixel (x, y);
while (y < y1)
{
if (d < 0)
d += ds;
else {
if (x > x1){
x--;
d += dt;
} else {
x++;
d += dt;
}
}
y++;
putPixel (x, y);
}
}
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
switch(first)
100: {
101: case 0:
102: xi = x;
103: yi = (wh-y);
104: first = 1;
105: break;
106: case 1:
107: xf = x;
108: yf = (wh-y);
109: bresenhamAlg ( xi, yi, xf, yf);
110: first = 0;
111: break;
112: }
113: }
114: }
115: void myinit()
116: {
117: glViewport(0,0,ww,wh);
118: glMatrixMode(GL_PROJECTION);
119: glLoadIdentity();
120: gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
121: glMatrixMode(GL_MODELVIEW);
122: }
123: int main(int argc, char** argv)
124: {
125: glutInit(&argc,argv);
126: glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
127: glutInitWindowSize(ww,wh);
128: glutCreateWindow("Bresenham Line Algorithm");
129: glutDisplayFunc(display);
130: myinit();
131: glutMouseFunc(mouse);
132: glutMainLoop();
133: return 0;
134: }