So I got a circle in a Windows Forms Application and have to place 20 random points in this circle. My idea was to split the circle into 4 parts to make it more balanced. My problem is that the points are all generated around the middle and I have no idea how to fix this...
Graphics g;
Pen p;
Random r = new Random();
int[] KegelX = new int[20];
int[] KegelY = new int[20];
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Kegelplatzierung();
p = new Pen(Color.Black);
g = this.CreateGraphics();
g.DrawEllipse(p, new Rectangle(50, 50, 400, 400));
for (int i = 0; i < 20; i++)
{
g.DrawEllipse(p, new Rectangle(KegelX[i], KegelY[i], 1, 1));
}
p.Dispose();
g.Dispose();
}
private void Kegelplatzierung() {
for (int i = 0; i < 5; i++)
{
bool Kriterium = false;
while (!Kriterium)
{
KegelX[i] = r.Next(50, 250);
KegelY[i] = r.Next(50, 250);
if (Math.Sqrt((250 - KegelX[i]) ^ 2 + (KegelY[i] - 250) ^ 2) < 200)
{
Kriterium = true;
}
}
}
for (int i = 5; i < 10; i++)
{
bool Kriterium = false;
while (!Kriterium)
{
KegelX[i] = r.Next(250, 450);
KegelY[i] = r.Next(50, 250);
if (Math.Sqrt((KegelX[i] - 250) ^ 2 + (KegelY[i] - 250) ^ 2) < 200)
{
Kriterium = true;
}
}
}
for (int i = 10; i < 15; i++)
{
bool Kriterium = false;
while (!Kriterium)
{
KegelX[i] = r.Next(50, 250);
KegelY[i] = r.Next(250, 450);
if (Math.Sqrt((250 - KegelX[i]) ^ 2 + (250 - KegelY[i]) ^ 2) < 200)
{
Kriterium = true;
}
}
}
for (int i = 15; i < 20; i++)
{
bool Kriterium = false;
while (!Kriterium)
{
KegelX[i] = r.Next(250, 450);
KegelY[i] = r.Next(250, 450);
if (Math.Sqrt((KegelX[i] - 250) ^ 2 + (250 - KegelY[i]) ^ 2) < 200)
{
Kriterium = true;
}
}
}
}
Examples: http://puu.sh/gB6Dg/e81f8c3486.png http://puu.sh/gB6Ec/306f61424c.png
Thanks for help!
The problem is that in C#,
^
is the logical xor operator. You need to use Math.Pow instead. So...and so on.
This will do it as you planned it. Note however, that the symmetry between the four quadrants is only necessary if you really need it to balance very few points. For greater numbers it is not necessary and you could reduce the code to around half of its lines..!
Here is the result. The dots spread over the whole circle uniformly, not clustering in the middle:
The direct code, sans quadrants;
You can do this by choosing a random radius and angle for each point.
To avoid noticeable quantisation of the angle component into radial spokes, I use
(double)rand.Next() / int.MaxValue
to get a random number between 0 and 1 and multiply that by 2π.To avoid the points being bunched up near the centre of the circle, I used Chris A.'s formula (at Generate a random point within a circle (uniformly)) to generate the radius:
To make sure objects are disposed of cleanly, you can use the
using
construct - it makes sure you don't accidentally forget.It's usually better to assign things like the radius of the circle to a variable as then you can easily change it in one place, and it makes the code easier to read if you use meaningful variable names.
Here's an example of the output, but with the radius set to 100 and generating 200 points:
Generates a random set of points inside a circle of a given radius...