-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateMaxDistances.html
213 lines (205 loc) · 6.96 KB
/
calculateMaxDistances.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Max Distance Calculator</title>
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script>
$(function() {
var brainType = location.hash.split("#")[1] || "human";
var brainTypes = ["dolphin", "human"];
brainType = brainType.toLowerCase();
if (!(brainTypes.includes(brainType))) brainType = "human";
var $canvas = $("<canvas/>").css("opacity", "0.5");
var brainType = "human"
$canvas[0].width = 1000;
$canvas[0].height = 1000;
var questions = {};
var questionData;
var ctx = $canvas[0].getContext('2d');
ctx.strokeStyle = 'white'
ctx.fillStyle = 'orange';
$('body').append([$canvas]);
$.get(brainType+"/questionBank.json", function(qData) {
questionData = qData;
console.log(questionData);
$.get(brainType+"/structures.json", function(data) {
//for each question
for (i in data) {
console.log(i);
var index = questionData.findIndex(p => p.region == i);
var required = questionData[index].pointsPerLayer;
console.log(required);
// console.log("Starting ", i);
//ctx.fillStyle = 'rgb(255,165,0)';
//for each layers
var layer = {};
for (j in data[i]) {
for (k in data[i][j]) {
var points = data[i][j][k];
for (pointNum = 0; pointNum < points.length/2; pointNum++) {
if (pointNum == 0) {
ctx.beginPath();
ctx.moveTo(points[pointNum*2], points[pointNum*2+1]);
}
else {
ctx.lineTo(points[pointNum*2], points[pointNum*2+1]);
}
}
ctx.closePath();
ctx.stroke();
}
var newPoints = [];
for (xIt = 0; xIt < 1000; xIt++) {
for (yIt = 0; yIt <1000; yIt ++) {
if(ctx.isPointInPath(xIt,yIt)) {
newPoints.push(xIt,yIt);
}
}
}
console.log(newPoints);
layer[j] = maxDistance(orderedPair(newPoints), required);
// }
// var pixels = ctx.getImageData(0,0,1000,1000);
// var pixelData = pixels.data;
// var total = 0;
// for (pixelNum=0; pixelNum<pixelData.length/4; pixelNum+=1) {
// var red = pixelNum[i*4];
// if (red>100) total++;
// }
// ctx.clearRect(0,0,1000,1000);
// console.log(j);
// layer[j] = total;
// console.log(total);
}
questions[i] = layer;
//console.log("Ending ", i);
}
console.log(JSON.stringify(questions));
//ctx.putImageData(pixelData, 0, 0);
});
});
});
function maxDistance(points, required) {
var indexes = [];
var max = 0;
for (it = 0; it<required; it++) {
if (points[it]) indexes[it] = it;
}
//var skipper = Math.ceil(points.length/5);
var skipper = 1;
var completedPoints = 1; //dtarts at one since zero-based indexing.
var currentIndex = 0;
while (indexes[0]<points.length) {
var newValue = indexes[currentIndex];
if (newValue>=points.length) {
indexes[currentIndex]=points.length-1;
currentIndex--;
completedPoints++;
continue;
}
if (newValue<=points.length && currentIndex<required-completedPoints) {
currentIndex++;
indexes[currentIndex] = indexes[currentIndex-1]+skipper;
continue;
}
var arra = [];
for (var pointNum = required-1; pointNum>=0; pointNum--) {
arra.push(points[indexes[pointNum]]);
}
//console.log(arra);
max = compare(arra,max);
indexes[currentIndex]+=skipper;
//caclulate distance here.
}
//console.log(max);
return max;
}
function compare(arr, max) {
var dist = 0;
var divider = Math.pow(arr.length, 2);
for (a = 0; a < arr.length; a++) {
for (b = 0; b < arr.length; b++) {
var x = arr[a].x - arr[b].x;
var y = arr[a].y - arr[b].y;
dist += Math.sqrt(x * x + y * y);
}
}
dist/=divider;
if (dist > max) {
max = dist;
}
return max;
}
//
// function subsetIterator(arr, length) {
// var max = 0;
// var subset = [];
// if (length < 2) return 0;
// else if (length<6) {
// for (a = 0; a < arr.length; a += skips) {
// subset[0] = arr[a];
// if(length-1==subset.length) {
// max=compare(subset, max);
// continue;
// }
// for (b = 0; b < arr.length; b += skips) {
// if (a == b) continue
// subset[1] = arr[b];
// if(length-1==subset.length) {
// max=compare(subset, max);
// continue;
// }
// for (c = 0; c < arr.length; c += skips) {
// if (a == c && b == c) continue
// subset[2]= arr[c];
// if(length-1==subset.length) {
// max=compare(subset, max);
// continue;
// }
// for (d = 0; d < arr.length; d += skips) {
// if (a == d || b == d || c == d) continue
// subset[3]= arr[d];
// if(length-1==subset.length) {
// max=compare(subset, max);
// continue;
// }
// for (e = 0; e < arr.length; e += skips) {
// if (a == e || b == e || c == e || d == e) continue;
// subset[4]= arr[e];
// if(length-1==subset.length) {
// max=compare(subset, max);
// continue;
// }
// for (f = 0; f < arr.length; f += skips) {
// if (a == f || b == f || c == f || d == f || e == f) continue;
// subset[5]= arr[f];
// max=compare(subset, max);
// }
// }
// }
// }
// }
// }
// }
// console.log("end layer");
// return max;
// }
// Example:
function orderedPair(array) {
var retArray = [];
for (loop = 0; loop < array.length - 1; loop += 2) {
var obj = {};
obj.x = array[loop];
obj.y = array[loop + 1];
retArray.push(obj);
}
return retArray;
}
</script>
</head>
<body>
<div id="root"> Please note, this computation may take a long time...</div>
</body>
</html>