24
\$\begingroup\$

Growing up, my first console game system was an Atari 2600 and I will always have a love for some of those games I so enjoyed as a child. Many of the graphics are still memorable, perhaps even iconic.

It turns out that these sprites are very simplistic bitmaps, 8 pixels wide with variable height where the binary representation is the arrangement of the pixels.

For example, the hex bytes 0x18, 0x24, 0x18 would draw a crude circle like so:

0x18: 00011000
0x24: 00100100
0x18: 00011000

As 8 pixels wide creates fairly small graphics (even by Atari 2600 standards) it was common to double or quadruple either the height, width or both to create a larger (though more blocky and distorted) version of the same image. They would commonly also be flipped vertically or horizontal for both player sprites and playfields. The game Combat is a good example of this.

The challenge is, to write code to display these sprites as "graphics" in ASCII form including the ability to stretch or flip them vertically, horizontally or both. This must be in the form of either a full program, or callable function.

Input:

  • An array of bytes, each representing the horizontal bits for that line.
  • A non-zero integer value for each direction, horizontal and vertical representing the scaling factor for that dimension.
  • A negative value indicates that the dimension should also be flipped along it's axis.

Output:

  • ASCII representation to STDOUT or a newline-separated string, using a space character for black (0) pixels and any printable, non-space character of your choice for white (1) pixels.

Test data:

bmp1 = [ 0x06, 0x0F, 0xF3, 0xFE, 0x0E, 0x04, 0x04, 0x1E, 0x3F, 0x7F, 0xE3, 0xC3, 0xC3, 0xC7, 0xFF, 0x3C, 0x08, 0x8F, 0xE1, 0x3F ]
bmp2 = [ 0x07, 0xFD, 0xA7 ]
bmp3 = [ 0x00, 0x8E, 0x84, 0xFF, 0xFF, 0x04, 0x0E, 0x00 ]
bmp4 = [ 0x00, 0xFC, 0xFC, 0x38, 0x3F, 0x38, 0xFC, 0xFC]

Note: Above example input arrays of bytes are provided as hex. If your platform does not accept hex literals for byte representation you may convert them to a native byte-equivalent literal.

Example Output:

f( bmp1, 1, 1 ) =>
--------
     XX 
    XXXX
XXXX  XX
XXXXXXX 
    XXX 
     X  
     X  
   XXXX 
  XXXXXX
 XXXXXXX
XXX   XX
XX    XX
XX    XX
XX   XXX
XXXXXXXX
  XXXX  
    X   
X   XXXX
XXX    X
  XXXXXX
--------

f( bmp1, -2, 1 ) =>
----------------
  XXXX          
XXXXXXXX        
XXXX    XXXXXXXX
  XXXXXXXXXXXXXX
  XXXXXX        
    XX          
    XX          
  XXXXXXXX      
XXXXXXXXXXXX    
XXXXXXXXXXXXXX  
XXXX      XXXXXX
XXXX        XXXX
XXXX        XXXX
XXXXXX      XXXX
XXXXXXXXXXXXXXXX
    XXXXXXXX    
      XX        
XXXXXXXX      XX
XX        XXXXXX
XXXXXXXXXXXX    
----------------

f( bmp2, 1, 2 ) =>
--------
     XXX
     XXX
XXXXXX X
XXXXXX X
X X  XXX
X X  XXX
--------

f( bmp2, 2, 1 ) =>
----------------
          XXXXXX
XXXXXXXXXXXX  XX
XX  XX    XXXXXX
----------------

f( bmp2, -2, -2 ) =>
----------------
XXXXXX    XX  XX
XXXXXX    XX  XX
XX  XXXXXXXXXXXX
XX  XXXXXXXXXXXX
XXXXXX          
XXXXXX          
----------------

f( bmp3, 1, -1 ) =>
--------

    XXX 
     X  
XXXXXXXX
XXXXXXXX
X    X  
X   XXX 

--------

f( bmp3, 3, 3 ) =>
------------------------



XXX         XXXXXXXXX   
XXX         XXXXXXXXX   
XXX         XXXXXXXXX   
XXX            XXX      
XXX            XXX      
XXX            XXX      
XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX
               XXX      
               XXX      
               XXX      
            XXXXXXXXX   
            XXXXXXXXX   
            XXXXXXXXX   



------------------------

f( bmp4, -1, -1 ) =>
--------
  XXXXXX
  XXXXXX
   XXX  
XXXXXX  
   XXX  
  XXXXXX
  XXXXXX

--------

f( bmp4, 4, 2 ) =>
--------------------------------


XXXXXXXXXXXXXXXXXXXXXXXX        
XXXXXXXXXXXXXXXXXXXXXXXX        
XXXXXXXXXXXXXXXXXXXXXXXX        
XXXXXXXXXXXXXXXXXXXXXXXX        
        XXXXXXXXXXXX            
        XXXXXXXXXXXX            
        XXXXXXXXXXXXXXXXXXXXXXXX
        XXXXXXXXXXXXXXXXXXXXXXXX
        XXXXXXXXXXXX            
        XXXXXXXXXXXX            
XXXXXXXXXXXXXXXXXXXXXXXX        
XXXXXXXXXXXXXXXXXXXXXXXX        
XXXXXXXXXXXXXXXXXXXXXXXX        
XXXXXXXXXXXXXXXXXXXXXXXX        
--------------------------------

Note: the horizontal lines above and below are to show the beginning and end of the output. They are not required in the output, however empty lines (represented by all zeros/spaces) at the beginning and/or end are required, as shown.

Note 2: these test bitmaps were inspired by and re-drawn/coded based on game screenshots tagged as "fair use" on Wikipedia.

Winning Criteria

  • This is , so the shortest code in bytes per language wins.
  • Standard loopholes are forbidden.
\$\endgroup\$
9
  • 7
    \$\begingroup\$ "Somebody get this freakin' duck away from me!" - Strong Bad \$\endgroup\$ Jan 29, 2019 at 14:29
  • 9
    \$\begingroup\$ The irony is that even the cleverest golfing here will probably not be as clever as what programmers for the Atari 2600 actually had to do if they wanted anything more interesting than a Pong clone -- the whole screen was rendered one line at a time and the CPU spent most of its time doing that. With only 128 bytes of RAM, there was no room for a luxury like a screen buffer... The five whole sprites you got were the luxury. \$\endgroup\$ Jan 29, 2019 at 15:16
  • \$\begingroup\$ Can we take the input as a list of 8-bit binary-strings, or similar formats where the bytes are already unpacked into bits? \$\endgroup\$
    – Luis Mendo
    Jan 29, 2019 at 16:09
  • \$\begingroup\$ @LuisMendo "If your platform does not accept hex literals for byte representation you may convert them to a native byte-equivalent literal." \$\endgroup\$ Jan 29, 2019 at 16:09
  • \$\begingroup\$ @KevinCruijssen That's the point, I don't know what is accepted as equivalent. Does that open the door to inputting the bitmap directly? \$\endgroup\$
    – Luis Mendo
    Jan 29, 2019 at 16:31

16 Answers 16

5
\$\begingroup\$

Python 2, 117 bytes

def f(m,w,h):
 for r in m[::cmp(h,0)]:print(''.join(' X'[1<<i&r>0]*abs(w)for i in range(8)[::cmp(0,w)])+'\n')*abs(h),

Try it online!

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 27 26 bytes

εS²Ä×J³Äи²0‹ií]³0‹iR}˜0ð:»

Takes the input as a list of 8-bit binary-strings, and outputs with 1 as non-space character.

-1 byte thanks to @MagicOctopusUrn.

Try it online or verify all test cases.

Explanation:

