duanhuan5409 2015-06-23 19:08
浏览 48

将X,Y对转换为唯一可反转的数字

Assume you have coordinate pairs Xn,Yn how would you convert the pair into a unique reverse-able number such that for any such series Yn * Xn != Xn * Yn?

Right now I use the multiplication method to get one number; however, this causes problems because for any series like 1600,2000 2000,1600 results in the same non unique number.

Thanks.

  • 写回答

1条回答 默认 最新

  • douji6735 2015-06-24 06:40
    关注

    If you want to store the address of a pixel on a computer screen in a single number, the most efficient way would be to multiply Y by the number of horizontal pixels. With a horizontal resolution of 1280, the top row will have numbers 0 through 1279, the second row 1280 through 2559, and so on. In fact, this is how the computers themselves do it. (*)

    But if you want to use the same formula for different computer monitors, it's best to not rely on the horizontal resolution, but to use a large constant value for the multiplication factor, so that the calculations become more efficient.
    For 32 bits integers, 216 is a nice number; this allows for resolutions of up to 65536 × 65536. (Of course if you're trying to print posters, you may have to grow to a bigger scale.)

     number = (Y<<16)+X;
    

    and to reverse

     X = number&65535; Y = number>>16;
    

    (*) That's an oversimplification, but you get the idea.

    评论

报告相同问题?