WPF:在随机点渲染画布 [英] WPF: Rendering a canvas at Random Points

查看:94
本文介绍了WPF:在随机点渲染画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考此我正在开发的编程游戏.

游戏开始时,我在画布上的随机点上生成了这些机器人,乍一看(同时添加一个或两个机器人),这似乎可以正常工作.

When the game is started, I am generating these robots at supposingly random points on a Canvas, and at first look (adding one or two bots at the same time), this seemed to be working as it should.

...但是,当我同时添加大量机器人时,这就是它们在画布上随机"出现的方式:

...but, when I added a ton of bots at the same time, this is how they where "randomly" appeared on the canvas:

替代文字http://img22.imageshack.us/img22/6895/randombotpositionlf7 .jpg

所谓的随机点毕竟似乎并不是那么随机...它们只是直线上的点

The supposedly random points don't seem that random after all...they're just points of a straight line!

这是我计算积分的方式:

Here is how I am calculating the Points:

SetStartingPoint(GetRandomPoint(ArenaWidth, ArenaHeight)); //the width and height are 550, 480 at the moment 

//which calls:

private Point GetRandomPoint(double maxWidth, double maxHeight)
{
        return new Point(new Random().Next(0, (int)(maxWidth-80)), new Random().Next(0, (int)maxHeight));
}

//and ultimately:

private void SetStartingPoint(Point p)
{
        Translate_Body.X = (double)p.X;
        Translate_Body.Y = (double)p.Y;
}

关于上面的代码,Translate_Body是机械手(canvas)的TranslateTransform类型,因此通过分配其XY属性,它将其位置更改为新值

As regards the above code, Translate_Body is of type TranslateTransform of the robot (canvas), so by assigning its X and Y properties, it will change its position to the new values

我在这里想念什么?

[UPDATE]解决方案:

问题就像大家都建议的那样,由于使用了新的实例化,因此无法正确植入数字.

The problem was like you all suggested, the numbers weren't being seeded properly because of the new instantiations.

我现在将代码更改为使用单个Random变量,并从中植入所有点.

I now changed the code to use a single Random variable and to seed all the points from it.

但是我仍然不明白为什么这些点是在看似直线的坐标线上生成的. 有人可以解释吗?

But I still can't understand why the points where being generated at a seemingly straight line of coordinates. Is anyone able to explain this ?

推荐答案

我相信问题在于您的随机数没有正确植入种子.使用随机的单个实例,而不是多个实例,您应该会看到很多改进.

I believe the problem is that your random numbers aren't being seeded properly. Use a single instance of Random, rather than multiple ones, and you should see much improvement.

用户添加了为什么它们在一条直线上"-可能是由于基于种子生成的随机数的默认级数. 维基百科是一个不错的起点-我不是数学专家,所以不会尝试解释随机数世界的核心. :)

User added "why they were in a straight line" -- probably because of the default progression of the generated random numbers based on the seed. wikipedia would be a good place to start -- I'm no math expert, so won't try to explain the core of the world of random numbers. :)

这篇关于WPF:在随机点渲染画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