首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >锦标赛括号算法

锦标赛括号算法
EN

Stack Overflow用户
提问于 2009-08-18 14:54:18
回答 1查看 5.8K关注 0票数 3

我需要创建一个asp.net页面,自动生成一个支架锦标赛网球风格。关于数据库中比赛的管理,这不是问题。

问题是括号的动态图形创建。用户将能够创建锦标赛由2-4…32名球员。我不知道如何在html或gdi中创建图形括号...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-08-18 19:28:53

使用Silverlight和Grid,您可以生成如下所示的内容:

为此,请定义一个包含网格的常规UserControl。(当您使用Silverlight3.0SDK在VS2008中构建silverlight应用程序时,这是默认设置)。

然后,在用户控件的构造函数中添加对以下内容的调用:

代码语言:javascript
复制
private void SetupBracket(int n)
{
    var black = new SolidColorBrush(Colors.Gray);
    // number of levels, or rounds, in the single-elim tourney
    int levels = (int)Math.Log(n, 2) + 1;
    // number of columns in the Grid.  There's a "connector"
    // column between round n and round n+1.
    int nColumns = levels * 2 - 1;

    // add the necessary columns to the grid
    var cdc = LayoutRoot.ColumnDefinitions;
    for (int i = 0; i < nColumns; i++)
    {
        var cd = new ColumnDefinition();
        // the width of the connector is half that of the regular columns
        int width = ((i % 2) == 1) ? 1 : 2;
        cd.Width = new GridLength(width, GridUnitType.Star);
        cdc.Add(cd);
    }
    var rdc = LayoutRoot.RowDefinitions;

    // in the grid, there is one row for each player, and 
    // an interleaving row between each pair of players. 
    int totalSlots = 2 * n - 1;
    for (int i = 0; i < totalSlots; i++)
    {
        rdc.Add(new RowDefinition());
    }

    // Now we have a grid of the proper geometry.  
    // Next: fill it. 

    List<int> slots = new List<int>();
    ImageBrush brush = new ImageBrush();
    brush.ImageSource = new BitmapImage(new Uri("Bridge.png", UriKind.Relative));

    // one loop for each level, or "round" in the tourney.  
    for (int j = 0; j < levels; j++)
    {
        // Figure the number of players in the current round.
        // Since we insert the rounds in the reverse order, 
        // think of j as the "number of rounds remaining."
        // Therefore, when j==0, playersThisRound=1.  
        // When j == 1, playersThisRound = 2.  etc.
        int playersThisRound = (int)Math.Pow(2, j);

        int x = levels - j;
        int f = (int)Math.Pow(2, x - 1);

        for (int i = 0; i < playersThisRound; i++)
        {
            // do this in reverse order.  The innermost round is 
            // inserted first.
            var r = new TextBox();
            r.Background = black;
            if (j == levels - 1)
                r.Text = "player " + (i + 1).ToString();
            else
                r.Text = "player ??";

            // for j == 0, this is the last column in the grid.
            // for j == levels-1, this is the first column.
            // The grid column is not the same as the current
            // round, because of the columns used for the 
            // interleaved connectors.
            int k = 2 * (x - 1); 
            r.SetValue(Grid.ColumnProperty, k);

            int m = (i * 2 + 1) * f - 1;
            r.SetValue(Grid.RowProperty, m);
            LayoutRoot.Children.Add(r);

            // are we not on the last round? 
            if (j > 0)
            {
                slots.Add(m);
                // Have we just inserted two rows?  Then we need
                // a connector between these two and the next 
                // round (the round previously added).
                if (slots.Count == 2)
                {
                    string xamlTriangle = "<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' "+
                                          "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " +
                                          "Data='M0,0 L 100 50 0 100 Z' Fill='LightBlue' Stretch='Fill'/>";

                    Path path = (Path)System.Windows.Markup.XamlReader.Load(xamlTriangle);

                    path.SetValue(Grid.ColumnProperty, 2 * (x - 1) + 1);
                    path.SetValue(Grid.RowProperty, slots[0]);
                    path.SetValue(Grid.RowSpanProperty, slots[1] - slots[0] + 1);
                    this.LayoutRoot.Children.Add(path);
                    slots.Clear();
                }
            }

        }
    }
}

在上图中,连接器只是一个等腰三角形,顶点指向右侧。它由字符串上的XamlReader.Load()生成。

我想你也会想要美化它,用不同的颜色和字体来设计它。

您可以将这个silverlight“用户控件”插入到任何HTML网页中,就像在页面中嵌入flash应用程序一样。有针对IE、Firefox、Opera、Safari和Chrome的silverlight插件。

如果您不想使用Silverlight,您可以使用类似的方法来构造HTML表。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1294412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档