public static void MsaglMoveToMedian()

in GraphLayout/MSAGL/GraphmapsWithMesh/LocalModifications.cs [91:207]


        public static void MsaglMoveToMedian(Tiling g, Dictionary<int, Node> idToNodes, LgLayoutSettings _lgLayoutSettings)
        {

            //foreach point first produce the crossing candidates.
            g.buildCrossingCandidates();


            //now proceess the movement
#if SHARPKIT //https://code.google.com/p/sharpkit/issues/detail?id=340
            int[,] listNeighbors = null;
            throw new InvalidOperationException();
#else
            int[,] listNeighbors = new int[20, 3];
#endif
            double[] d = new double[10];
            int a = 0, b = 0;
            Core.Geometry.Point[] p = new Core.Geometry.Point[10];
            bool localRefinementsFound = true;
            int iteration = 10;
            //int offset = iteration * 2;
            int unit = (int)_lgLayoutSettings.NodeSeparation / 2;

            if (_lgLayoutSettings.hugeGraph)
            {
                iteration = 3;
                unit = (int)_lgLayoutSettings.NodeSeparation;
            }

            while (localRefinementsFound && iteration > 0)
            {
                iteration--;
                localRefinementsFound = false;


                for (int index = g.N; index < g.NumOfnodesBeforeDetour; index++)
                {
                    Vertex w = g.VList[index];

                    int numNeighbors = 0;

                    for (int k = 0; k < g.DegList[w.Id]; k++)
                    {
                        numNeighbors++;
                        listNeighbors[numNeighbors, 1] = g.EList[w.Id, k].NodeId;
                        listNeighbors[numNeighbors, 2] = k;
                    }

                    if (numNeighbors <= 1) continue;

                    for (int counter = 1; counter <= 9; counter++)
                    {
                        d[counter] = 0;

                        if (counter == 1) { a = unit; b = unit; }
                        if (counter == 2) { a = 0; b = unit; }
                        if (counter == 3) { a = -unit; b = unit; }
                        if (counter == 4) { a = -unit; b = 0; }
                        if (counter == 5) { a = -unit; b = -unit; }
                        if (counter == 6) { a = 0; b = -unit; }
                        if (counter == 7) { a = unit; b = -unit; }
                        if (counter == 8) { a = unit; b = 0; }
                        if (counter == 9) { a = 0; b = 0; }


                        for (int k = 1; k <= numNeighbors; k++)
                        {
                            double length = Math.Sqrt((w.XLoc + a - g.VList[listNeighbors[k, 1]].XLoc) *
                                          (w.XLoc + a - g.VList[listNeighbors[k, 1]].XLoc)
                                          +
                                          (w.YLoc + b - g.VList[listNeighbors[k, 1]].YLoc) *
                                          (w.YLoc + b - g.VList[listNeighbors[k, 1]].YLoc)
                                    );
                            if (length < 1)
                            {
                                length = 1000;
                            }


                            d[counter] += length;

                        }


                        p[counter] = new Core.Geometry.Point(a, b);


                    }
                    Array.Sort(d, p);

                    for (int counter = 1; counter <= 9; counter++)
                    {
                        var mincostA = (int)p[counter].X;
                        var mincostB = (int)p[counter].Y;

                        if (!(mincostA == 0 && mincostB == 0))
                        {
                            w.XLoc += mincostA;
                            w.YLoc += mincostB;
                            if (g.GetNodeExceptTheGivenNode(w, w.XLoc, w.YLoc, 5) >= 0 ||
                                g.MsaglGoodResolution(w, listNeighbors, numNeighbors, 5) == false
                                || g.noCrossingsHeuristics(w, index) == false
                                )
                            {
                                w.XLoc -= mincostA;
                                w.YLoc -= mincostB;
                            }
                            else
                            {
                                localRefinementsFound = true;
                                break;
                            }
                        }
                    }

                }
            }
        }