-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
378 lines (348 loc) · 9.63 KB
/
main.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* @file 项目JS文件
* @author wenhui-huang([email protected])
*/
//磁道序列数组
let trackSequence = [];
/**
* 磁盘磁道总个数
* @type {Number}
*/
const track = 150;
/**
* 需要生成的磁道序列个数
* @type {Number}
*/
const trackNumber = 40;
/**
* 生成[minNum,maxNum]的随机整数
*
* @param {number} minNum 最小的数
* @param {number} maxNum 最大数
* @return {number} 产生的随机数
*/
function randomNum(minNum,maxNum) {
switch(arguments.length){
case 1:
return parseInt(Math.random() * minNum + 1, 10);
break;
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
break;
default:
return 0;
break;
}
}
/**
* 生成num个磁道号序列
* 其中50%位于 0~49,25%分布在 50~99,25%分布在 100~149
*
* @param {number} num 磁道号序列个数
* @return {Array} trackSequence 磁道号序列数组
*/
function generateTrackNumberSequence(num) {
trackSequence = [];
const firstSequence = num * 0.5;
const secondSequence = num * 0.25;
const thirdSequence = num * 0.25;
for(let i = 0; i <= firstSequence; i++) {
trackSequence.push(randomNum(0,49));
}
for(let i = 0; i <= secondSequence; i++) {
trackSequence.push(randomNum(50,99));
}
for(let i = 0; i <= thirdSequence; i++) {
trackSequence.push(randomNum(100,149));
}
//将数字顺序打乱
for (let i = 1; i < trackSequence.length; i++) {
const random = Math.floor(Math.random() * (i + 1));
[trackSequence[i], trackSequence[random]] = [trackSequence[random], trackSequence[i]];
}
return trackSequence;
}
/**
* 算法选项数组
* @type {Array}
*/
const algorithms = Array.from(document.querySelectorAll('.btn-group > button'));
//用户选择的算法
let algorithmValue = null;
/**
* 记录用户选择的算法选项,并改变该选项的css样式
*
* @param {Object} btn 用户选择的事件对象
*/
function selectAlgorithm(btn) {
algorithmValue = this.dataset.algorithm;
algorithms.forEach(key => key.classList.remove('button-active'));
btn.target.classList.add('button-active');
}
algorithms.forEach(key => key.addEventListener('click', selectAlgorithm));
/**
* 提示信息
*
* @param {String} tips 信息文本
* @param {boolean} bool 是否要提示信息
*/
function showErrorMessage(tips, bool) {
const promptBox = document.querySelector('.error-message');
promptBox.innerHTML = tips;
if(bool) {
promptBox.classList.add('show-error-message');
} else {
promptBox.classList.remove('show-error-message');
}
}
/**
* 检查用户是否选择了算法和输入了磁头初始位置
* 错误时输出相应提示,正确时返回磁头初始位置
*
* @return {string | boolean}
*/
function check() {
const headInitalPosition = document.querySelector('.head-position').value;
if(!algorithmValue) {
showErrorMessage('请选择算法!', true);
return false;
} else if(!headInitalPosition) {
showErrorMessage('请输入磁头初始位置!', true);
return false;
} else {
showErrorMessage('', false);
return headInitalPosition;
}
}
/**
* 先来先服务算法
*
* @param {Array} headPath 磁头路径数组,传入时只包含磁头初始位置
* @return {Array} 磁头将要走过的路径数组
*/
function fcfs(headInitalPosition) {
let headPath = [];
headPath.push(headInitalPosition);
return headPath.concat(trackSequence);
}
/**
* 找到数组最小值并返回下标
*
* @return {number} coor 数组最小值下标
*/
Array.prototype.min = function () {
var min = this[0];
var coor = 0;
this.forEach((ele, index,arr) => {
if(ele < min) {
min = ele;
coor = index;
}
})
return coor;
}
/**
* 最短寻道时间算法
*
* @param {String} headInitalPosition 磁头初始位置
* @return {Array} headPath 磁头路径数组
*/
function sstf(headInitalPosition) {
let headPath = [];
headPath.push(headInitalPosition);
let trackSequenceCopy = trackSequence.concat();
for(let i = 0; i < trackSequence.length; i++) {
let distance = trackSequenceCopy.map(key => Math.abs(key - headPath[i]));
let minCoor = distance.min();
headPath.push(trackSequenceCopy[minCoor]);
trackSequenceCopy.splice(minCoor, 1);
}
return headPath;
}
/**
* 电梯调度算法
*
* @param {number} headInitalPosition 磁头初始位置
* @return {Array} 磁头路径数组
*/
function scan(headInitalPosition) {
let headPath = [];
headPath.push(headInitalPosition);
headPath = headPath.concat((trackSequence.filter(key => key <= headInitalPosition)).sort((a, b) => b - a));
headPath = headPath.concat((trackSequence.filter(key => key > headInitalPosition)).sort((a, b) => a - b));
return headPath;
}
/**
* 循环扫描算法
*
* @param {number} headInitalPosition 磁头初始位置
* @return {Array} 磁头路径数组
*/
function cScan(headInitalPosition) {
let headPath = [];
headPath.push(headInitalPosition);
headPath = headPath.concat((trackSequence.filter(key => key <= headInitalPosition)).sort((a, b) => b - a));
headPath = headPath.concat((trackSequence.filter(key => key > headInitalPosition)).sort((a, b) => b - a));
return headPath;
}
const canvas = document.querySelector('#draw');
const ctx = canvas.getContext('2d');
/**
* 获取css样式
* 如果是height或者width,先去掉单位再返回该属性值
*
* @param {Object} obj 将要获取样式的元素
* @param {String} attr 样式的属性
* @return {Array} cssStyleNumber 属性值
*/
function getStyle(obj,attr){
const cssStyle = obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
if(attr == 'height' || attr == 'width') {
return cssStyle.slice(0, cssStyle.length - 2);
}
return cssStyle;
}
/**
* 画线的方法
*
* @param {number} x1 起点横坐标
* @param {number} y1 起点纵坐标
* @param {number} x2 终点横坐标
* @param {number} y2 终点纵坐标
*/
function drawLine(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.closePath();
}
/**
* 显示坐标轴上的标记文本
*
* @param {String} text 要显示的文本
* @param {Number} xPos 横坐标
* @param {Number} yPos 纵坐标
*/
function drawText(text, xPos, yPos) {
ctx.textAlign = "center";
ctx.font = "20px Arial";
ctx.fillText(text, xPos, yPos);
}
/**
* 绘制X轴及标记点
*
* @param {Array} xCoorArray 按升序排好的横坐标数组
*/
function drawCoordinateAxis(xCoorArray) {
ctx.strokeStyle = '#566a80';
ctx.fillStyle = '#566a80';
ctx.lineWidth = 2;
for(let i = 0; i < xCoorArray.length; i++) {
const startXCoor = (xCoorArray[i] / track) * canvas.width;
const endYCoor = (xCoorArray[i + 1] / track) * canvas.width;
drawText(xCoorArray[i], startXCoor, 25);
drawLine(startXCoor, 40, endYCoor, 40);
drawLine(startXCoor, 40, startXCoor, 30);
}
}
/**
* 画点
*
* @param {Number} xPos 圆心横坐标
* @param {Number} yPos 圆心纵坐标
* @param {Number} size 圆半径
*/
function drawPoint(xPos, yPos, size) {
ctx.beginPath();
ctx.arc(xPos, yPos, size, 0, Math.PI * 2, true);
ctx.fill();
}
/**
* 绘制折线图
*
* @param {Array} headPath 磁头轨迹数组
*/
function drawLineChart(headPath) {
ctx.strokeStyle = '#566a80';
ctx.fillStyle = '#566a80';
ctx.lineWidth = 2;
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
//起点和终点纵坐标
let startYCoor = 50;
let endYCoor = 60;
for(let i = 0; i < headPath.length; i++) {
//起点和终点横坐标
let startXCoor = (headPath[i] / track) * canvas.width;
let endXCoor = (headPath[i + 1] / track) * canvas.width;
if(startXCoor == endXCoor) {
endYCoor = startYCoor;
}
drawPoint(startXCoor, startYCoor, 5);
drawLine(startXCoor, startYCoor, endXCoor, endYCoor);
startYCoor = endYCoor;
endYCoor += 10;
}
}
/**
* 展示图
*
* @param {Array} headPath 磁头轨迹数组
*/
function showCanvas(headPath) {
//清除画布
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = getStyle(canvas, 'width') * 2;
canvas.height = getStyle(canvas, 'height') * 2;
let xCoorArray = []
xCoorArray = xCoorArray.concat(headPath);
//横坐标数组
xCoorArray.sort((a, b) => a - b);
console.log(`横坐标数组:${xCoorArray}`);
drawCoordinateAxis(xCoorArray);
drawLineChart(headPath);
}
/**
* 显示磁头移动道数
*
* @param {Array} headPath 磁头移动路径
*/
function showMoveNumber(headPath) {
let moveNumber = [];
for(let i = 1; i < headPath.length; i++) {
moveNumber.push(Math.abs(headPath[i] - headPath[i - 1]));
}
showErrorMessage(`磁头移动道数:${moveNumber.reduce((a, b) => a + b)}`, true);
}
/**
* 生成随机磁道序列
* 根据用户选择的算法和磁头初始位置绘制效果图
*
*/
function start() {
const headInitalPosition = check();
if(headInitalPosition && algorithmValue) {
generateTrackNumberSequence(trackNumber);
console.log(`随机生成的磁道序列:${trackSequence}`);
let headPath = [];
switch(algorithmValue) {
case 'fcfs':
headPath = fcfs(headInitalPosition);
break;
case 'sstf':
headPath = sstf(headInitalPosition);
break;
case 'scan':
headPath = scan(headInitalPosition);
break;
case 'c-scan':
headPath = cScan(headInitalPosition);
break;
}
console.log(`磁头轨迹:${headPath}`);
showCanvas(headPath);
showMoveNumber(headPath);
}
}
document.querySelector('.start-btn').addEventListener('click', start);