Unicode rectangles

10

2

Given

  • a matrix a of characters from u=" ╶╺╵└┕╹┖┗╴─╼┘┴┶┚┸┺╸╾━┙┵┷┛┹┻╷┌┍│├┝╿┞┡┐┬┮┤┼┾┦╀╄┑┭┯┥┽┿┩╃╇╻┎┏╽┟┢┃┠┣┒┰┲┧╁╆┨╂╊┓┱┳┪╅╈┫╉╋"
  • the coordinates of a submatrix as x,y,w,h (left, top, width>1, height>1)
  • a thickness t of 1 (as in ) or 2 (as in )

render an inner border for the submatrix with the specified thickness, taking into account existing lines.

x=4;y=1;w=2;h=3;t=2;
a=[' ┌───┐',
   '┌┼┐  │',
   '│└┼──┘',
   '└─┘   ']

// output
r=[' ┌───┐',
   '┌┼┐ ┏┪',
   '│└┼─╂┨',
   '└─┘ ┗┛']

When overwriting a line fragment, the new thickness should be the greater of the old thickness and t.

This isn't about input parsing or finding the Kolmogorov complexity of Unicode, so you may assume a,u,x,y,w,h,t are available to you as variables. Also, you may put the result in a variable r instead of returning or outputting it, as long as r is of the same type as a.

If your language forces you to put code in functions (C, Java, Haskell, etc) and your solution consists of a single function, you can omit the function header and footer.

Larger test:

x=4;y=1;w=24;h=4;t=1;
a=['┏┱─────┐         ┌┐     ┎──┲━┓',
   '┠╂─────┘         ││     ┃  ┗━┛',
   '┃┃               ││     ┃     ',
   '┠╂──┲━━┓  ┏━━━━┓ ││    ┌╂┰┐   ',
   '┃┃  ┗━━┩  ┃    ┃ └╆━┓  └╂┸┘   ',
   '┃┃     │  ┃    ┃  ┃ ┃   ┃     ',
   '┗┹─────┘  ┗━━━━┛  ┗━┛   ╹     ']

// output
r=['┏┱─────┐         ┌┐     ┎──┲━┓',
   '┠╂──┬──┴─────────┼┼─────╂──╄━┛',
   '┃┃  │            ││     ┃  │  ',
   '┠╂──╆━━┓  ┏━━━━┓ ││    ┌╂┰┐│  ',
   '┃┃  ┗━━╃──╂────╂─┴╆━┱──┴╂┸┴┘  ',
   '┃┃     │  ┃    ┃  ┃ ┃   ┃     ',
   '┗┹─────┘  ┗━━━━┛  ┗━┛   ╹     ']

ngn

Posted 2017-08-29T10:22:53.387

Reputation: 11 449

will the input always hold 0 <= x < x + w < width(a) and 0 <= y < y + h < height(a)? – tsh – 2017-08-29T10:30:18.583

@tsh yes, input will be valid – ngn – 2017-08-29T10:42:39.407

Bah, my default font is dodgy - it shows some of those characters with the wrong heaviness unless you zoom in a lot. – Neil – 2017-08-29T11:51:36.960

@Neil I'm sorry about that. One workaround is to paste the examples in an editor where you can choose the font. – ngn – 2017-08-29T12:04:16.027

1About your bounty - it is impossible to award three 150-rep bounties. You have to double the rep count every time you start another bounty on the same question. – MD XF – 2017-09-05T20:42:17.710

@MDXF Oh... I didn't think about that. Thanks. There must be ways around it though, e.g. start a bounty on a fake question. – ngn – 2017-09-05T22:17:37.037

Answers

2

JavaScript, 218 bytes

(a,x,y,w,h,t,u)=>a.map((l,j)=>l.map((c,i)=>u[(g=(a,b)=>a?g(a/3|0,b/3|0)*3+Math.max(a%3,b%3):b)(u.indexOf(c),t*((j==y||j==y+h-1)*((i>x&&i<x+w)*9+(i>=x&&i<x+w-1))+(i==x||i==x+w-1)*((j>y&&j<y+h)*3+(j>=y&&j<y+h-1)*27)))]))

a should be taken as array of array of char.

f =

(a,x,y,w,h,t,u)=>a.map((l,j)=>l.map((c,i)=>u[(g=(a,b)=>a?g(a/3|0,b/3|0)*3+Math.max(a%3,b%3):b)(u.indexOf(c),t*((j==y||j==y+h-1)*((i>x&&i<x+w)*9+(i>=x&&i<x+w-1))+(i==x||i==x+w-1)*((j>y&&j<y+h)*3+(j>=y&&j<y+h-1)*27)))]))

