Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

28
LINES

< > BotCompany Repo | #388 // Rectangle functions (stringtorect, newRectangle, recttostring, ...)

Lua code

1  
function newRectangle(x, y, w, h)
2  
  return {x=x, y=y, width=w, height=h}
3  
end
4  
5  
function stringtorect(s)
6  
  local _, _, x1, y1, x2, y2 = string.find(s, "(%d+),%s*(%d+),%s*(%d+),%s*(%d+)")
7  
  x1, y1, x2, y2 = tonumber(x1), tonumber(y1), tonumber(x2), tonumber(y2)
8  
  return newRectangle(x1, y1, x2-x1, y2-y1)
9  
end
10  
11  
function recttostring(r)
12  
  return r.x..", "..r.y..", "..r.x+r.width..", "..r.y+r.height
13  
end
14  
15  
function mergeRectangles(r, s)
16  
  local x, y = math.min(r.x, s.x), math.min(r.y, s.y)
17  
  local x2, y2 = math.max(r.x+r.width, s.x+s.width), math.max(r.y+r.height, s.y+s.height)
18  
  return newRectangle(x, y, x2-x, y2-y)
19  
end
20  
21  
-- assumes f is an int
22  
function scaleRectangle(r, f)
23  
  return newRectangle(r.x*f, r.y*f, r.width*f, r.height*f)
24  
end
25  
26  
function translateRect(r, x, y)
27  
  return newRectangle(r.x+x, r.y+y, r.width, r.height)
28  
end

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

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: 670 / 289
Referenced in: [show references]