-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarchingSquares.cpp
More file actions
180 lines (157 loc) · 3.56 KB
/
MarchingSquares.cpp
File metadata and controls
180 lines (157 loc) · 3.56 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
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
#include <bits/stdc++.h>
#include "circle.h"
#include "line.h"
using namespace std;
const int SIZE = 1;
const int XMIN = 0;
const int XMAX = 700;
const int YMIN = 0;
const int YMAX = 700;
const int WMIN = 0;
const int WMAX = 700;
const int WIDTH = (XMAX - XMIN + SIZE);
const int HEIGHT = (YMAX - YMIN + SIZE);
double val[WIDTH][HEIGHT];
// define the function to be drawn
double f(int x, int y) {
// ellipse
double x1 = (x - 300) * (x - 300) / 40000.0;
double y1 = (y - 300) * (y - 300) / 10000.0;
return x1 + y1 - 1;
}
// draw the square based on the index and the coordinates
void displaySquare(int idx, int x, int y) {
double fromx, fromy, tox, toy;
switch(idx) {
case 0:
case 15:
break;
case 1:
case 14:
fromx = x + SIZE / 2;
fromy = y;
tox = x;
toy = y + SIZE / 2;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
break;
case 2:
case 13:
fromx = x + SIZE / 2;
fromy = y;
tox = x + SIZE;
toy = y + SIZE / 2;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
break;
case 3:
case 12:
fromx = x;
fromy = y + SIZE / 2;
tox = x + SIZE;
toy = y + SIZE / 2;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
break;
case 4:
case 11:
fromx = x + SIZE;
fromy = y + SIZE / 2;
tox = x + SIZE / 2;
toy = y + SIZE;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
break;
case 5:
fromx = x + SIZE / 2;
fromy = y;
tox = x + SIZE;
toy = y + SIZE / 2;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
fromx = x;
fromy = y + SIZE / 2;
tox = x + SIZE / 2;
toy = y + SIZE;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
break;
case 10:
fromx = x + SIZE / 2;
fromy = y;
tox = x;
toy = y + SIZE / 2;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
fromx = x + SIZE;
fromy = y + SIZE / 2;
tox = x + SIZE / 2;
toy = y + SIZE;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
break;
break;
case 6:
case 9:
fromx = x + SIZE / 2;
fromy = y;
tox = x + SIZE / 2;
toy = y + SIZE;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
break;
case 7:
case 8:
fromx = x;
fromy = y + SIZE / 2;
tox = x + SIZE / 2;
toy = y + SIZE;
// drawline
drawline(fromx, fromy, tox, toy, WMIN, WMAX);
break;
}
}
// for every grid point, determine the index of the
// square based on the function value at neighbors
void marchingSquares() {
for (int i = XMIN; i < XMAX; i += SIZE) {
for (int j = YMIN; j < YMAX; j += SIZE) {
int id = 0;
if (val[i][j] > 0) id |= 1;
if (val[i + SIZE][j] > 0) id |= 2;
if (val[i + SIZE][j + SIZE] > 0) id |= 4;
if (val[i][j + SIZE] > 0) id |= 8;
displaySquare(id, i, j);
//cout << setw(3) << id;
}
// << endl;
}
}
// fill the grid with values at differen coordinates
void display(void) {
glClear (GL_COLOR_BUFFER_BIT);
for (int x = XMIN; x <= XMAX; x += SIZE) {
for (int y = YMIN; y <= YMAX; y += SIZE) {
val[x][y] = f(x, y);
}
}
marchingSquares();
glFlush();
}
void init (void) {
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (WMAX, WMAX);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Marching Squares");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}