-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.js
67 lines (59 loc) · 1.77 KB
/
display.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
function Display(graph) {
this.canvas = $('#map_canvas')[0];
this.graph = graph;
this.radius = 10;
if (this.canvas.getContext) {
this.ctx = this.canvas.getContext('2d');
this.ctx.lineWidth = 3;
this.ctx.strokeStyle = '#f00';
}
this.out = function(txt) {
if (typeof(txt) == 'object' && !$.isArray(txt)) {
var new_txt = [];
for(var i in txt) {
new_txt.push(i + ': ' + txt[i]);
}
txt = new_txt.join(', ');
}
cur = $('#c').html();
$('#c').html(cur + '\n' + txt);
};
this.draw = function(path) {
var points = new Array();
for(var i=0;i<path.length;i++) {
points.push([this.graph.coords[path[i]][0], this.graph.coords[path[i]][1]]);
}
var corners = new Array;
for(i=points.length-2;i>0;i--) {
var a = points[i-1];
var b = points[i];
var c = points[i+1];
var b1 = this.shorten_edge(a,b,this.radius);
var b2 = this.shorten_edge(c,b,this.radius);
corners[i] = [b1,b2];
}
for(i=points.length-2;i>0;i--) {
points.splice(i,1,corners[i][0],corners[i][1]);
}
this.ctx.moveTo(points[0][0],points[0][1]);
for(i=1;i<points.length;i++) {
this.ctx.lineTo(points[i][0],points[i][1]);
}
this.ctx.stroke();
};
this.shorten_edge = function(p1,p2,length) {
var p3 = [p2[0],p2[1]];
if (p1[0] == p2[0]) {
p3[1] = p2[1] + (1-2*(p1[1]<p2[1]))*length;
} else if (p1[1] == p2[1]) {
p3[0] = p2[0] + (1-2*(p1[0]<p2[0]))*length;
} else {
var m = (p2[1]-p1[1])/(p2[0]-p1[0]);
var mx = Math.round(length*Math.sqrt(1/(1+m*m)));
var my = Math.round(length*Math.sqrt(1/(1+1/(m*m))));
p3[0] = p2[0] + (1-2*(p1[0]<p2[0]))*mx;
p3[1] = p2[1] + (1-2*(m<0))*(1-2*(p1[0]<p2[0]))*my;
}
return p3;
};
}