Blazor.Diagrams icon indicating copy to clipboard operation
Blazor.Diagrams copied to clipboard

Rounding issue in the Orthogonal Router calculation

Open roulinthomas opened this issue 4 years ago • 1 comments

Hi,

I noticed that, after zooming in/out and redrawing the chart by defining a different list of nodes and links, some of my orthogonal lines were drawn as straight lines (normal route). After a little investigation, it seems that it is due to a rounding issue somewhere that creates an incorrect graph on which these lines are drawn. The algorithm can't find a path from origin to destination and relies on the normal route by default.

In my example case, in the CreateGraph method, the hotX list had these two distinct values : 540 and 540.00006424999981. And the algorithm fails to create a connection between these two values through the list of spots.

I added a rounding operation in Router.Orthogonal.cs > CreateGraph().

spots.ForEach(p =>
            {
                (var x, var y) = p;
                
                // Rounding added here.
                x = Math.Round(x, 2);
                y = Math.Round(y, 2);
                
                if (!hotXs.Contains(x)) hotXs.Add(x);
                if (!hotYs.Contains(y)) hotYs.Add(y);
                graph.Add(new Point(x, y));
            });

And in the call to the ShortestPath method in Router.Orthogonal.cs > Orthogonal().

var path = ShortestPath(graph, new Point(Math.Round(origin.X, 2), Math.Round(origin.Y, 2)), new Point(Math.Round(destination.X, 2), Math.Round(destination.Y, 2)));

This solved my problem and the grid was generated successfully. This little fix is only a workaround though and, in my opinion, this rounding problem should be identified and corrected at the source (in the zoom feature ?).

roulinthomas avatar Oct 12 '21 13:10 roulinthomas

Hello, thank you for reporting this! I will investigate it and take action from there.

zHaytam avatar Oct 12 '21 19:10 zHaytam