使用哈希函数赋予对象令人难忘的个性
(注意:该项目是用 Python 编写的。)
我正在运行一个模拟,其中有许多我想在屏幕上显示并进行操作的对象。需要有一种方法来识别每个对象,因为它们会突然从一个地方移动到另一个地方,我希望能够跟踪哪个对象移动到了哪里。
我一直在想的是,我会给每一个物体赋予一个“个性”。几种颜色和一个英文名称,我将把它作为 GUI 上的对象表示。我认为哈希函数将用于生成这些颜色和名称,但我从未使用过哈希函数。
我怎样才能做我想做的事?
(Note: The project is in Python.)
I'm running a simulation in which I have many objects that I want to show on the screen and manipulate with. There needs to be a way to identify each object, because they'll be moving from place to place abruptly and I want to be able to track which object moved where.
What I've been thinking is, to every object I'll generate a "personality". A couple of colors, and an english name, and I'll put that as the object representation on the GUI. I figured that a hash function will be used to make these colors and names, but I've never worked with hash functions.
How can I do what I want to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 uuid(python 中的模块 uuid >= 2.5)。
在版本 4 中,根据定义,此 uuid 在所有字段(一个字段除外)上都是随机的。
您可以正确分解字段以创建颜色或名称(通过将名称存储桶映射到特定字段)。当然,您限制了哈希值(真实身份始终是 uuid),但出于视觉目的,它已经足够了。
例如,您可以使用前三个八位字节来生成颜色 #9d477d,并使用剩余的八位字节
c7
从一组 256 个名称中选择一个名称。如果您最终得到的颜色太难看,则相反,可以在 HSV 中工作,并将饱和度和值限制到给定水平。同样,这会进一步限制您的哈希(但毕竟颜色空间已经相当有限)。
use a uuid (module uuid in python >= 2.5).
This uuid, in version 4, is by definition random on all fields (except one)
You can decompose the fields properly to create a color or a name (by mapping a bucket of names to a specific field). Of course you are limiting your hash (the real identity is always the uuid) but for visual purposes it's greatly sufficient.
For example, you could use the first three octets to generate the color #9d477d, and the remaining octet
c7
to pick one name out of a set of 256.If you end up with too ugly colors, you can work in HSV instead, and clamp saturation and value to given levels. again, this restricts your hash even more (but the color space is already pretty limited after all).