当前位置:网站首页>[unity] mapping 2D coordinates to ID
[unity] mapping 2D coordinates to ID
2022-07-27 23:59:00 【Magician Dix】
On the plane , Lattice coordinates are usually two-dimensional coordinates (X,Y), But two-dimensional coordinates have many inconveniences , And if as a key value , Its value taking efficiency is not high . So most of the time , Two dimensional coordinates need to be converted into ID To use ( And can from ID Convert back to coordinates ).
The common way is X*N+Y,N For a large enough number . But this way has problems , It can only support all positive numbers or all negative numbers , Once there are positive and negative , Coordinate conversion will fail . Therefore, we need a more convenient algorithm , Can map two-dimensional coordinates into ID, And can transform each other .
1、 sketch
The basic principle is shown in the figure below :

This is it. ID Generation order of , Generate in a spiral shape .
2、 Code
There is nothing to say about the principle , Pure computation . Go straight to the code :
2.1 : Coordinate conversion ID
public int ToInt(Vector2Int grid)
{
if (grid.x == 0 && grid.y == 0) return 0;
int k = math.max(math.abs(grid.x), math.abs(grid.y));
if (grid.x > grid.y)
return 4 * k * k + 1 + 2 * k + grid.x + grid.y;
else
return 4 * k * k + 1 - 2 * k - grid.x - grid.y;
}2.2: ID Convert back to coordinates
ID Converting back to coordinates is slightly more complicated , The code is as follows :
public Vector2Int GetMapGrid(int index)
{
if (index == 0) return Vector2Int.zero;
float sqrtX = math.sqrt(index);
int k = (int)((sqrtX + 1) / 2);
if (sqrtX > 2 * k)
{
int xAy = index - (4 * k * k + 1) - 2 * k;
if (xAy < 0)
{
int y = -1 * k;
int x = xAy - y;
return new Vector2Int(x, y);
}
else
{
int x = k;
int y = xAy - x;
return new Vector2Int(x, y);
}
}
else
{
int xAy = (4 * k * k + 1) - index - 2 * k;
if (xAy < 0)
{
int x = -k;
int y = xAy - x;
return new Vector2Int(x, y);
}
else
{
int y = k;
int x = xAy - y;
return new Vector2Int(x, y);
}
}
}边栏推荐
- Redis hash underlying data structure
- Unity implements simple Sketchpad drawing function (notes)
- File & recursion 14.1
- File&递归14.1
- 面试官问线程安全的List,看完再也不怕了!
- [RoarCTF2019]RSA
- The txt file named according to the sequence number is renamed from the back to the front
- Shell编程规范与变量
- [NCTF2019]babyRSA1
- Redis hash underlying data structure
猜你喜欢
随机推荐
[actf freshmen 2020] crypto AES
Why do I need to wait for 2msl?
J9数字科普:Sui网络的双共识是如何工作的?
Comparison between virtual memory and cache
(十二)51单片机----用DS18B20浅测一下工(江)西的室外温度
Redefine analysis - release of eventbridge real-time event analysis platform
2022 International Conference on civil, building and Environmental Engineering (iccaee 2022)
Technical certification | Tupo software and Huawei cloud create a new situation of win-win cooperation
[flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)
传奇服务端:GOM GeeM2引擎更新时必须要修改哪些地方?
[GWCTF 2019]BabyRSA1
Redis 哈希Hash底层数据结构
Zcmu--1720: death is like the wind, I want to pretend to force
[MRCTF2020]babyRSA
Character stream learning 14.3
How to use C WinForm to copy files and display progress
What is the prospect of low code development? Are you really optimistic about low code development?
What are the software operation and maintenance monitoring?
Introduction to several common usage scenarios of message queue
【飞控开发基础教程6】疯壳·开源编队无人机-SPI(六轴传感器数据获取)


![[NCTF2019]babyRSA1](/img/c1/52e79b6e40390374d48783725311ba.gif)






