@@ -265,6 +265,32 @@ function findRoute(
265265 return null
266266}
267267
268+ function snap ( vertices : Point [ ] , gridSize = 10 ) {
269+ if ( vertices . length <= 1 ) {
270+ return vertices
271+ }
272+
273+ for ( let i = 0 , len = vertices . length ; i < len - 1 ; i += 1 ) {
274+ const first = vertices [ i ]
275+ const second = vertices [ i + 1 ]
276+ if ( first . x === second . x ) {
277+ const x = gridSize * Math . round ( first . x / gridSize )
278+ if ( first . x !== x ) {
279+ first . x = x
280+ second . x = x
281+ }
282+ } else if ( first . y === second . y ) {
283+ const y = gridSize * Math . round ( first . y / gridSize )
284+ if ( first . y !== y ) {
285+ first . y = y
286+ second . y = y
287+ }
288+ }
289+ }
290+
291+ return vertices
292+ }
293+
268294export const router : Router . Definition < ManhattanRouterOptions > = function (
269295 vertices ,
270296 optionsRaw ,
@@ -282,7 +308,7 @@ export const router: Router.Definition<ManhattanRouterOptions> = function (
282308 )
283309
284310 const oldVertices = vertices . map ( ( p ) => Point . create ( p ) )
285- let newVertices : Point [ ] = [ ]
311+ const newVertices : Point [ ] = [ ]
286312
287313 // The origin of first route's grid, does not need snapping
288314 let tailPoint = sourceEndpoint
@@ -351,10 +377,7 @@ export const router: Router.Definition<ManhattanRouterOptions> = function (
351377 }
352378
353379 if ( options . snapToGrid ) {
354- newVertices = newVertices . map ( ( vertice ) => {
355- const gridSize = edgeView . graph . grid . getGridSize ( )
356- return vertice . snapToGrid ( gridSize )
357- } )
380+ return snap ( newVertices , edgeView . graph . grid . getGridSize ( ) )
358381 }
359382
360383 return newVertices
0 commit comments