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

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

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: 614 / 267
Referenced in: #409 - Grid + Floodfill
#411 - Grid + Floodfill with hashes
#412 - LXTerminal Cursor Finder
#413 - Symbol Finder (include)
#416 - Grid + Floodfill with rough hashes
#418 - Grid + Floodfill with string rep
#420 - Grid + Floodfill with hashes / limited start area (for start menus)
#421 - Grid + Floodfill with hashes / limited start area (for start menus), optimized
#422 - Grid + Floodfill with hashes (with virtual stack)
#423 - Grid + Floodfill with hashes (hacked for galculator)
#424 - LXTerminal Contents Finder
#425 - LXTerminal Contents Segmenter
#426 - LXTerminal Contents Recognizer
#427 - LXTerminal character reference maker
#428 - LXTerminal Contents Finder (include)
#429 - LXTerminal character reference maker 2
#432 - LXTerminal Contents Recognizer 2
#434 - LXTerminal Cursor Finder 2
#437 - LXTerminal Contents Recognizer 2 (cursor line only)
#438 - LXTerminal Contents Recognizer 2 (repeatable)
#439 - LXTerminal Contents Recognizer 2 (cursor line only, repeatable)
#440 - LXTerminal Cursor Finder 2 (repeatable)
#442 - Grid + Floodfill with hashes (size 3)
#444 - White Grid + Floodfill
#445 - White Grid
#446 - White Spaces (include)
#447 - Java recognizer pre-experiment
#452 - Java grid locator (include)
#453 - Java character reference maker
#455 - Java source recognizer
#456 - Java character reference maker (non-bold)
#461 - Grid + Floodfill with hashes (size 2)
#463 - Grid + Floodfill (size = 50)
#464 - Grid + Floodfill (size = 15)
#465 - Grid + Floodfill (size = 10)
#469 - Grid + Floodfill with hashes (for lighter elements)
#476 - Haiku font recognizer (collecting data)
#478 - Horizontal split with crop (include)
#480 - Horizontal split with crop 2 (include)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)