ε         # Map the (implicit) input-list to:
 S        #  Convert the binary-String to a list of characters
  ²Ä      #  Take the absolute value of the second input
    ×     #  And repeat each character that many times
     J    #  And then join it back together to a single string again
 ³Ä       #  Take the absolute value of the third input
   и      #  Repeat that string as a list that many times
 ²0‹i     #  If the second input is negative:
     í    #   Reverse each string in the list
]         # Close both the if-statement and (outer) map
³0‹i }    # If the third input is negative:
    R     #  Reverse the list of lists
      ˜   # Flatten the list of lists to a list of strings
0ð:       # Replace all 0s with spaces " "
   »      # And join the strings by newlines (which is output implicitly)
\$\endgroup\$
5
  • \$\begingroup\$ There has to be a 2-byter for 0‹i... \$\endgroup\$ Jan 29, 2019 at 16:35
  • \$\begingroup\$ @MagicOctopusUrn There should be a 1-byte for 0‹ indeed.. We do have a 1-byter for >=0, which is d. But we should also have a 1-byter to check for negative imo. Now I just use 0‹ or d_. \$\endgroup\$ Jan 29, 2019 at 16:41
  • \$\begingroup\$ All I could come up with was: „íR³²‚0‹Ï.V (full code εε²Ä×}J³Äи0ð:}„íR³²‚0‹Ï.V˜») which isn't an improvement, but does get rid of one of those negative checks. \$\endgroup\$ Jan 29, 2019 at 16:49
  • 1
    \$\begingroup\$ Also, pretty sure εS²Ä×J³Äи²0‹ií]³0‹iR}˜0ð:» saves a byte. If you can take in a 2D array, you can remove the S entirely for 25 bytes. \$\endgroup\$ Jan 29, 2019 at 17:12
  • \$\begingroup\$ @MagicOctopusUrn Ah of course, S²Ä× instead of ε²Ä×}. Thanks! Hmm, if we are allowed to take the binary-inputs as a list of 0s and 1s an additional byte could be saved by omitting the S. Will ask OP if this is allowed. I like your „íR³²‚0‹Ï.V in your other comment as well. :) \$\endgroup\$ Jan 29, 2019 at 17:19
3
\$\begingroup\$

MATL, 24 19 bytes

B,!i|1&Y"2M0<?XP]Zc

Inputs are an array of decimal numbers, horizontal scale, vertical scale.

Try it online!

Explanation

B        % Implicit input: array of numbers. Convert to binary. Gives a zero-one
         % matrix, each row containing the binary expansion of a number
,        % Do twice
  !      %   Transpose
  i      %   Input: number
  |      %   Absolute value
  1&Y"   %   Repeat each row that many times
  2M     %   Push the latest input again
  0<     %   Is it negative?
  ?      %   If so:
    XP   %     Flip vertically
  ]      %   End
  Zc     %   Convert each nonzero into '#'. Zeros are displayed as space
         % Implicit end. Implicit display
\$\endgroup\$
3
\$\begingroup\$

Dyalog APL, 46 42 33 bytes

' #'[⍉⊃{⊖⍣(0>⍺)⍉⍵/⍨|⍺}/⎕,⊂⎕⊤⍨8/2]

Try it online!

-9 thanks to ngn!

\$\endgroup\$
3
  • \$\begingroup\$ each -> reduce: {' #'[⊃{⌽⍣(0>⍺)⊢(|⍺)/⍉⍵}/⍺,⊂⍉⍵⊤⍨8/2]} dfn -> program: ' #'[⊃{⌽⍣(0>⍺)⊢(|⍺)/⍉⍵}/⎕,⊂⍉⎕⊤⍨8/2] \$\endgroup\$
    – ngn
    Apr 7, 2019 at 16:00
  • \$\begingroup\$ shorter: ' #'[⍉⊃{⊖⍣(0>⍺)⍉⍵/⍨|⍺}/⎕,⊂⎕⊤⍨8/2]. btw, the output for the second test seems reversed in your original solution \$\endgroup\$
    – ngn
    Apr 7, 2019 at 16:19
  • \$\begingroup\$ @ngn thanks! the inputs for the 2nd example should've been reversed to match the 2nd test-case in the question. \$\endgroup\$
    – dzaima
    Apr 7, 2019 at 16:41
