#### Wrda

**Spell Reviewer**

- Joined
- Nov 18, 2012

- Messages
- 1,906

**Why is this relevant?**

One day I figured I needed a way to know the center of a tile, given an axis coordinate. Such thing isn't available to us directly so I did it myself. Also this is important for our common knowledge that sometimes we take for granted.

**Assumptions**

Our intuition would say that the length of a tile is exactly 128.00, and that given the tile of the center of the map, which is at 0, 0, would have the edges as 64.00, 64.00, -64.00, -64.00 (up, right, down, left edges respectively).

Upon having a loop to check each edge, this assumption was just wrong. Edges are 63.499, 63.499, -64.500, -64.500 (same order as above), length is 127.999 instead.

**Researched method**

I delved into a basic method to find out the edges of a tile by alternating between 2 different tile types, and with a loop, which increases an axis point (variable r in test map).

**Figuring out the logic**

My first attempt was pretty wack, quite inacurate, still useful in certain situations. The second is fully accurate, although it appears to be somewhat slower? Not very significative I hope.

JASS:

```
function GetTileCenter takes real x returns real
local real abs = RAbsBJ(x)
local real mod = ModuloReal(abs, 128.00)
local real multiples = R2I(abs/128)
if mod >= 63.500 and x > 0 then
return (128*(multiples + 1))*RSignBJ(x)
elseif mod >= 64.501 and x < 0 then
return (128*(multiples + 1))*RSignBJ(x)
elseif mod == 0 and multiples >= 1 then
return (128*(multiples))*RSignBJ(x)
endif
return (abs - mod)*RSignBJ(x)
endfunction
```

JASS:

```
function GetTileCenterEx takes real x returns real
local real result = RAbsBJ(x)
if x < 0 then
return (result - ModuloReal(result, 128.00))*-1
endif
return result - ModuloReal(result, 128.00)
endfunction
```

Lua:

```
--INACCURATE
---@param x number
---@return number
function GetTileCenterEx(x)
local result = math.abs(x)
if x < 0 then
return (result - math.fmod(result, 128.00))*-1
end
return result - math.fmod(result, 128.00)
end
--FULLY ACCURATE
---@param x number
---@return number
function GetTileCenter(x)
local abs = math.abs(x)
local mod = math.fmod(abs, 128.00)
local multiples = math.floor(abs/128)
local sign = 1
if x < 0 then
sign = -1
end
if mod >= 63.500 and x > 0 then
return (128*(multiples + 1))*sign
elseif mod >= 64.501 and x < 0 then
return (128*(multiples + 1))*sign
elseif mod == 0 and multiples >= 1 then
return (128*(multiples))*sign
end
return (abs - mod)*sign
end
```

You can test it on the test map, just to see that I'm not going crazy..., yet.

#### Attachments

Last edited: