PDA

View Full Version : Re-Scaling to Meters



Oberoten
May 16th, 2008, 07:51
Very quick Script :


To put the measurements to one grid-square = 1 meter AND get areas instead of sides of area effects with the results rounded to 1 decimal do the following :


In imagewindow_image.lua

Replace the whole
function onMeasureVector(token, vector)
.....
end

with



function onMeasureVector(token, vector)
if hasGrid() then
local diagonals = 0;
local straights = 0;
local gridsize = getGridSize();

for i = 1, #vector do
local gx = math.abs(math.floor(vector[i].x / gridsize));
local gy = math.abs(math.floor(vector[i].y / gridsize));

if gx > gy then
diagonals = diagonals + gy;
straights = straights + gx - gy;
else
diagonals = diagonals + gx;
straights = straights + gy - gx;
end
end

local squares = diagonals * 1.5 + straights;
local feet = squares * 1;
return math.floor( (feet * 10^1) + 0.5) / (10^1) .. "\ Meters";
else
return "";
end
end


Also replace :

function onMeasurePointer(length)
.....
end

with



function onMeasurePointer(length)
if hasGrid() then

return math.floor( ((((length / getGridSize() * 1) * 10^1) + 0.5) / (10^1))^2) .. "\ Meter(s)";
else
return "";
end
end



If you wish to change the number of decimals just change the blue numbers to the number of decimals desired.

Oberoten
May 16th, 2008, 08:03
Oh typical... NOW the rounding is broken.

Oberoten
May 16th, 2008, 08:13
Corrected code reads :

Also... Hmm... What borked it seemed to be the fact that i tried to calculate area rather than side of squares and cones.

Anyone wanna give me a hint on how to do this properly?




function onMeasureVector(token, vector)
if hasGrid() then
local diagonals = 0;
local straights = 0;
local gridsize = getGridSize();

for i = 1, #vector do
local gx = math.abs(math.floor(vector[i].x / gridsize));
local gy = math.abs(math.floor(vector[i].y / gridsize));

if gx > gy then
diagonals = diagonals + gy;
straights = straights + gx - gy;
else
diagonals = diagonals + gx;
straights = straights + gy - gx;
end
end

local squares = diagonals * 1.5 + straights;
local meters = squares * 1;
return meters .. "\ Meters";
else
return "";
end
end
function onMeasurePointer(length)
if hasGrid() then

local rounded = math.floor( ( (length / getGridSize() * 1) * 10^2) + 0.5) / (10^2)
return rounded .. "\ Meter(s)";
else
return "";
end
end

Foen
May 16th, 2008, 16:34
I'll give it a go and post back later.

BTW, I think there is a potential problem with the quoted string "\ Meter(s)", which should probably read " Meter(s)". Or even " Metre(s)" if you are a stickler for SI spelling *grin*

The backslash character is an escape character and is probably left over from having \' where it means a real apostrophe rather than the end of a single-quoted string.

Stuart

Foen
May 16th, 2008, 18:08
Hi Oberoten

Hopefully this should do the trick:



local unitname = "\'";
local multiple = 5;
local decimals = 0;

function round(value, dp)
local result = value * (10^dp);
result = math.floor(result + 0.5);
return result / (10^dp);
end

function onMeasureVector(token, vector)
if hasGrid() then
local diagonals = 0;
local straights = 0;
local gridsize = getGridSize();

for i = 1, #vector do
local gx = math.abs(math.floor(vector[i].x / gridsize));
local gy = math.abs(math.floor(vector[i].y / gridsize));

if gx > gy then
diagonals = diagonals + gy;
straights = straights + gx - gy;
else
diagonals = diagonals + gx;
straights = straights + gy - gx;
end
end

local squares = math.floor(diagonals * 1.5) + straights;
local units = squares * multiple;

return units .. unitname;
else
return "";
end
end

function onMeasurePointer(length)
if hasGrid() then
return round(length / getGridSize() * multiple, decimals) .. unitname;
else
return "";
end
end


The units, multiples (number of units per square) and decimal places are now more easily changed.

For your requirements, I suggest:



local unitname = "m"; --[[ or " Meter(s)" ]]
local multiple = 1;
local decimals = 1;


Hope that helps

Stuart

Oberoten
May 17th, 2008, 07:51
It allready worked with the meter-scaling. What I was thinking about was if there was a way to change how it measure the rectangles per example so you get the number of units covered instead of just the range from the middle to the edge.

Foen
May 17th, 2008, 08:41
Sorry Oberoten, I misunderstood your request.

Unfortunately it doesn't seem you can infer the context of a call to onMeasurePointer (the reference is here (https://www.fantasygrounds.com/refdoc/imagecontrol.xcp)) and there doesn't seem to be any other events exposed that would allow you to do it.

Stuart

Oberoten
May 17th, 2008, 09:37
Don't sorry me. :) Willingness to help is always aprechiated.

- Obe