圆圈内的随机点(Random points inside a circle)

编程入门 行业动态 更新时间:2024-10-26 06:27:16
圆圈内的随机点(Random points inside a circle)

所以我在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.

更多推荐

本文发布于:2023-08-05 23:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆圈   Random   circle   points

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!