-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrl.js
216 lines (161 loc) · 6.14 KB
/
contrl.js
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
// 页面控制相关
var myLine = {
"points": 0,
"x1": 0,
"y1": 0,
"x2": 0,
"y2": 0
}
var PointsRecord = {
"class": "",
"points": []
}
var buffer = []
buffer.push(PointsRecord)
var mylines = []
var tempPosition = []
var methodChild1 = '<option value="DDA">DDA</option><option value="中点算法">中点算法</option><option value="Bresenham">Bresenham</option><option value="中点画圆">中点画圆</option><option value="中点画椭圆">中点画椭圆</option>'
var methodChild2 = '<option value="多边形填充">多边形填充</option><option value="矩形截线">矩形截线</option><option value="多边形裁剪">多边形裁剪</option>'
var exam = "exam2"
var paraFram = document.querySelector("#" + exam)
paraFram.style.display = "block"
document.querySelector("#method").innerHTML = methodChild2
console.log("repaint happen");
// document.querySelector("#method").innerHTML=methodChild
function drawBuffer(buffer) {
console.log("buffer", buffer);
var positions = []
for (var i = 0; i < buffer.length; i++) {
console.log("swich", i, buffer[i].class);
switch (buffer[i].class) {
case "Polygon":
positions.push(buffer[i].points[0])
positions.push(buffer[i].points[1])
for (var j = 0; j < buffer[i].points.length / 2 - 1; j++) {
k = 2 * j
temp = DDA([buffer[i].points[k], buffer[i].points[k + 1], buffer[i].points[k + 2], buffer[i].points[k + 3]])
positions.push.apply(positions, temp)
}
k = 2 * j
console.log("k=", k);
temp = DDA([buffer[i].points[k], buffer[i].points[k + 1], buffer[i].points[0], buffer[i].points[1]])
positions.push.apply(positions, temp)
break
case "Lines":
for (var j = 0; j + 2 < buffer[i].points.length; j = j + 4) {
k = j
temp = DDA([buffer[i].points[k], buffer[i].points[k + 1], buffer[i].points[k + 2], buffer[i].points[k + 3]])
positions.push.apply(positions, temp)
}
if (buffer[i].points.length % 4 != 0) {
positions.push(buffer[i].points[j])
positions.push(buffer[i].points[j + 1])
}
break
case "Rectangle":
for (var j = 0; j + 2 < buffer[i].points.length; j = j + 4) {
k = j
temp = DDA([buffer[i].points[k], buffer[i].points[k + 1], buffer[i].points[k], buffer[i].points[k + 3]])
positions.push.apply(positions, temp)
temp = DDA([buffer[i].points[k + 2], buffer[i].points[k + 1], buffer[i].points[k + 2], buffer[i].points[k + 3]])
positions.push.apply(positions, temp)
temp = DDA([buffer[i].points[k], buffer[i].points[k + 1], buffer[i].points[k + 2], buffer[i].points[k + 1]])
positions.push.apply(positions, temp)
temp = DDA([buffer[i].points[k], buffer[i].points[k + 3], buffer[i].points[k + 2], buffer[i].points[k + 3]])
positions.push.apply(positions, temp)
}
if (buffer[i].points.length % 4 != 0) {
positions.push(buffer[i].points[j])
positions.push(buffer[i].points[j + 1])
}
break
// case "":
// buffer.splice(i, 1)
// break
}
}
main(positions)
}
function getLinesByUI(x, y) {
if (PointsRecord.class != "Lines") {
PointsRecord.class = "Lines"
PointsRecord.points = []
}
PointsRecord.points.push(x)
PointsRecord.points.push(y)
drawBuffer(buffer)
}
function getPolygonByUI(x, y) {
if (PointsRecord.class != "Polygon") {
PointsRecord.class = "Polygon"
PointsRecord.points = []
}
PointsRecord.points.push(x)
PointsRecord.points.push(y)
drawBuffer(buffer)
}
function getRectangleByUI(x, y) {
if (PointsRecord.class != "Rectangle") {
PointsRecord.class = "Rectangle"
PointsRecord.points = []
}
PointsRecord.points.push(x)
PointsRecord.points.push(y)
drawBuffer(buffer)
}
document.querySelector("#renderCanvas").addEventListener('click', event => {
x = event.offsetX / 5
y = (500 - event.offsetY) / 5
x = parseInt(x)
y = parseInt(y)
radios = document.getElementsByName("element")
var val
for (let index = 0; index < radios.length; index++) {
const tempelement = radios[index];
if (radios[index].checked) {
val = radios[index].value
}
}
if (PointsRecord.class != val && PointsRecord.class != "") {
var temp = JSON.stringify(PointsRecord)
temp = JSON.parse(temp)
buffer.push(temp)
}
switch (val) {
case "Polygon":
getPolygonByUI(x, y)
break
case "Lines":
getLinesByUI(x, y)
break
case "Rectangle":
getRectangleByUI(x, y)
break
}
})
document.querySelector("#clearALL").addEventListener('click', event => {
buffer = []
PointsRecord = {
"class": "",
"points": []
}
buffer.push(PointsRecord)
positions = []
main([])
})
document.querySelector("#examSelect").addEventListener('change', event => {
examText = event.target.value
var paraFram = document.querySelector("#" + exam)
paraFram.style.display = 'none'
exam = examText
var paraFram = document.querySelector("#" + exam)
paraFram.style.display = "block"
switch (exam) {
case 'exam1':
document.querySelector("#method").innerHTML = methodChild1
break
case 'exam2':
document.querySelector("#method").innerHTML = methodChild2
break
}
})