Beispiel #1
0
        private void drawMap(HnS hideAndSeek)
        {
            Graph h          = hideAndSeek.getGraphHnS();
            int   houseCount = h.getHouseCount();

            //Making a List of house per Weight
            int maxWeight = h.getMaxWeight();

            List <int>[] listPerWeight = new List <int> [maxWeight + 1];
            for (int i = 0; i <= maxWeight; i++)
            {
                listPerWeight[i] = new List <int>();
            }
            for (int house = 1; house <= houseCount; house++)
            {
                int idx = h.getWeight(house);
                listPerWeight[idx].Add(house);
            }

            //2D Array of point
            //use   : storing left and right point of ellipse(s) horizontal radius
            int[,] matrixOfPoints = new int[houseCount + 1, 5];

            //colors
            Brush ellipseCol = (Brush)(new BrushConverter().ConvertFrom("#C70039"));
            Brush lineCol    = (Brush)(new BrushConverter().ConvertFrom("#787878"));

            //Relative position of grid (circle and label)
            int x   = 50;
            int y   = 50;
            int rad = 50;

            //drawing houses
            for (int weight = 0; weight <= maxWeight; weight++)
            {
                y = 50;
                foreach (int house in listPerWeight[weight])
                {
                    //x1,y1,x2,y2
                    matrixOfPoints[house, 1] = x;
                    matrixOfPoints[house, 2] = y + (rad / 2);
                    matrixOfPoints[house, 3] = x + rad;
                    matrixOfPoints[house, 4] = y + (rad / 2);

                    string label       = house.ToString();
                    Grid   ellipseGrid = new Grid();

                    //Create ellipse and textboxt as house representation
                    ellipseGrid.Children.Add(new Ellipse()
                    {
                        Name   = "ellipse" + house,
                        Width  = rad,
                        Height = rad,
                        Fill   = ellipseCol
                    });

                    ellipseGrid.Children.Add(new TextBlock()
                    {
                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment   = VerticalAlignment.Center,
                        Text       = label,
                        Foreground = Brushes.White
                    });


                    mainCanvas.Children.Add(ellipseGrid);
                    //Grid Location
                    Canvas.SetTop(ellipseGrid, y);
                    Canvas.SetLeft(ellipseGrid, x);
                    Canvas.SetZIndex(ellipseGrid, 2);

                    y += 100;
                }
                x += 100;
            }

            //drawing lines
            for (int house = 1; house <= houseCount; house++)
            {
                //get adjacency list
                List <int> listHouse = h.getListHouse(house);
                int        xgap      = 20;

                Grid lineGrid = new Grid();

                foreach (int neighbor in listHouse)
                {
                    if (house < neighbor)
                    {
                        lineGrid.Children.Add(new Line()
                        {
                            //create line with starting point in house and end point in neighbor
                            StrokeThickness = 4,
                            Stroke          = lineCol,
                            X1 = matrixOfPoints[house, 3] - xgap,
                            Y1 = matrixOfPoints[house, 4],
                            X2 = matrixOfPoints[neighbor, 1] + xgap,
                            Y2 = matrixOfPoints[neighbor, 2]
                        });
                    }
                }

                //Bring to back
                Canvas.SetZIndex(lineGrid, 1);
                mainCanvas.Children.Add(lineGrid);
            }
        }