-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS1101S M19 Anomaly in focus.js
75 lines (66 loc) · 2.26 KB
/
CS1101S M19 Anomaly in focus.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
// Q1
function red_rectangle_stream(s) {
function rectangles(arr) {
function top_row() {
for (let i = 0; i < HEIGHT; i = i + 1) {
for (let j = 0; j < WIDTH; j = j + 1) {
if (arr[i][j][1] === 0 && arr[i][j][2] === 0 && arr[i][j][0] > 0) {
return i;
}
}
}
}
function bottom_row() {
for (let i = HEIGHT-1; i > 0; i = i - 1) {
for (let j = WIDTH-1; j > 0; j = j - 1) {
if (arr[i][j][1] === 0 && arr[i][j][2] === 0 && arr[i][j][0] > 0) {
return i;
}
}
}
}
function first_column() {
for (let j = 0; j < WIDTH; j = j + 1) {
for (let i = 0; i < HEIGHT; i = i + 1) {
if (arr[i][j][1] === 0 && arr[i][j][2] === 0 && arr[i][j][0] > 0) {
return j;
}
}
}
}
function last_column() {
for (let j = WIDTH-1; j > 0; j = j - 1) {
for (let i = HEIGHT-1; i > 0; i = i - 1) {
if (arr[i][j][1] === 0 && arr[i][j][2] === 0 && arr[i][j][0] > 0) {
return j;
}
}
}
}
return pair(pair(top_row(),first_column()), pair(bottom_row(),last_column()));
}
return stream_map(rectangles,s);
}
head(red_rectangle_stream(anomaly_stream));
// should evaluate to: [[141, 191], [159, 209]]
// Q2
function trim(image, rectangle) {
const trimmed = [];
const i_min = head(head(rectangle));
const j_min = tail(head(rectangle));
const i_max = head(tail(rectangle));
const j_max = tail(tail(rectangle));
for (let i = i_min; i <= i_max; i = i + 1) {
const new_i = i - i_min;
trimmed[new_i] = [];
for (let j = j_min; j <= j_max; j = j + 1) {
const new_j = j - j_min;
trimmed[new_i][new_j] = image[i][j];
}
}
return trimmed;
}
const focused_stream = stream_combine(
trim,
anomaly_stream,
red_rectangle_stream(anomaly_stream));