function newRectangle(x, y, w, h) return {x=x, y=y, width=w, height=h} end function stringtorect(s) local _, _, x1, y1, x2, y2 = string.find(s, "(%d+),%s*(%d+),%s*(%d+),%s*(%d+)") x1, y1, x2, y2 = tonumber(x1), tonumber(y1), tonumber(x2), tonumber(y2) return newRectangle(x1, y1, x2-x1, y2-y1) end function recttostring(r) return r.x..", "..r.y..", "..r.x+r.width..", "..r.y+r.height end function mergeRectangles(r, s) local x, y = math.min(r.x, s.x), math.min(r.y, s.y) local x2, y2 = math.max(r.x+r.width, s.x+s.width), math.max(r.y+r.height, s.y+s.height) return newRectangle(x, y, x2-x, y2-y) end -- assumes f is an int function scaleRectangle(r, f) return newRectangle(r.x*f, r.y*f, r.width*f, r.height*f) end function translateRect(r, x, y) return newRectangle(r.x+x, r.y+y, r.width, r.height) end