Results 1 to 4 of 4

Thread: Random Circles in Picture Box

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2017
    Posts
    2
    Rep Power
    0

    Question Random Circles in Picture Box

    Hello! I would love to know how can i draw circles in random positions but without them getting in the way of each other. I want them to be seperated at least 50 pixels from each other but without them leaving the boundaries of the Picture Box. Please help me out with this i would be so greatful!!
    This is my code and its close but it doesnt do it!

    Code:
    private void btnCrear_Click(object sender, EventArgs e)        {
                objDraw.Clear(Color.AliceBlue);
                Amount = Convert.ToInt16(txtNodos.Text);
                SolidBrush SBColors = new SolidBrush(Color.Black);
                for (int i = 0; i < Amount; i++)
                {
                    PosX = objNum.Next(100, 700);
                    PosY = objNum.Next(100, 300);
    
    
                    if (i > 0)
                    {
                        if (PosXB + 50 < 700 && PosYB + 50 < 300)
                        {
                            if (PosX > PosXB + 50 && PosY > PosYB + 50)
                            {
                                objDraw.FillEllipse(SBColors, PosX, PosY, 50, 50);
                                PosXB = PosX;
                                PosYB = PosY;
                                MessageBox.Show("1");
                            }
                            else
                            {
                                objDraw.FillEllipse(SBColors, PosX+50, PosY+50, 50, 50);
                                PosXB = PosX+50;
                                PosYB = PosY+50;
                                MessageBox.Show("2");
                            }
                        }
                        else if (PosXB - 50 > 100 && PosYB - 50 > 100)
                        {
                            if (PosX < PosXB - 50 && PosY < PosYB - 50)
                            {
                                objDraw.FillEllipse(SBColors, PosX, PosY, 50, 50);
                                PosXB = PosX;
                                PosYB = PosY;
                                MessageBox.Show("3");
                            }
                            else
                            {
                                objDraw.FillEllipse(SBColors, PosX-50, PosY, 50, 50);
                                PosXB = PosX-50;
                                PosYB = PosY-50;
                                MessageBox.Show("4");
                            }
                        }
                    }
                    else
                    {
                        objDraw.FillEllipse(SBColors, PosX, PosY, 50, 50);
                        PosXB = PosX;
                        PosYB = PosY;
                    }
    
    
                }

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,882
    Rep Power
    146
    You should start by generating the appropriate number of Point values to represent the centres of the circles. That means using a Random object to generate an X and a Y value. Use appropriate min and max values when generating the numbers so that each circle will be wholly within the bounds of the PictureBox. After generating a valid Point, add it to a List. When you generate a Point, calculate the distance between it and each Point already in the List to make sure that they are at least one diameter plus the extra clearance apart. If they are not then discard that Point, otherwise add it to the List. Keep going until you have all Points.

    Once you've got all the Points, you can Translate then all to the top-left corner of the bounding box for the circle. That is the data you will use to draw. You can then call Invalidate to force a Paint event.

    In the Paint event handler, loop through the List and call DrawEllipse for each Point in it.

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2017
    Posts
    2
    Rep Power
    0
    Thank you for your help! I'll give it a try and see if this works.

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,882
    Rep Power
    146
    Actually, now that I think about it, there's no point generating the centres and then translating because the distances between the final points and the initial points are all going to be the same anyway. You may as well just generate the top-left corners to begin with. In that case, the min would be 0 and the max would be one diameter less than the container dimension in that direction.

Similar Threads

  1. Question Circles intersection & repositioning
    By sam in forum C# General Discussion
    Replies: 3
    Last Post: 09-08-2016, 7:25 PM
  2. Question Random file access
    By pumper in forum C# General Discussion
    Replies: 17
    Last Post: 05-16-2016, 10:23 AM
  3. Save a picture from pictureBox to my PC
    By fsoender in forum C# General Discussion
    Replies: 12
    Last Post: 07-09-2015, 3:21 PM
  4. Question How "opened dialog" box makes an automatic picture selection?
    By halit1982 in forum C# General Discussion
    Replies: 0
    Last Post: 06-27-2012, 10:34 AM
  5. Question Draw 200 lines with random width
    By msasan1367 in forum C# General Discussion
    Replies: 1
    Last Post: 05-27-2011, 6:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •