-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
137 lines (117 loc) · 2.78 KB
/
sketch.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
var low, mid, high, n;
low = mid = 0
n = 30
high = n - 1;
var flagO = [100, 100]
/*
invariant
red = [0..low-1]
white = [low..mid-1]
undecided = [mid..high-1]
blue = [high..n-1]
*/
var stripes
function setup() {
frameRate(5)
createCanvas(1000, 600);
background(0);
stripes = Array(floor(n / 3)).fill(new Strip(1)).concat(Array(floor(n / 3)).fill(new Strip(2))).concat(Array(n - 2 * floor(n / 3)).fill(new Strip(3)))
stripes = shuffle(stripes);
printS(stripes)
fill(220);
stroke(255, 0, 255)
rect(flagO[0], flagO[1], 500, 300)
}
class Strip {
constructor(color) {
this.color = color;
this.width = 500
this.height = floor(300 / n)
}
}
function printS(S) {
var op = ""
S.forEach(s => op += `${s.color} `)
console.log(op)
}
var notComplete = true;
function draw() {
background(0)
if (mid <= high) {
switch (stripes[mid].color) {
case 1:
[stripes[mid], stripes[low]] = [stripes[low], stripes[mid]]
low++;
mid++;
break;
case 2:
mid++;
break;
case 3:
[stripes[mid], stripes[high]] = [stripes[high], stripes[mid]]
high--;
}
} else {
notComplete = false;
}
var index = 0;
var h = 0,
w = 500
//for drawing
while (index < n) {
//for boundary of undecided and white
if (notComplete && (index == mid || index == low)) {
strokeWeight(3)
stroke(0)
line(flagO[0], flagO[1] + h, flagO[0] + w, flagO[1] + h)
strokeWeight(1)
var tag = (index == mid) ? " mid": " low";
fill(255)
text(tag , flagO[0] + w, flagO[1] + h + stripes[0].height)
}
if (mid <= index && index <= high) {
switch (stripes[index].color) {
case 1:
fill(255, 0, 0, 200)
// fill(0, 255, 255) // cyan
break;
case 2:
fill(255, 255, 255, 200)
// fill(0, 255, 0)
break;
case 3:
fill(0,0 , 255, 200)
// fill(255, 255, 0) //yellow
}
} else {
switch (stripes[index].color) {
case 1:
fill(255, 0, 0)
break;
case 2:
fill(255)
break;
case 3:
fill(0, 0, 255)
}
}
noStroke()
// if(!notComplete)
// console.error(h)
rect(flagO[0], flagO[1] + h, w, stripes[index].height)
//for boundary of undecided
if (notComplete && index == high) {
strokeWeight(3)
stroke(0)
// console.error('kya hai be')
line(flagO[0], flagO[1] + h + stripes[0].height, flagO[0] + w, flagO[1] + h + stripes[0].height)
fill(255)
text(" high", flagO[0] + w, flagO[1] + h + stripes[0].height)
strokeWeight(1)
}
h += stripes[index].height
index++;
}
if (!notComplete)
printS(stripes)
}