-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathBresenhsm.cpp
64 lines (54 loc) · 1.15 KB
/
Bresenhsm.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
#include <GL/glut.h>
#include<iostream>
using namespace std;
int width = 200;
int height = 200;
unsigned char* pixels;
void set_pixel(int x, int y) {
int index = y*width + x;
pixels[index * 3 + 0] = 255;
pixels[index * 3 + 1] = 0;
pixels[index * 3 + 2] = 0;
}
void Bresenhsm_init(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
float k = dy*1.0/ dx;
float e = k - 0.5;
int x = x1;
int y = y1;
while (x <= x2) {
set_pixel(x, y);
//cout << x << " " << y << endl;
x++;
if (e > 0) {
y++;
e += k - 1;
}
else {
e += k;
}
}
}
void mydraw() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(300, 100);//
glutInitWindowSize(width, height);
glutCreateWindow("Bresenhsm画线");
pixels = new unsigned char[width*height * 3];
int x1, y1, x2, y2;
cout << "请输入两个点坐标: ";
cin >> x1 >> y1 >> x2 >> y2;
Midpoint_init(x1, y1, x2, y2);
glutDisplayFunc(&mydraw);
glutMainLoop();
return 0;
}