x=4;y=1;w=24;h=4;t=1;
a=['┏┱─────┐         ┌┐     ┎──┲━┓',
   '┠╂─────┘         ││     ┃  ┗━┛',
   '┃┃               ││     ┃     ',
   '┠╂──┲━━┓  ┏━━━━┓ ││    ┌╂┰┐   ',
   '┃┃  ┗━━┩  ┃    ┃ └╆━┓  └╂┸┘   ',
   '┃┃     │  ┃    ┃  ┃ ┃   ┃     ',
   '┗┹─────┘  ┗━━━━┛  ┗━┛   ╹     '].map(x => [...x])
u=" ╶╺╵└┕╹┖┗╴─╼┘┴┶┚┸┺╸╾━┙┵┷┛┹┻╷┌┍│├┝╿┞┡┐┬┮┤┼┾┦╀╄┑┭┯┥┽┿┩╃╇╻┎┏╽┟┢┃┠┣┒┰┲┧╁╆┨╂╊┓┱┳┪╅╈┫╉╋";

output = f(a,x,y,w,h,t,u).map(x => x.join('')).join('\n');
document.write(`<pre lang="en" style="font-family: Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,sans-serif;">${output}</pre>`)

tsh

Posted 2017-08-29T10:22:53.387

Reputation: 13 072

as mentioned above, you are allowed to replace (a,x,y,w,h,t,u)=>... with r=... – ngn – 2017-08-30T07:42:48.507

2

Python 3, 226 201 197 bytes

n,m=x+w-1,y+h-1
r=[*map(list,a)]
R=range
for i in R(x,x+w):
 for j in R(y,y+h):A,B=j in(y,m),i in(x,n);r[j][i]=u[sum(3**o*max((i<n*A,y<j*B,x<i*A,j<m*B)[o]*t,u.index(a[j][i])//3**o%3)for o in R(4))]

Try it online!

Ungolfed:

n,m=x+w-1,y+h-1
r=[*map(list,a)]
for i in range(x,x+w):
 for j in range(y,y+h):
  p=u.index(a[j][i])
  c=(p%3,p%9//3,p%27//9,p//27)
  A,B=j in(y,m),i in(x,n)
  P=(i<n*A,y<j*B,x<i*A,j<m*B)
  l=sum(max(P[o]*t,c[o])*3**o for o in range(4))
  r[j][i]=u[l]

TFeld

Posted 2017-08-29T10:22:53.387

Reputation: 19 246

(p%3,p%9//3,p%27//9,p//27)[o]p//3**o%3 saves a bunch of bytes. Then max(…)*3**o for3**o*max(…)for saves one more. And then you can eke one more out by inlining the 3** and shuffling P around to index it with o%5-1, yielding: sum(o*max((i<n*A,j<m*B,y<j*B,x<i*A)[o%5-1]*t,p//o%3)for o in(1,3,9,27)) – Lynn – 2017-09-01T16:12:46.800

Err, that last step is a bad idea. Instead you can do R=range and get it down to 201

– Lynn – 2017-09-01T16:15:02.787

1

JavaScript (ES6), 174 bytes

r=a.map((l,j)=>l.map((c,i)=>u[c=u.indexOf(c),g=n=>c/n%3<t&&g(n,c+=n),j==y|j==h&&(i>=x&i<w&&g(1),i>x&i<=w&&g(9)),i==x|i==w&&(j>=y&j<h&&g(27),j>y&j<=h&&g(3)),c]),w+=x-1,h+=y-1)

a=`
┏┱─────┐         ┌┐     ┎──┲━┓
┠╂─────┘         ││     ┃  ┗━┛
┃┃               ││     ┃     
┠╂──┲━━┓  ┏━━━━┓ ││    ┌╂┰┐   
┃┃  ┗━━┩  ┃    ┃ └╆━┓  └╂┸┘   
┃┃     │  ┃    ┃  ┃ ┃   ┃     
┗┹─────┘  ┗━━━━┛  ┗━┛   ╹     
`.match(/.+/g).map(s=>[...s]);
x=4;
y=1;
w=24;
h=4;
t=1;
u=" ╶╺╵└┕╹┖┗╴─╼┘┴┶┚┸┺╸╾━┙┵┷┛┹┻╷┌┍│├┝╿┞┡┐┬┮┤┼┾┦╀╄┑┭┯┥┽┿┩╃╇╻┎┏╽┟┢┃┠┣┒┰┲┧╁╆┨╂╊┓┱┳┪╅╈┫╉╋";
r=a.map((l,j)=>l.map((c,i)=>u[c=u.indexOf(c),g=n=>c/n%3<t&&g(n,c+=n),j==y|j==h&&(i>=x&i<w&&g(1),i>x&i<=w&&g(9)),i==x|i==w&&(j>=y&j<h&&g(27),j>y&j<=h&&g(3)),c]),w+=x-1,h+=y-1)
;document.write(`<pre style="font-family:Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,sans-serif">${r.map(s=>s.join``).join`\n`}</pre>`);

Neil

Posted 2017-08-29T10:22:53.387

Reputation: 95 035