GraphLink.js
5.91 KB
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
/**
* User: CHT
* Date: 2020/5/8
* Time: 14:01
*/
import {
uuid,
vector,
mark
} from './utils'
import {
direction,
directionVector
} from './types'
export default class GraphLink {
static distance = 15
constructor (options, graph) {
const {
start,
end = null,
startAt = [0, 0],
endAt = [0, 0],
meta = null,
} = options
this.$options = options
const id = options[mark.relationMark] || uuid('link')
this.key = uuid('link')
this[mark.relationMark] = id
this.graph = graph
this.start = start
this.meta = meta
this.end = end
this.startDirection = directionVector[direction.top]
this.endDirection = directionVector[direction.top]
this.startAt = startAt
this.endAt = endAt
}
get end () {
return this._end
}
set end (node) {
if (this.start === node) {
return false
} else {
this._end = node
}
}
get startAt () {
return this._startAt
}
set startAt (offset) {
const relative = this.start.relative(offset)
this._startAt = relative.position
this.startDirection = relative.direction
}
get endAt () {
return this._endAt
}
set endAt (offset) {
if (this.end) {
const relative = this.end.relative(offset)
this._endAt = relative.position
this.endDirection = relative.direction
} else {
this._endAt = offset
}
}
get movePosition () {
return this._movePosition
}
set movePosition (offset) {
this._movePosition = offset
if (this.end) return
const relative = this.start.relative(
vector(offset)
.minus(this.graph.origin)
.minus(this.start.coordinate)
.end
)
this.endDirection = vector(relative.direction)
.multiply(-1)
.end
}
get pathPointList () {
const pointList = this.coordinateList()
, xList = []
, yList = []
pointList.forEach(item => {
xList.push(item[0])
yList.push(item[1])
})
return {
pointList,
xList,
yList,
minX: Math.min(...xList),
maxX: Math.max(...xList),
minY: Math.min(...yList),
maxY: Math.max(...yList)
}
}
startCoordinate () {
return vector(this.start.position)
.add(this.startAt)
.end
}
endCoordinate () {
if (this.end) {
return vector(this.end.position)
.add(this.endAt)
.end
} else {
return this.movePosition
}
}
coordinateList (turnRatio = 0.5) {
const entryPoint = this.startCoordinate()
const exitPoint = this.endCoordinate()
const entryDirection = this.startDirection
let exitDirection = this.endDirection
// 路径起点
const startPoint = vector(entryDirection)
.multiply(GraphLink.distance)
.add(entryPoint)
.end
// 路径终点
const endPoint = vector(exitDirection)
.multiply(GraphLink.distance)
.add(exitPoint)
.end
// 入口方向取反
exitDirection = vector(exitDirection)
.multiply(-1)
.end
// 终点 - 起点 垂直 水平向量
const pathHorizontalVec = [endPoint[0] - startPoint[0], 0]
const pathVerticalVec = [0, endPoint[1] - startPoint[1]]
const startDirection = this.pathDirection(
pathVerticalVec,
pathHorizontalVec,
entryDirection
)
const endDirection = this.pathDirection(
pathVerticalVec,
pathHorizontalVec,
exitDirection
)
const splitNum = vector(startDirection)
.dotProduct(endDirection)
.end > 0 ? 2 : 1
const pathMiddle = endDirection === pathHorizontalVec
? pathVerticalVec
: pathHorizontalVec
let points = []
points.push(entryPoint, startPoint)
if (splitNum === 1) {
const point1 = vector(startPoint)
.add(startDirection)
.end
const point2 = vector(point1)
.add(endDirection)
.end
points.push(point1, point2)
} else {
const point1 = vector(startDirection)
.multiply(turnRatio)
.add(startPoint)
.end
const point2 = vector(point1)
.add(pathMiddle)
.end
const point3 = vector(endDirection)
.multiply(1 - turnRatio)
.add(point2)
.end
points.push(point1, point2, point3)
}
points.push(exitPoint)
return points
}
pathDirection (vertical, horizontal, direction) {
if (
vector(horizontal)
.parallel(direction)
.end
) {
if (
vector(horizontal)
.dotProduct(direction)
.end > 0
) {
return horizontal
} else {
return vertical
}
} else {
if (
vector(vertical)
.dotProduct(direction)
.end > 0
) {
return vertical
} else {
return horizontal
}
}
}
isPointInLink (position, pathPointList) {
const {
pointList,
minX,
minY,
maxX,
maxY
} = pathPointList || this.pathPointList
const n = 5
if (
position[0] < minX - n
|| position[0] > maxX + n
|| position[1] < minY - n
|| position[1] > maxY + n
) {
return false
}
for (let i = 0; i < pointList.length - 2; i++) {
const prev = pointList[i]
const current = pointList[i + 1]
const top = Math.min(prev[1], current[1]) - n
const right = Math.max(prev[0], current[0]) + n
const bottom = Math.max(prev[1], current[1]) + n
const left = Math.min(prev[0], current[0]) - n
const [x, y] = position
if (x > left && x < right && y > top && y < bottom) {
return true
}
}
return false
}
remove () {
return this.graph.removeLink(this)
}
toJSON () {
return {
[mark.relationMark]: this[mark.relationMark],
[mark.startMark]: this.start[mark.relationMark],
[mark.endMark]: this.end[mark.relationMark],
startAt: this.startAt,
endAt: this.endAt,
meta: this.meta
}
}
}