-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
219 lines (200 loc) · 4.3 KB
/
main.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>
#define VIEWHEIGHT 500.0
#define VIEWWIDTH 500.0
#define OFFHEIGHT 100
#define OFFWIDTH 100
struct pt {
GLint x;
GLint y;
bool valid;
} linestart, lineend, wnddim;
bool ptCompare(struct pt lhs, struct pt rhs)
{
if(lhs.x != rhs.x || lhs.y != rhs.y)
return false;
return true;
}
struct pt dispToCoord(struct pt pos);
void drawView(void);
void drawLine(void);
void display(void);
void resize(GLsizei width, GLsizei height);
void mpress(int btn, int state, int x, int y);
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
drawView();
drawLine();
glFlush();
glutSwapBuffers();
}
void drawLine(void)
{
struct pt bound_bl = dispToCoord((struct pt){0, 0, false}),
bound_tr = dispToCoord((struct pt){VIEWWIDTH, VIEWHEIGHT, false});
if(!linestart.valid || !lineend.valid ||
ptCompare(linestart, lineend) ||
linestart.x < bound_bl.x ||
linestart.x > bound_tr.x ||
lineend.x < bound_bl.x ||
lineend.x > bound_tr.x ||
linestart.y < bound_bl.y ||
linestart.y > bound_tr.y ||
lineend.y < bound_bl.y ||
lineend.y > bound_tr.y)
return;
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POINTS);
GLint deltax = lineend.x - linestart.x,
deltay = lineend.y - linestart.y,
x = 0,
y = 0;
GLfloat m = (float)deltay / deltax;
if(m == INFINITY || m == NAN) {
while(y < deltay) {
glVertex2i(linestart.x, linestart.y + y);
y++;
}
}
else if(m == -INFINITY) {
while(y > deltay) {
glVertex2i(linestart.x, linestart.y + y);
y--;
}
}
else {
float residual = 0;
while(x > deltax) {
x--;
int cnt = 0;
do {
glVertex2i(linestart.x + x, linestart.y + y);
cnt++;
y--;
} while(cnt < m + residual);
do {
glVertex2i(linestart.x + x, linestart.y + y);
cnt--;
y++;
} while(cnt > m + residual);
residual = m + residual - cnt;
}
while(x < deltax) {
x++;
int cnt = 0;
do {
glVertex2i(linestart.x + x, linestart.y + y);
cnt++;
y++;
} while(cnt < m + residual);
do {
glVertex2i(linestart.x + x, linestart.y + y);
cnt--;
y--;
} while(cnt > m + residual);
residual = m + residual - cnt;
}
}
glEnd();
}
void drawCircle()
{
}
struct pt dispToCoord(struct pt pos)
{
return (struct pt){
(pos.x + OFFWIDTH) * wnddim.x / (VIEWWIDTH + 2 * OFFWIDTH),
(pos.y + OFFHEIGHT) * wnddim.y / (VIEWHEIGHT + 2 * OFFHEIGHT),
true
};
}
void mpress(int btn, int state, int x, int y)
{
y = wnddim.y - y;
struct pt check;
check.x = 0;
check.y = 0;
check.valid = true;
struct pt checkbl = dispToCoord(check);
check.x = VIEWWIDTH;
check.y = VIEWHEIGHT;
check.valid = true;
struct pt checktr = dispToCoord(check);
if(x < checkbl.x || y < checkbl.y ||
x > checktr.x || y > checktr.y)
return;
if(btn == GLUT_LEFT_BUTTON) {
linestart.x = x;
linestart.y = y;
linestart.valid = true;
glutPostRedisplay();
}
else if(btn == GLUT_RIGHT_BUTTON) {
lineend.x = x;
lineend.y = y;
lineend.valid = true;
glutPostRedisplay();
}
}
void drawView(void)
{
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_POINTS);
struct pt bound_bl = dispToCoord((struct pt){0, 0, false}),
bound_tr = dispToCoord((struct pt){VIEWWIDTH, VIEWHEIGHT, false}),
pos = {0, 0, true};
for(pos.x = bound_bl.x; pos.x < bound_tr.x; pos.x++)
for(pos.y = bound_bl.y; pos.y < bound_tr.y; pos.y++) {
glVertex2i(pos.x, pos.y);
}
glEnd();
}
void resize(GLsizei width, GLsizei height)
{
wnddim.x = width;
wnddim.y = height;
wnddim.valid = true;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, 0.0,
height, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void keypress(unsigned char key, int x, int y)
{
switch(key) {
case 'q':
case 'Q':
exit(0);
}
}
int gcf(int a, int b)
{
if(a % b)
return gcf(b, a % b);
return b;
}
int main(int argc, char **argv)
{
memset(&linestart, 0, sizeof(linestart));
memset(&lineend, 0, sizeof(lineend));
memset(&wnddim, 0, sizeof(wnddim));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 10);
glutInitWindowSize(700, 700);
glutCreateWindow("Program 1");
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutMouseFunc(mpress);
glutKeyboardFunc(keypress);
glutMainLoop();
return 0;
}