所以我在Windows窗体应用程序中有一个圆圈,并且必须在这个圆圈中放置20个随机点。 我的想法是将圆圈分成4个部分,使其更加平衡。 我的问题是,所有的点都产生在中间,我不知道如何解决这个问题...
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; } } } }示例: http : //puu.sh/gB6Dg/e81f8c3486.png http://puu.sh/gB6Ec/306f61424c.png
感谢帮助!
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!
最满意答案
问题是在C#中, ^是逻辑异或运算符。 您需要改为使用Math.Pow 。 所以...
if (Math.Sqrt(Math.Pow(250 - KegelX[i], 2) + Math.Pow(KegelY[i] - 250, 2)) < 200)等等。
The problem is that in C#, ^ is the logical xor operator. You need to use Math.Pow instead. So...
if (Math.Sqrt(Math.Pow(250 - KegelX[i], 2) + Math.Pow(KegelY[i] - 250, 2)) < 200)and so on.
更多推荐
发布评论