3
\$\begingroup\$

Prolog (SWI), 252 bytes

N+E+R:-N<1,R=[];N-1+E+S,R=[E|S].
N*E*R:-R=E,E=[];N<0,reverse(E,F),-N*F*R;[H|T]=E,N+H+S,N*T*U,append(S,U,R).
N/E/R:-N<1,R=[];(E<N,D=E,F=32;D=E-N,F=35),N/2/D/C,R=[F|C].
[H|T]^X^Y^R:-128/H/A,X*A*B,Y*[[10|B]]*C,append(C,D),(T=[],R=D;T^X^Y^S,append(D,S,R)).

Try it online!

Explanation

N+E+R:-N<1,R=[];N-1+E+S,R=[E|S].   Make `R` a list containing `E` repeated `N` times
       N<1,R=[]                    If `N<1`, let `R` be the empty list
       N-1+E+S                     Else recurse with `N-1`, `E` and `S`
           R=[E|S]                 Let `R` be a new list with `E` as head and `S` as tail
N*E*R:-R=E,E=[];N<0,reverse(E,F),-N*F*R;[H|T]=E,N+H+S,N*T*U,append(S,U,R).
                                   Let `R` be a list
                                   with each element in `E` repeated `N` times
                                   e.g. 2*[3, 6] -> [3, 3, 6, 6]
       R=E,E=[]                    Let `R` be `E` if `E` is the empty list
       N<0,reverse(E,F)            Else if `N<0`, let `F` be the reverse of `E`
           -N*F*R                  Recurse with `-N`, `F` and `R`
       [H|T]=E                     Else let `H` be the head and `T` be the tail of `E`
           N+H+S                   Let `S` be `N+H+S` (our function, not addition)
           N*T*U                   Recurse with `N`, `T` and `U`
           append(S,U,R)           let `R` be the concatenation of `S` and `U`
N/E/R:-N<1,R=[];(E<N,D=E,F=32;D=E-N,F=35),N/2/D/C,R=[F|C].
                                   Make `R` the binary representation of `E`
                                   with `N` as the value of the current bit
                                   where 0 and 1 are space and hash respectively
    N<1,R=[]                       If `N<1` let `R` be the empty list
    (
        E<N,D=E,F=32               If `E<N` the bit isn't set, so `D=E`, `F=space`
        D=E-N,F=35                 Else `D=E-N`, `F=hash`
    )
        N/2/D/C                    Recurse with `N/2`, `D` and `C`
        R=[F|C]                    Let `R` be a new list with `F` as head and `C` as tail
[H|T]^X^Y^R:-128/H/A,X*A*B,Y*[[10|B]]*C,append(C,D),(T=[],R=D;T^X^Y^S,append(D,S,R)).
                                   Make `R` the result,
                                   with inputs being the list `[H|T]`
                                   and the scales `X` and `Y`
   128/H/A                         Let `A` be the binary representation of `H` (8 bits)
   X*A*B                           Let `B` be `A` with each element repeated `X` times
   Y*[[10|B]]*C                    Let `C` be `B` with a newline prepended,
                                   repeated `Y` times
   append(C,D)                     Let `D` be `C` flattened by one level (joining lines)
   (
       T=[],R=D                    If `T` is empty, let `R` be `D` 
       T^X^Y^S                     Else recurse with `T`, `X`, `Y` and `S`
           append(D,S,R)           Let `R` be the concatenation of `D` and `S`
   )
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 28 bytes

FθE↔ζ⭆⮌↨ι²×§ Xμ↔ηF›η⁰‖F‹ζ⁰‖↓

Try it online! Link is to verbose version of code. Explanation:

Fθ

Loop over the list of bytes.

E↔ζ

Map over the vertical scaling factor, thus multiplying the output lines.

