-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge.js
164 lines (127 loc) · 3.37 KB
/
edge.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
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
function Edge(node) {
this.start = node;
this.end = null;
this.id = Edge.nextId++;
}
Edge.nextId = 0;
Edge.prototype.setEndXY = function(x, y) {
this.end = new _End(x, y);
}
Edge.prototype.setEnd = function(node) {
if(this.start == node)
throw "Edge can't have equal start and end nodes.";
this.end = node;
}
Edge.prototype._drawTriangle = function() {
ctx.beginPath();
ctx.moveTo(-5, -5);
ctx.lineTo(5, 0);
ctx.lineTo(-5, 5);
ctx.closePath();
ctx.fill();
}
Edge.prototype._lineIntersect = function(Sx, Sy, dx, dy, Ex, Ey, vx, vy) {
var divisor = vx * dy - vy * dx;
if(divisor == 0)
return null;
var j = (Ey * dx - Ex * dy + Sx * dy - Sy * dx) / divisor;
return {
x: Ex + j * vx,
y: Ey + j * vy
}
}
Edge.prototype._distance = function(x1, y1, x2, y2) {
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
Edge.prototype.draw = function() {
if(this.end == null)
return;
var SCx = this.start.getCenterX();
var SCy = this.start.getCenterY();
var ECx = this.end.getCenterX();
var ECy = this.end.getCenterY();
// draw line of the edge
ctx.beginPath();
ctx.moveTo(SCx, SCy);
ctx.lineTo(ECx, ECy);
ctx.stroke();
// draw arrow of the edge
ctx.save();
var Sx = this.start.x;
var Sy = this.start.y;
var Ex = this.end.x;
var Ey = this.end.y;
var dx = ECx - SCx;
var dy = ECy - SCy;
var angle = Math.atan2(dy, dx);
if(this.end instanceof Node) {
var w = this.end.width;
var h = this.end.height;
// cp stands for closest point
var cp = this._lineIntersect(SCx, SCy, dx, dy, Ex, Ey, 1, 0);
if(cp && !this.end.isInside(cp.x, cp.y))
cp = null;
// p stands for point
var p = this._lineIntersect(SCx, SCy, dx, dy, Ex, Ey, 0, 1);
if(cp == null)
cp = p;
else if(p != null && this._distance(SCx, SCy, p.x, p.y) < this._distance(SCx, SCy, cp.x, cp.y) && this.end.isInside(p.x, p.y))
cp = p;
p = this._lineIntersect(SCx, SCy, dx, dy, Ex + w, Ey + h, 1, 0);
if(cp == null)
cp = p;
else if(p != null && this._distance(SCx, SCy, p.x, p.y) < this._distance(SCx, SCy, cp.x, cp.y) && this.end.isInside(p.x, p.y))
cp = p;
p = this._lineIntersect(SCx, SCy, dx, dy, Ex + w, Ey + h, 0, 1);
if(cp == null)
cp = p;
else if(p != null && this._distance(SCx, SCy, p.x, p.y) < this._distance(SCx, SCy, cp.x, cp.y) && this.end.isInside(p.x, p.y))
cp = p;
if(cp == null || !this.end.isInside(cp.x, cp.y)) {
ctx.restore();
return;
}
var dist = this._distance(0, 0, dx, dy);
var ndx = dx/dist;
var ndy = dy/dist;
ctx.translate(cp.x - 5 * ndx, cp.y - 5 * ndy);
} else
ctx.translate(ECx, ECy);
ctx.rotate(angle);
this._drawTriangle();
ctx.restore();
}
Edge.prototype.isInside = function(x, y) {
var SCx = this.start.getCenterX();
var SCy = this.start.getCenterY();
var ECx = this.end.getCenterX();
var ECy = this.end.getCenterY();
var dx = ECx - SCx;
var dy = ECy - SCy;
var p = this._lineIntersect(SCx, SCy, dx, dy, x, y, dx, -dy);
return this._distance(x, y, p.x, p.y) < 5;
}
Edge.prototype.hold = function() {
held = this;
}
Edge.prototype.release = function() {
held = null;
}
Edge.prototype.delete = function() {
var i = edges.indexOf(held);
if(i > -1)
edges.splice(i, 1);
}
Edge.prototype.move = function(x, y) {
this.setEndXY(x, y);
}
function _End(x, y) {
this.x = x;
this.y = y;
}
_End.prototype.getCenterX = function() {
return this.x;
}
_End.prototype.getCenterY = function() {
return this.y;
}