- Joined
- Oct 20, 2007
- Messages
- 353
I think this will be useful for synchronization systems because it can replace two reals (coordinates only) by one integer and speed up sync process.
I need to warn that it doesn't return exactly the same coordinates, but they are pretty close to original. On 256x256 map coordinates difference is lesser than 0.5 which is practically invisible by human eye. The smaller map the better accuracy.
Library:
Testing library:
I need to warn that it doesn't return exactly the same coordinates, but they are pretty close to original. On 256x256 map coordinates difference is lesser than 0.5 which is practically invisible by human eye. The smaller map the better accuracy.
Library:
JASS:
library Point2Int initializer Init // Version 1.0 by D.O.G
/*
function P2I takes real x, real y returns integer
- Converts X, Y coordinates to integer
function I2X takes integer p returns real
- Gets X coordinate from integer
function I2Y takes integer p returns real
- Gets Y coordinate from integer
*/
globals
private real wx
private real wy
private real ww
private real wh
endglobals
function P2I takes real x, real y returns integer
return (R2I((x - wx) * 65535.00 / ww) + 65536 * R2I((y - wy) * 65535.00 / wh))
endfunction
function I2X takes integer p returns real
if p < 0 then
set p = -2147483648 + p
endif
return I2R(p - (p / 65536) * 65536) * ww / 65535.00 + wx
endfunction
function I2Y takes integer p returns real
if p < 0 then
set p = (-2147483648 + p) / 65536 + 32768
else
set p = p / 65536
endif
return I2R(p) * wh / 65535.00 + wy
endfunction
private function Init takes nothing returns nothing
local rect w = GetWorldBounds()
set wx = GetRectMinX(w)
set wy = GetRectMinY(w)
set ww = GetRectMaxX(w) - wx
set wh = GetRectMaxY(w) - wy
call RemoveRect(w)
set w = null
endfunction
endlibrary
Testing library:
JASS:
library Point2IntTest initializer Init requires Point2Int
private function Actions takes nothing returns boolean
local real x = GetOrderPointX()
local real y = GetOrderPointY()
local integer p = P2I(x, y)
local real x2 = I2X(p)
local real y2 = I2Y(p)
call BJDebugMsg("Actual point: " + R2S(x) + " " + R2S(y))
call BJDebugMsg("Saved point: " + I2S(p))
call BJDebugMsg("Loaded point: " + R2S(x2) + " " + R2S(y2))
call BJDebugMsg("Difference: " + R2S(x - x2) + " " + R2S(y - y2) + "\n\n")
return false
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER)
call TriggerAddCondition(t, Condition(function Actions))
set t = null
endfunction
endlibrary