get("#348") -- bright and rgb
function getLeftmostPoint(img, y)
local x1 = 0
while x1 < img.width and img.getInt(x1, y) >= 0xF00000 do
x1 = x1+1
end
return x1
end
function getRightmostPoint(img, y)
local x2 = img.width
while x2 > 0 and img.getInt(x2-1, y) >= 0xF00000 do
x2 = x2-1
end
return x2
end
function getWidth(img, y)
local x2 = getRightmostPoint(img, y)
local x1 = getLeftmostPoint(img, y)
return math.max(0, x2-x1)
end
function getAverageWidth(img, y1, y2)
local sum = 0
for y = y1, y2-1 do
sum = sum+getWidth(img, y)
end
return sum/(y2-y1)
end
function getAverageLeft(img, y1, y2)
y1, y2 = y1 or 0, y2 or img.height -- default arguments
local sum = 0
for y = y1, y2-1 do
sum = sum+getLeftmostPoint(img, y)
end
return sum/(y2-y1)
end
function getAverageLeft_debug(img, y1, y2)
y1, y2 = y1 or 0, y2 or img.height -- default arguments
local sum = 0
for y = y1, y2-1 do
local x = getLeftmostPoint(img, y)
sum = sum+x
print("y", y, "left", x)
end
return sum/(y2-y1)
end
function getMaxWidth(img, y1, y2)
local max = 0
for y = y1, y2-1 do
max = math.max(max, getWidth(img, y))
end
return max
end
function upperWidth(img)
return getAverageWidth(img, 0, math.floor(img.height/3))
end
function upperMaxWidth(img)
return getMaxWidth(img, 0, math.floor(img.height/3))
end
function lowerWidth(img)
return getAverageWidth(img, math.floor(img.height*2/3), img.height)
end
function lowerMaxWidth(img)
return getMaxWidth(img, math.floor(img.height*2/3), img.height)
end
function upperToLowerWidth(img)
return upperWidth(img) / lowerWidth(img)
end
function getCenterOfGravity(img)
local sumx, sumy, sumdensity = 0, 0, 0
for y=0, img.height-1 do
for x=0, img.width-1 do
local density = 1-bright(rgb(img.getInt(x,y)))
sumdensity = sumdensity+density
sumx = sumx+density*x
sumy = sumy+density*y
end
end
return sumx/sumdensity, sumy/sumdensity
endSimilar to #343, but with "img" argument
test run test run with input download show line numbers
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment