-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo.html
862 lines (808 loc) · 35.4 KB
/
geo.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dynamic display system:geo-based location</title>
<link rel="stylesheet" href="static/style.css">
<style>
body {
background-image: url("world_map_gif.gif");
background-size: 1600px 800px;
background-position: 40px 40px;
background-repeat: no-repeat;
}
.photo {
border: solid;
}
.wrapper {
float: left;
}
.axis path,
.axis line {
fill: none;
stroke: #557799;
shape-rendering: crispEdges;
}
.info {
margin: 40px 4px 0 4px;
float: right;
width: 112px;
height: 40px;
color: #7fdbff;
}
</style>
<script src="static/numeric-1.2.6.js" charset="utf-8"></script>
<script src="static/d3.js" charset="utf-8"></script>
<script src="static/li.js" charset="utf-8"></script>
<script src="static/jquery-2.2.0.js" charset="utf-8"></script>
<script>
var margin_length = 40;
var margin = {
top: margin_length, right: margin_length,
bottom: margin_length, left: margin_length
};
//this variables is used to construct two rectangles:
// the attract is to hold related pics inside, the push is to push nonrelated pics outside
var scale_rate = {attract: 2, push: 2.5};
//this is the size of display plane
var v = {height: 800, width: 1600};
//pixel length
var pl = 40;
// pixel matrix
// each element in the matrix is a array in which store the id(s) of the photo that cover this pixel
// having more than one element in the list means overlapping.
// pm[j][i] represents the cell located at j-th row and i-th column
var pm = new Array(v.height / pl + 1).join('0').split('').map(function () {
return new Array(v.width / pl + 1).join('0').split('').map(function () {
return [];
});
});
var t_i = 0;
while (t_i < v.height) {
// console.log(t_i + "," + Math.floor(t_i / pl));
t_i++;
}
var geo_w = d3.scale.linear().range([0, v.width]).domain([-180, 180]);
var geo_h = d3.scale.linear().range([0, v.height]).domain([90, -90]);
var coverage = {max: .82, min: .25, stable: .81};
var rate = {max: .9, min: .05, default: .1, step: .035, selected: 1};
var o = {x: 0, y: 0, h: v.height, w: v.width};
var step = 20;
var base_dir = "geo/";
var info_file = "info.csv";
var sim_info_hist = "sim_info_hist.csv";
var sim_info_cosine = "sim_info_cosine.csv";
var sim_info_ghc = "sim_info_ghc.csv";
var sim_matrix;
var dataset;
var push_vertexes = [
{x: 175, y: 42.922},
{x: 1525, y: 42.922},
{x: 1525, y: 937.078},
{x: 175, y: 937.078}];
// puhlic variable that store the related pictures list
var related = [];
var selected = {h: 715.325, w: 1080, x: 310, y: 132.338};
var interval = 1000;
// canvas
var svga;
var x = d3.scale.linear().range([0, v.width]).domain([0, v.width]);
var y = d3.scale.linear().range([0, v.height]).domain([0, v.height]);
var modi = d3.scale.linear().range([0, 3]).domain([3, 0]);
function cal_coverage() {
var total_area = v.height * v.width;
var cover_area = 0.0;
dataset.forEach(function (d) {
cover_area += d.i.rate * d.ExifImageWidth * d.i.rate * d.ExifImageHeight;
});
cvr = cover_area / total_area;
}
//record the coverage rate, more precisely, we sum all pictures' area and
// divide it by the area of displaying plane
var cvr = 0.0;
d3.text(base_dir + sim_info_cosine, function (text) {
var data = d3.csv.parseRows(text).map(function (row) {
return row.map(function (value) {
return +value;
});
});
// console.log(data);
sim_matrix = data.slice();
});
d3.csv(base_dir + info_file, function (error, data) {
if (error) throw error;
dataset = data.slice();
base();
draw_gallery();
})
function exceeds(d, o) {
return d.i.top().y < o.y || d.i.right().x > o.x + o.w || d.i.bottom().y > o.y + o.h || d.i.left().x < o.x;
}
function draw_gallery() {
init();
// console.dir(dataset);
judge0(0);
}
function overlap(d0, d1) {
return doPolygonsIntersect(d0.i.vertexes(),
d1.i.selected && ($.inArray(d0.i.id, related)) < 0 ? push_vertexes : d1.i.vertexes())
&& (d0.FileName != d1.FileName);
}
function base() {
svga = d3.select(".wrapper").append("svg")
.attr("width", v.width + margin.left + margin.right)
.attr("height", v.height + margin.top + margin.bottom)
.append("g").attr("class", "g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var xAxis = d3.svg.axis().scale(x)
.orient("top");
var yAxis = d3.svg.axis().scale(y)
.orient("left");
svga.append("g")
.attr("class", "x axis")
// .attr("transform", "translate(0," + 2 * radius + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", v.width)
.attr("y", -6)
.style("text-anchor", "end")
.text("x axis");
svga.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", -12)
.attr("x", 12 - v.height)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("y axis");
}
function init() {
dataset.forEach(function (d) {
d.i = {
id: parseInt(d.FileName.substr(3, 4)),
translate_x: 0,
translate_y: 0,
rate: rate.default,
rotate: 0,
selected: false
};
d.i.select = function () {
dataset.forEach(function (di) {
di.i.selected = false;
});
d.i.selected = true;
};
d.i.locate = function (x, y, a) {
d.i.translate_x = x - d.i.w() / 2;
d.i.translate_y = y - d.i.h() / 2;
var pmi = d.i.toPixel();
var j1 = pmi[1][0];
var j2 = pmi[1][1];
var i1 = pmi[0][0];
var i2 = pmi[0][1];
pm.forEach(function (p, j) {
if (j >= j1 && j <= j2) {
p.forEach(function (p1, i) {
var index = p1.indexOf(d.i.id);
if (i >= i1 && i <= i2) {
if (index < 0) {
p1.push(d.i.id);
}
} else {
if (index >= 0) {
p1.splice(index, 1);
}
}
});
} else {
p.forEach(function (p1, i) {
var index = p1.indexOf(d.i.id);
if (index >= 0) {
p1.splice(index, 1);
}
});
}
});
};
d.i.update_rate = function (r, a) {
d.i.rate = r;
};
d.i.update_rotate = function (r, a) {
d.i.rotate = r;
};
d.i.width = function () {
if (d.i.rotate / 180.0 == 0) {
return d.i.w();
} else {
return d.i.h();
}
};
d.i.height = function () {
if (d.i.rotate / 180.0 == 0) {
return d.i.h();
} else {
return d.i.w();
}
}
d.i.w = function () {
return d.ExifImageWidth * d.i.rate;
// return d.ExifImageWidth;
};
d.i.h = function () {
return d.ExifImageHeight * d.i.rate;
// return d.ExifImageHeight;
};
/**Orientation indication
1 2 3 4 5 6 7 8
888888 888888 88 88 8888888888 88 88 8888888888
88 88 88 88 88 88 88 88 88 88 88 88
8888 8888 8888 8888 88 8888888888 8888888888 88
88 88 88 88
88 88 888888 888888
*/
switch (d.Orientation) {
case "1":
d.i.rotate = 0;
break;
case "3":
d.i.rotate = 180;
break;
case "6":
d.i.rotate = 90;
break;
case "8":
d.i.rotate = -90;
break;
default:
d.i.rotate = 0;
}
d.i.x = function () {
return d.i.center().x;
};
d.i.y = function () {
return d.i.center().y;
};
// distance from one vertex to center of the rectangle
d.i.radius = function () {
return Math.sqrt(Math.pow(d.i.w() / 2, 2) + Math.pow(d.i.h() / 2, 2));
};
// the angle between (center.x-x0,center.y-y0) and (x,0)
d.i.base_angle = function () {
return rad2deg(Math.atan(d.ExifImageHeight / d.ExifImageWidth));
};
d.i.center = function () {
// center is always this value
// translate_x and translate_y is always this value, no matter rotate or not
return {
x: d.i.translate_x + d.i.w() / 2,
y: d.i.translate_y + d.i.h() / 2
};
};
// return the four vertexes, in original clockwise order
// (0:top-left, 1:top-right,2:bottom-right,3"bottom-left)
d.i.vertexes = function () {
var vertexes = [];
var cent = d.i.center();
vertexes.push({
x: cent.x - d.i.radius() * Math.cos(deg2rad(d.i.rotate + d.i.base_angle())),
y: cent.y - d.i.radius() * Math.sin(deg2rad(d.i.rotate + d.i.base_angle()))
});
vertexes.push({
x: vertexes[0].x + d.i.w() * Math.cos(deg2rad(d.i.rotate)),
y: vertexes[0].y + d.i.w() * Math.sin(deg2rad(d.i.rotate))
});
vertexes.push({
x: vertexes[1].x - d.i.h() * Math.sin(deg2rad(d.i.rotate)),
y: vertexes[1].y + d.i.h() * Math.cos(deg2rad(d.i.rotate))
});
vertexes.push({
x: vertexes[0].x - d.i.h() * Math.sin(deg2rad(d.i.rotate)),
y: vertexes[0].y + d.i.h() * Math.cos(deg2rad(d.i.rotate))
});
return vertexes;
};
d.i.top = function () {
var k = (4 - Math.floor(d.i.rotate / 90) % 4) % 4;
return d.i.vertexes()[k];
};
d.i.right = function () {
var k = (5 - Math.floor(d.i.rotate / 90) % 4) % 4;
return d.i.vertexes()[k];
};
d.i.bottom = function () {
var k = (6 - Math.floor(d.i.rotate / 90) % 4) % 4;
return d.i.vertexes()[k];
};
d.i.left = function () {
var k = (7 - Math.floor(d.i.rotate / 90) % 4) % 4;
return d.i.vertexes()[k];
};
//return a 2 dimention array [[i1,i2],[j1,j2]] in which i1 i2 represent the index boundary of x-axis
// and j1 j2 represent the index boundary of y-axis
d.i.toPixel = function () {
var i1 = Math.floor(d.i.left().x / pl);
var i2 = Math.floor(d.i.right().x / pl);
var j1 = Math.floor(d.i.top().y / pl);
var j2 = Math.floor(d.i.bottom().y / pl);
return [[i1, i2], [j1, j2]];
};
});
dataset.forEach(function (d, i) {
var x = Math.random() * v.width;
var y = Math.random() * v.height;
d.i.locate(x, y);
});
svga.selectAll(".photo").data(dataset, function (p) {
return p.FileName;
}).enter().append("image").attr("transform", function (p, i) {
return "translate(" + p.i.translate_x + ","
+ p.i.translate_y + ")"
+ ",rotate("
+ p.i.rotate
+ "," + p.i.w() / 2 + "," + p.i.h() / 2
+ "),scale(" + p.i.rate + ")"
;
}).attr("width", function (d) {
return d.ExifImageWidth;
}).attr("height", function (d) {
return d.ExifImageHeight;
}).attr("xlink:href", function (p) {
return base_dir + p.FileName;
}).attr("class", "photo")
.on("click", function (d) {
dataset.forEach(function (d0) {
if (d0.i.selected) {
var cent = d.i.center();
d0.i.update_rate(d.i.rate);
d0.i.locate(cent.x, cent.y);
d0.i.update_rate(rate.default);
}
});
d.i.select();
d.i.update_rate(rate.selected);
d.i.locate(v.width / 2, v.height / 2);
related = get_related(d);
var attr;
attr = d.i.vertexes().map(function (k) {
var x = d.i.center().x;
var y = d.i.center().y;
return {
x: k.x > x ? v.width - (v.width - k.x) / 2
: k.x / 2,
y: k.y > y ? v.height - (v.height - k.y) / 2
: k.y / 2
};
});
var push;
push = d.i.vertexes().map(function (k) {
var x = d.i.center().x;
var y = d.i.center().y;
return {
x: k.x > x ? v.width - (v.width - k.x) / 3
: k.x / 3,
y: k.y > y ? v.height - (v.height - k.y) / 3
: k.y / 3
};
});
var px = push.map(function (p) {
return p.x;
});
var py = push.map(function (p) {
return p.y;
});
var sx = attr.map(function (p) {
return p.x;
});
var sy = attr.map(function (p) {
return p.y;
});
var minpx = px.reduce(function (a, b) {
return Math.min(a, b);
});
var maxpx = px.reduce(function (a, b) {
return Math.max(a, b);
});
var minpy = py.reduce(function (a, b) {
return Math.min(a, b);
});
var maxpy = py.reduce(function (a, b) {
return Math.max(a, b);
});
var minsx = sx.reduce(function (a, b) {
return Math.min(a, b);
});
var maxsx = sx.reduce(function (a, b) {
return Math.max(a, b);
});
var minsy = sy.reduce(function (a, b) {
return Math.min(a, b);
});
var maxsy = sy.reduce(function (a, b) {
return Math.max(a, b);
});
var push_rect = {
x: minpx, y: minpy, h: maxpy - minpy, w: maxpx - minpx
};
// /*
selected = {
x: minsx, y: minsy, h: maxsy - minsy, w: maxsx - minsx
};
push_vertexes = push;
svga.selectAll("rect").remove();
svga.append("rect")
.attr("x", selected.x)
.attr("y", selected.y)
.attr("height", selected.h)
.attr("width", selected.w).style("fill-opacity", .1);
svga.append("rect")
.attr("x", push_rect.x)
.attr("y", push_rect.y)
.attr("height", push_rect.h)
.attr("width", push_rect.w).style("fill-opacity", .1);
// */
dataset = dataset.map(function (d) {
if (!d.i.selected) {
if ($.inArray(d.i.id, related) >= 0) {
d.i.update_rate(rate.max);
} else {
d.i.update_rate(rate.default);
}
}
return d;
});
judge0(0);
}
);
}
/**
* Helper function to determine whether there is an intersection between the two polygons described
* by the lists of vertices. Uses the Separating Axis Theorem
*
* @param a an array of connected points [{x:, y:}, {x:, y:},...] that form a closed polygon
* @param b an array of connected points [{x:, y:}, {x:, y:},...] that form a closed polygon
* @return true if there is any intersection between the 2 polygons, false otherwise
*/
function doPolygonsIntersect(a, b) {
var polygons = [a, b];
var minA, maxA, projected, i, i1, j, minB, maxB;
for (i = 0; i < polygons.length; i++) {
// for each polygon, look at each edge of the polygon, and determine if it separates
// the two shapes
var polygon = polygons[i];
for (i1 = 0; i1 < polygon.length; i1++) {
// grab 2 vertices to create an edge
var i2 = (i1 + 1) % polygon.length;
var p1 = polygon[i1];
var p2 = polygon[i2];
// find the line perpendicular to this edge
var normal = {x: p2.y - p1.y, y: p1.x - p2.x};
minA = maxA = undefined;
// for each vertex in the first shape, project it onto the line perpendicular to the edge
// and keep track of the min and max of these values
for (j = 0; j < a.length; j++) {
projected = normal.x * a[j].x + normal.y * a[j].y;
if (isUndefined(minA) || projected < minA) {
minA = projected;
}
if (isUndefined(maxA) || projected > maxA) {
maxA = projected;
}
}
// for each vertex in the second shape, project it onto the line perpendicular to the edge
// and keep track of the min and max of these values
minB = maxB = undefined;
for (j = 0; j < b.length; j++) {
projected = normal.x * b[j].x + normal.y * b[j].y;
if (isUndefined(minB) || projected < minB) {
minB = projected;
}
if (isUndefined(maxB) || projected > maxB) {
maxB = projected;
}
}
// if there is no overlap between the projects, the edge we are looking at separates the two
// polygons, and we know there is no overlap
if (maxA < minB || maxB < minA) {
// console.log("polygons don't intersect!");
return false;
}
}
}
return true;
}
function isUndefined(a) {
return a == undefined;
}
function rad2deg(r) {
return r * 180 / Math.PI
}
function deg2rad(d) {
return d * Math.PI / 180;
}
//this function is called after the system init, to solve exceed and overlap
function judge0(count) {
if (((dataset.some(function (d) {
return d.i.selected;
}) || cvr > coverage.stable) && dataset.every(function (d0) {
return !exceeds(d0, o) &&
dataset.every(function (d1) {
return !overlap(d0, d1);
});
}))
|| count > 2 * step) {
// console.log("stable at step:" + count);
update_draw(count);
setTimeout(function () {
dataset.forEach(function (d) {
// console.dir(d);
d.i.locate(geo_w(d.Longitude), geo_h(d.Latitude));
});
cycle(0, true);
}, 1000);
} else {
cycle(count, false);
}
}
function cycle(count, geo) {
cal_coverage();
update_draw(count);
setTimeout(function () {
dataset.filter(function (d) {
return !d.i.selected;
}).sort(function (a, b) {
var ea = exceeds(a, o), eb = exceeds(b, o);
if (ea && !eb) {
return -1;
}
if (eb && !ea) {
return 1;
}
return 0;
}).forEach(function (d) {
/*solve exceeds*/
var exco;
var unit = 300;
if (geo) {
console.dir(d);
// geo_w(d.Longitude), geo_h(d.Latitude)
exco = {
x: geo_w(d.Longitude) - unit / 2,
y: geo_h(d.Latitude) - unit / 2,
w: unit,
h: unit
}
} else {
exco = $.inArray(d.i.id, related) >= 0 ?
selected : o;
}
var solved = solve_exceeds(exco, d, {x: d.i.x(), y: d.i.y()});
var exceedsbool = solved.exceeds;
// var adjacent = dataset.filter(function (d1) {
// return overlap(d, d1);
// }).slice();
// if (adjacent.length == 0) {
// if (d.i.rate < rate.max && cvr < coverage.max && !d.i.selected) {
// d.i.update_rate(d.i.rate + rate.step);
// }
// } else {
var overlapbool = false;
/* solve overlap*/
dataset.forEach(function (d1) {
if (d.i.id == d1.i.id) {
return;
}
if (overlap(d, d1)) {
overlapbool = true;
//we see this rectangle as a circle with radius (width+height)/2 to make this process easier
var r = (d.i.w() + d.i.h()) / 2;
var r1 = (d1.i.w() + d1.i.h()) / 2;
r1 = d1.i.selected ? r1 * scale_rate.push : r1;
//vx and vy denote the move direction vector
var vx = d.i.x() - d1.i.x();
var vy = d.i.y() - d1.i.y();
//min distance between two circles to ensure no overlap
var vd = Math.sqrt(vx * vx + vy * vy);
var vdx = vd == 0 ? 0 : ( vx / vd) * (r + r1 - vd) / step;
var vdy = vd == 0 ? 0 : ( vy / vd) * (r + r1 - vd) / step;
// x += vdx;
// y += vdy;
if (vdx >= 0) {
solved.xp += vdx;
} else {
solved.xn += vdx;
}
if (vdy >= 0) {
solved.yp += vdy;
} else {
solved.yn += vdy;
}
}
});
if (!overlapbool && !exceedsbool) {
} else {
if (solved.yn * solved.yp != 0 || solved.xn * solved.xp != 0) {
//TODO
if (d.i.rate > rate.min && cvr > coverage.min && !d.i.selected) {
d.i.update_rate(d.i.rate - rate.step / 2);
}
} else {
}
d.i.pre_solved = solved;
// }
var x = d.i.x() + (solved.xn + solved.xp );
var y = d.i.y() + (solved.yn + solved.yp );
d.i.locate(x, y);
}
});
judge0(count + 1);
}, 0);
}
function update_draw(count) {
d3.select(".cvr").text(Math.round(cvr * 1000) / 1000);
d3.select(".cnt").text(count);
update_draw_dataset(dataset, interval);
}
function get_related(d) {
var index = d.i.id;
var res = sim_matrix[index].map(function (m, i) {
return {index: i, value: m};
}).sort(function (a, b) {
return b.value - a.value;
}).filter(function (m, i) {
// return the first 12 photos in photo set
return i < 12;
}).map(function (m) {
return m.index;
});
return res;
}
function update_draw_dataset(dataset, interval) {
svga.selectAll(".photo").data(dataset, function (p) {
return p.FileName;
}).style("fill", function (p) {
return $.inArray(p.i.id, related) >= 0 ? "red" : "blue";
}).transition().duration(interval).ease("linear").attrTween("transform", function (p, index) {
var current = d3.select(this).attr("transform");
var to = "translate("
+ p.i.translate_x + ","
+ p.i.translate_y + "),rotate("
+ p.i.rotate
+ "," + p.i.w() / 2 + "," + p.i.h() / 2
+ "),scale(" + p.i.rate + ")";
return d3.interpolateString(current, to);
});
var pmlist = [];
pm.forEach(function (p, j) {
p.forEach(function (p1, i) {
pmlist.push({j: j, i: i, value: p1});
});
});
}
//o is the container rectangle, d is the displaying pic
function solve_exceeds(o, d, obj) {
var xp = 0, xn = 0, yp = 0, yn = 0;
var exceeds = false;
var x = obj.x, y = obj.y;
if (0 < o.y - d.i.top().y) {
exceeds = true;
yp += o.y - d.i.top().y;
}
if (0 > o.h + o.y - d.i.bottom().y) {
exceeds = true;
yn += o.h + o.y - d.i.bottom().y;
}
if (0 < o.x - d.i.left().x) {
exceeds = true;
xp += o.x - d.i.left().x;
}
if (0 > o.w + o.x - d.i.right().x) {
exceeds = true;
xn += o.w + o.x - d.i.right().x;
}
return {xp: xp, xn: xn, yp: yp, yn: yn, exceeds: exceeds};
}
function createArray(length) {
var arr = new Array(length || 0),
i = length;
if (arguments.length > 1) {
var args = Array.prototype.slice.call(arguments, 1);
while (i--) arr[length - 1 - i] = createArray.apply(this, args);
}
return arr;
}
//d is one photo object, we find the blank around it
function findBlank(d, pm) {
var j1, j2, i1, i2;
var pixelindex = d.i.toPixel();
i1 = pixelindex[0][0];
i2 = pixelindex[0][1];
j1 = pixelindex[1][0];
j2 = pixelindex[1][1];
var jmax = v.height / pl;
var imax = v.width / pl;
//four boolean variables indicate whether we can continue on this direction
var bj1 = j1 > 0, bj2 = j2 < jmax - 1, bi1 = i1 > 0, bi2 = i2 < imax - 1;
while (bj1 || bj2 || bi1 || bi2) {
// goes left goes from pm[i1][j1-1] to pm[i2][j1-1]
if (bi1) {
if (pm.filter(function (p, i) {
return i >= i1 && i <= i2;
}).every(function (p) {
if (p[i1 + 1] == null) {
console.dir(i1);
}
return p[i1 - 1].length == 0;
})) {
i1 = i1 - 1;
if (i1 == 0) {
bi1 = false;
}
} else {
bi1 = false;
}
}
// goes right goes from pm[i1][i2+1] to pm[i2][i2+1]
if (bi2) {
if (pm.filter(function (p, i) {
return i >= i1 && i <= i2;
}).every(function (p) {
if (p[i2 + 1] == null) {
console.dir(i2);
}
return p[i2 + 1].length == 0;
})) {
i2 = i2 + 1;
if (i2 == imax - 1) {
bi2 = false;
}
} else {
bi2 = false;
}
}
if (bj1) {
if (pm[j1 - 1] == null) {
console.dir(j1);
}
if (pm[j1 - 1].filter(function (p, j) {
return j >= j1 && j <= j2;
}).every(function (p) {
return p.length == 0;
})) {
j1 = j1 - 1;
if (j1 == 0) {
bj1 = false;
}
} else {
bj1 = false;
}
}
if (bj2) {
if (pm[j2 - 1] == null) {
console.dir(j2);
}
if (pm[j2 + 1].filter(function (p, j) {
return j >= j1 && j <= j2;
}).every(function (p) {
return p.length == 0;
})) {
j2 = j2 + 1;
if (j2 == jmax - 1) {
bj2 = false;
}
} else {
bj2 = false;
}
}
}
return [[i1, i2], [j1, j2]];
}
</script>
</head>
<body>
<div class="wrapper"></div>
</body>
</html>