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)
endtest 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
| Snippet ID: | #388 |
| Snippet name: | Rectangle functions (stringtorect, newRectangle, recttostring, ...) |
| Eternal ID of this version: | #388/1 |
| Text MD5: | 598681b5832392a5b6e5960de28cd884 |
| Author: | stefan |
| Category: | utility functions |
| Type: | Lua code |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-02-08 19:17:15 |
| Source code size: | 855 bytes / 28 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 1041 / 352 |
| Referenced in: | [show references] |