当前位置:网站首页>[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);
}
}
}边栏推荐
- Monologue of a software Investor: why don't I pursue fast-growing companies
- BUUCTF-bbbbbbrsa
- Explain the idempotence of distributed system in detail
- BUUCTF-Baby RSA
- 基于mediapipe的姿态识别和简单行为识别
- [flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)
- BUUCTF-RSA4
- [NPUCTF2020]EzRSA
- 虚拟存储器与Cache的比较
- 2022/7/26
猜你喜欢

29. Learn the stacked column chart of highcharts using percentage

TCP的粘包拆包问题+解决方案

Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further

Lua basic grammar learning

Shell编程规范与变量

BUUCTF-RSA4

Monologue of a software Investor: why don't I pursue fast-growing companies

Latex常用总结(2):输入矩阵(输入矩阵、对角阵、方程组等)

BUUCTF-RSA4

Master data management theory and Practice
随机推荐
重新定义分析 - EventBridge 实时事件分析平台发布
What are the methods of process synchronization?
【开发教程11】疯壳·ARM功能手机-定时器实验教程
Monologue of a software Investor: why don't I pursue fast-growing companies
BUUCTF-[BJDCTF2020]RSA1
2022 International Conference on civil, building and Environmental Engineering (iccaee 2022)
This is the most concise guide to tcpdump in history. It's enough to read this one
15million per day! BYD masks won a US $1billion order in California
How Flink uses savepoint
Redis hash underlying data structure
Latex common summary (2): input matrix (input matrix, diagonal matrix, equations, etc.)
[flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)
为什么 Redis 集群要使用反向代理? 看这篇就明白了
NDK series (6): let's talk about the way and time to register JNI functions
【开发教程9】疯壳·开源蓝牙心率防水运动手环-心率监测
MySQL data query (where)
QT with OpenGL (shadow mapping)
Why does redis cluster use reverse proxy? Just read this one
TCP的粘包拆包问题+解决方案
Sort sort