Windows Phone绘图省略号

时间:2012-04-27 09:59:45

标签: c# .net windows-phone-7 drawing ellipse

您好

我想在一个大椭圆上绘制40个椭圆。 ITS STRANGE。

for (int i = 0; i < 40; i++)
        {
            {
                {
                    Random r = new Random();
                    int distance = r.Next(0, 10000);
                    var rv = r.Next(0, 359);
                    var x = Math.Sin(rv * Math.PI / 180) * 225;
                    rv = r.Next(0, 359);
                    var y = Math.Cos(rv * Math.PI / 180) * 225;
                    Ellipse e = new Ellipse();
                    e.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(counter * 5), (byte)(counter * 3), (byte)(counter * 1)));
                    e.Margin = new Thickness(y, -150 + x, 0, 0);
                    e.Width = 25;
                    e.Height = 25;
                    counter++;
                    PointsGrid.Children.Add(e);
                }
            }
        }

此代码仅绘制7,8或有时9个省略号。但是,如果我在代码中添加额外的行:

for (int i = 0; i < 40; i++)
        {
            {
                {
                    Random r = new Random();
                    int distance = r.Next(0, 10000);
                    var rv = r.Next(0, 359);
                    var x = Math.Sin(rv * Math.PI / 180) * 225;
                    rv = r.Next(0, 359);
                    var y = Math.Cos(rv * Math.PI / 180) * 225;
                    Ellipse e = new Ellipse();
                    e.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(counter * 5), (byte)(counter * 3), (byte)(counter * 1)));
                    e.Margin = new Thickness(y, -150 + x, 0, 0);
                    e.Width = 25;
                    e.Height = 25;
                    counter++;
                    PointsGrid.Children.Add(e);
                    MessageBox.Show(""); // Additional line
                }
            }
        }

如果我添加显示消息框,请在所有这些按下确定,我将看到所有40个省略号...

问题是它怎么可能,我该如何解决它?

2 个答案:

答案 0 :(得分:4)

问题是你使用Random。如果每次都创建一个新的Random(),它将使用当前时间作为种子。由于当前时间是相同的(因为代码执行得如此之快,因此伪随机数总是相同的。)

Random r = new Random();

for (int i = 0; i < 40; i++)
        {
            {
                {
                    int distance = r.Next(0, 10000);
                    var rv = r.Next(0, 359);
                    var x = Math.Sin(rv * Math.PI / 180) * 225;
                    rv = r.Next(0, 359);
                    var y = Math.Cos(rv * Math.PI / 180) * 225;
                    Ellipse e = new Ellipse();
                    e.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(counter * 5), (byte)(counter * 3), (byte)(counter * 1)));
                    e.Margin = new Thickness(y, -150 + x, 0, 0);
                    e.Width = 25;
                    e.Height = 25;
                    counter++;
                    PointsGrid.Children.Add(e);
                    //MessageBox.Show(""); // Additional line
                }
            }
        }

消息框只会“减慢”执行速度 - 这意味着您的所有圈子都不会相互叠加。

答案 1 :(得分:3)

这可能是一个时间问题。你应该在循环之外声明Random,因为种子是基于当前时间的。

    Random r = new Random();

    for (int i = 0; i < 40; i++)
    {
        int distance = r.Next(0, 10000);
        var rv = r.Next(0, 359);
        var x = Math.Sin(rv * Math.PI / 180) * 225;
        rv = r.Next(0, 359);
        var y = Math.Cos(rv * Math.PI / 180) * 225;
        Ellipse e = new Ellipse();
        e.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(counter * 5), (byte)(counter * 3), (byte)(counter * 1)));
        e.Margin = new Thickness(y, -150 + x, 0, 0);
        e.Width = 25;
        e.Height = 25;
        counter++;
        PointsGrid.Children.Add(e);
    }

如果这确实是你的问题,那就意味着你真的画了40个椭圆,但大多数都是重叠的。