Question Random Circles in Picture Box

Humberto

New member
Joined
Dec 2, 2017
Messages
2
Programming Experience
1-3
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!! :D
This is my code and its close but it doesnt do it!

C#:
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;
                }


            }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,052
Location
Sydney, Australia
Programming Experience
10+
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.
 

Humberto

New member
Joined
Dec 2, 2017
Messages
2
Programming Experience
1-3
Thank you for your help! I'll give it a try and see if this works. :D
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,052
Location
Sydney, Australia
Programming Experience
10+
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.
 
Top Bottom