-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
146 lines (136 loc) · 3.76 KB
/
main.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
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
136
137
138
139
140
141
142
143
144
145
#include "lib.h"
#define W 320
#define H 240
class Neuron neuron;
std::vector<struct pt> stuck;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glLoadIdentity();
glOrtho(-0.5, (GLdouble)w - 0.5, (GLdouble)h - 0.5, -0.5, -1.0, 1.0);
}
void mouse(int button, int state, int x, int y)
{
struct pt temp;
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
temp.val[0] = (double)x/W;
temp.val[1] = (double)y/H;
temp.type = 1.0;
neuron.data.push_back(temp);
temp.val[0] = x;
temp.val[1] = y;
stuck.push_back(temp);
glPointSize(5);
glColor3d(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
break;
case GLUT_MIDDLE_BUTTON:
std::cout << "x = " << x << " y = " << y << std::endl;
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN) {
temp.val[0] = (double)x/W;
temp.val[1] = (double)y/H;
temp.type = 0.0;
neuron.data.push_back(temp);
temp.val[0] = x;
temp.val[1] = y;
stuck.push_back(temp);
glPointSize(5);
glColor3d(0.0, 1.0, 0.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
break;
default:
break;
}
}
void keyboard(unsigned char key, int x, int y)
{
double tmp[2];
switch (key) {
case 'q':
case 'Q':
exit(0);
case 'a':
neuron.study();
break;
case 'b':
glPointSize(1);
for (int i = 0; i < W; i++) {
for (int j = 0; j < H; j++) {
tmp[0] = (double)i/W;
tmp[1] = (double)j/H;
glBegin(GL_POINTS);
if (neuron.output(tmp)) {
glColor3d(1.0, 0.0, 0.0);
} else {
glColor3d(0.0, 1.0, 0.0);
}
glVertex2i(i, j);
glEnd();
}
}
glFlush();
break;
case 'c':
for (int i = 0; i < neuron.data.size(); i++) {
tmp[0] = neuron.data[i].val[0];
tmp[1] = neuron.data[i].val[1];
if (neuron.output(tmp)) {
glPointSize(3);
glBegin(GL_POINTS);
glColor3d(1.0, 1.0, 1.0);
glVertex2i(stuck[i].val[0], stuck[i].val[1]);
glEnd();
glFlush();
} else {
glPointSize(3);
glBegin(GL_POINTS);
glColor3d(0.0, 0.0, 0.0);
glVertex2i(stuck[i].val[0], stuck[i].val[1]);
glEnd();
glFlush();
}
}
break;
case 'd':
neuron.show_para();
break;
default:
break;
}
}
void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}
int main(int argc, char *argv[])
{
glutInitWindowPosition(100, 100);
glutInitWindowSize(W, H);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}