开发者

Using a hash function to give a memorable personality to objects

开发者 https://www.devze.com 2022-12-13 01:37 出处:网络
(Note: The project is in Python.) I\'m running a simulation in which I have many objects that I want to show开发者_C百科 on the screen and manipulate with. There needs to be a way to identify each ob

(Note: The project is in Python.)

I'm running a simulation in which I have many objects that I want to show开发者_C百科 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?


use a uuid (module uuid in python >= 2.5).

This uuid, in version 4, is by definition random on all fields (except one)

>>> uuid.uuid4()
UUID('9d477dc7-a986-4e3d-aa4f-6e57f690be78')

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消