Brzooz
Brzooz

Reputation: 55

Windows Phone drawing ellipses

Hello

I want to draw 40 ellipses on one big ellipse. 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);
                }
            }
        }

This code draws only 7,8 or sometimes 9 ellipses. But if I add additional line to code:

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
                }
            }
        }

If i add showing messageboxes, press ok on all of them, I will see all 40 ellipses...

The question is how could it be and how can i fix it ?

Upvotes: 0

Views: 2644

Answers (2)

Dave Bish
Dave Bish

Reputation: 19646

The issue is your use of Random. If you make a new Random() every time - it will use the current time as a seed. Since the current time is the same (since the code is executed so quickly the pesudo-random number is always the same.)

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
                }
            }
        }

The message-box just "slows" the execution - meaning all your circles aren't drawn on-top of each other.

Upvotes: 4

Kevin Gosse
Kevin Gosse

Reputation: 39007

That may be a timing issue. You should declare Random outside of the loop, since the seed is based on the current time.

    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);
    }

If that's really your issue, it means that your really drawing 40 ellipses, but most of them are overlapping.

Upvotes: 3

Related Questions