⭆⮌↨ι²×§ Xμ↔η

Convert the input to base 2, reverse it, map the digits to space and X, then multiply each character by the horizontal scaling factor.

F›η⁰‖

If the horizontal scaling factor was positive, reflect to get the image the correct way around again.

F‹ζ⁰‖↓

Reflect vertically if the vertical scaling factor was negative.

\$\endgroup\$
3
  • \$\begingroup\$ Not that it would save any bytes, but I'm just curious: why did you use F (For) instead of ¿ (If) for the checks? \$\endgroup\$ Jan 29, 2019 at 16:59
  • 1
    \$\begingroup\$ @KevinCruijssen In Charcoal succinct mode the else is implied so the only time I can use if is if it's the last statement in the block. \$\endgroup\$
    – Neil
    Jan 29, 2019 at 18:53
  • \$\begingroup\$ Ah ok, didn't knew about that. So using two If here would actually be an If ... Else If ... instead of two loose If. Hmm, good to know. \$\endgroup\$ Jan 29, 2019 at 19:00
2
\$\begingroup\$

C (clang), 120 bytes

k,w,l,_;f(*o,z,x,y){for(w=z*y;w;)for(k=w>0?z*y-w--:++w,_=l=8*x;_;putchar(_?o[k/y]>>(l>0?--l/x:7-++l/x)&1?88:46:10))_=l;}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Saved 2 bytes thanks to ceilingcat \$\endgroup\$
    – AZTECCO
    Jan 31, 2019 at 22:51
2
+100
\$\begingroup\$

Common Lisp, 157 bytes

(lambda(l x y)(dolist(i(if(< y 0)(reverse l)l))(dotimes(j(abs y))(dotimes(n 8)(dotimes(k(abs x))(princ(if(logbitp(if(< x 0)n(- 7 n))i)"#"" "))))(princ"
"))))

Try it online!

Explanation

(lambda(l x y)                           ; Lambda with parameters `l`, `x`, `y`
    (dolist
        (i                               ; For `i` in the list  
            (if(< y 0)(reverse l)l)      ; The reverse of `l` if `y<0` else `l`
        )
        (dotimes(j(abs y))(dotimes(n 8)(dotimes(k(abs x))
                                         ; Do `y` times, for `n` from 0 to 7, do `x` times
        (princ(if(logbitp(if(< x 0)n(- 7 n))i)"#"" "))))
                                         ; If `x<0` and the `n`th bit is 1
                                         ; or `x>0` and the `7-n`th bit is 1
                                         ; print "#", else print " "
        (princ"
")                                       ; After every `y` loop, print a newline
        )
    )
)
\$\endgroup\$
2
\$\begingroup\$

Tcl, 192 bytes

proc f {l x y} {lmap i [if $y<0 {lreverse $l} {lindex $l}] {time {lmap n {0 1 2 3 4 5 6 7} {time {puts -nonewline [expr $i&1<<($x<0?$n:7-$n)?{#}:{ }]} [expr abs($x)]};puts {}} [expr abs($y)]}}

Try it online!

proc f {l x y}                           Define a function `f` with arguments `l`, `x`, `y`
{lmap i                                  For each `i` in
    [if $y<0 {lreverse $l} {lindex $l}]  The reverse of `l` if `y<0` else `l`
    {
        time {                           Do `abs(y)` times
            lmap n {0 1 2 3 4 5 6 7} {   For `n` from 0 to 7
                time {                   Do `abs(x)` times
                    puts -nonewline      Print without newline
                         [expr $i&1<<($x<0?$n:7-$n)?{#}:{ }]
                                         If `x<0` and the `n`th bit is 1 or
                                         `x>0` and the `7-n`th bit is 1
                                         then return "#" else return " "
                } [expr abs($x)]
            };
            puts {}                      Print a newline
        } [expr abs($y)]
    }
}
\$\endgroup\$
2
\$\begingroup\$

8088 machine code, IBM PC DOS, 77 71 bytes

Assembled:

B402 84FF 7906 FD03 F14E F6DF 518A CFAC 5051 B108 8AF3 84F6 7902 F6DE
518A CEB2 2384 DB79 04D0 C8EB 02D0 C072 02B2 2050 CD21 58E2 FA59 E2E4
B20D CD21 B20A CD21 5958 E2CC 59E2 C5

Listing:

    PR_BMP  MACRO BMP, SZBMP, ZX, ZY
            LOCAL LOOP_Y, LOOP_Y2, LOOP_X, LOOP_X2, X_POS, X_NEG
B4 02   MOV  AH, 2          ; DOS display char function 
84 FF   TEST ZY, ZY         ; is Y scale negative?
79 06   JNS  LOOP_Y         ; if positive, start Y LOOP
FD      STD                 ; direction flag start from end
03 F1   ADD  BMP, CX        ; advance input byte array to end
4E      DEC  BMP            ; zero adjust index
F6 DF   NEG  ZY             ; make counter positive
     LOOP_Y:    
51      PUSH CX             ; save outer byte loop counter
8A CF   MOV  CL, ZY         ; set up repeat counter (Y scale factor)
AC      LODSB               ; load byte into AL
     LOOP_Y2:
50      PUSH AX             ; save original AL
51      PUSH CX             ; save outer loop
B1 08   MOV  CL, 8          ; loop 8 bits
8A F3   MOV  DH, ZX         ; DH is positive X scale used as counter
84 F6   TEST ZX, ZX         ; is X scale negative?
79 02   JNS  LOOP_X         ; if so, make counter positive
F6 DE   NEG  DH             ; compliment X counter 
    LOOP_X:
51      PUSH CX             ; save bit counter
8A CE   MOV  CL, DH         ; set repeat counter (X scale factor)
B2 23   MOV  DL, '#'        ; by default, display a #
84 DB   TEST ZX, ZX         ; is X scale negative?
79 04   JNS  X_POS          ; if so, rotate left 1 bit
D0 C8   ROR  AL, 1          ; else rotate right LSB into CF
EB 02   JMP  X_NEG          ; jump to examine CF
    X_POS:
D0 C0   ROL  AL, 1          ; rotate left MSB into CF
    X_NEG:
72 02   JC   LOOP_X2        ; is a 1?   
B2 20   MOV  DL, ' '        ; if not, display a space
    LOOP_X2:    
50      PUSH AX             ; save AL (since silly DOS overwrites it)
CD 21   INT  21H            ; display char
58      POP  AX             ; restore AL
E2 FA   LOOP LOOP_X2        ; loop repeat counter
59      POP  CX             ; restore bit counter
E2 E4   LOOP LOOP_X         ; loop bit counter
B2 0D   MOV  DL, 0DH        ; display CRLF
CD 21   INT  21H
B2 0A   MOV  DL, 0AH
CD 21   INT  21H
59      POP  CX             ; restore outer loop
58      POP  AX             ; restore original AL
E2 CC   LOOP LOOP_Y2        ; loop row display
59      POP  CX             ; restore byte counter
E2 C5   LOOP LOOP_Y         ; loop byte counter
    ENDM

This turned out to be more of a doozy in ASM than I originally thought. Multiple concurrent loops and lots of if/else branching can certainly give you headaches.

This is implemented as a MACRO since it allows function-like parameter passing for testing.

Output

Here is a test program for DOS that prompts for the X and Y scaling factor and draws to the screen. Note, scaling the dragon too much will scroll past the top since default DOS window is only 24 rows.

enter image description here

And here's our little dragon (duck):

enter image description here

Try it Online!

You can test in a DOS VM using DOSBox or VirtualConsoles.com with the following steps:

  1. Download VCS.ZIP (contains all four executables)
  2. Go to https://virtualconsoles.com/online-emulators/DOS/
  3. Upload the ZIP file you just downloaded, click Start
  4. Type PLANE, KEY, TANK or DRAGON.
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 146 132 bytes

param($x,$y,$b)&($t={$b|%({$r+=,$_*$y},{$r=,$_*-$y+$r})[$y-lt0]
$r-ne$e})|%{$b=,$_*8|%{''+' X'[($_-shr$i++%8)%2]}
$y=-$x;-join(&$t)}

Try it online!

Unrolled:

param($x,$y,$bitmap)

$transform={
    $bitmap|%({$r+=,$_*$y},{$r=,$_*-$y+$r})[$y-lt0]
    $r -ne $empty
}

&$transform|%{                                  # $bitmap is an array of lines
    $bitmap=,$_*8|%{''+' X'[($_-shr$i++%8)%2]}  # $bitmap is an array of line pixels now
    $y=-$x                                      # reuse $y
    -join(&$transform)
}
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 105 bytes

($_,$h,$v)=@F;say for map{$_=reverse if$h<0;y/0/ /;s/./$&x abs$h/eg;($_)x abs$v}$v<0?reverse/\d+/g:/\d+/g

TIO

If input must be hex

126 bytes

\$\endgroup\$
1
\$\begingroup\$

Jelly, 21 bytes

⁴²+BḊ€⁸m€Ṡ}¥xA}ZʋƒYo⁶

Try it online!

Assumes there is at most one command-line argument.

\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Extended), 23 bytesSBCS

dzaima's method

Anonymous tacit prefix function. Takes \$(v,h,B)\$ as argument, where \$v\$ is the vertical scaling factor, \$h\$ is the horizontal scaling factor, and \$B\$ is the byte array as an 8-column packed bit-Boolean matrix (this is the normal and most compact way to represent raw bytes in APL). Requires ⎕IO←0 (zero based indexing).

' x'⊇⍨∘⊃{⊖⍣(>⍺)⍉⍵/⍨|⍺}/

Try it online!

{}/ reduce right-to-left using the following anonymous lambda:

|⍺ the magnitude of the left argument (the scaling factor)

⍵/⍨ use that to replicate the right argument horizontally

 transpose

⊖⍣() flip if:

  >⍺ the scaling factor is less than zero

 disclose (since reduction enclosed to reduce tensor rank from 1 to 0)

' x'⊇⍨ select elements from the string " x" using that matrix

\$\endgroup\$
1
\$\begingroup\$

Ruby, 89 bytes

->b,w,h{y=h>0?-1:0;(b*h.abs).map{y+=1;[*1+8*w..0,*0...8*w].map{|x|' X'[b[y/h][x/w]]}*''}}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

T-SQL, 216 bytes

Before executing this MS-SQL Studio Management, press CRTL-t to show data as text. The height cannot be adjusted to exceed the number of elements in the input.

Because of the horrible implementation of STRING_AGG, the height variable will only work in MSSM. MS should have made a third optional parameter to include the order of the elements being concatenated.

The online version can only support adjustment of the width. Height will result in a funky result with multiple stacking shapes.

USE master
DECLARE @ table(v int,i int identity)
INSERT @ values
(0x06),(0x0F),(0xF3),(0xFE),(0x0E),(0x04),
(0x04),(0x1E),(0x3F),(0x7F),(0xE3),(0xC3),
(0xC3),(0xC7),(0xFF),(0x3C),(0x08),(0x8F),
(0xE1),(0x3F)
-- @  = width
-- @h = height
DECLARE @s INT=1,@h INT=1

SELECT iif(@s>0,reverse(x),x)FROM(SELECT
string_agg(replicate(iif(v&n=0,' ','X'),abs(@s)),'')x,i,j
FROM(values(1),(2),(4),(8),(16),(32),(64),(128))x(n)
,@,(SELECT top(abs(@h))i j FROM @)g GROUP BY i,j)f
ORDER BY i*@h

This script will not show the correct shapes in the online version, so I made some minor adjustments to compensate. Try it online

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.