0

I want to write a program to print following pattern for text "NUOSPIN":

* * *  *   *  * * *  * * *  * * *  *  * * *
*   *  *   *  *   *  *      * * *  *  *   *
*   *  *   *  *   *  * * *  *      *  *   *
*   *  * * *  * * *  * * *  *      *  *   *

I am working on Javascript(NodeJS). I have to output it on console. Implementation in any language will work for me including java, C, javascript. I know I can do this by using prebuilt npm packages(like asciify), but I want to do this raw. What should be my approach for this? Do I have to write code for each character's pattern separately? And then print them by running logic of each character's pattern one by one?

Rahul Rajput
  • 284
  • 1
  • 3
  • 15
  • *"I want to do this raw"* - **1)** Why, when there are pre-existing libraries that do that? **2)** Look at the source code of those libraries to get an idea how to do this. – Tomalak Oct 11 '16 at 18:01
  • @Tomalak well, copying from the source code doesn't teach you anything, it just shows you the plain answer, and 2. because npm sometimes just isn't great. – DaCuteRaccoon Nov 18 '22 at 02:50

3 Answers3

3

You could use a bitmap and assemble the dots to the ASCII style, you want.

function getWord(s) {
    var ascii = [],
        font = {
            N: [7, 5, 5, 5],
            U: [5, 5, 5, 7],
            O: [7, 5, 5, 7],
            S: [7, 4, 7, 7],
            P: [7, 7, 4, 4],
            I: [1, 1, 1, 1]
        };

    s.split('').forEach(function (c) {
        var size =( font[c].reduce(function (r, a) {
            return r | a;
        }, 0)).toString(2).length;

        font[c].forEach(function (a, i) {
            var temp = a.toString(2).split('').map(function (c) {
                return +c ? '*' : ' ';
            });
            while (temp.length < size) {
                temp.unshift(' ');
            }
            ascii[i] = ascii[i] || [];
            ascii[i].push(temp.join(''));
        });
    });
    return ascii.map(function (a) {
        return a.join(' ');
    }).join('\n')
}

document.getElementById('tt').innerHTML = getWord('NUOSPIN');
<pre id="tt"></pre>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Or you can also do something a bit more sophisticated but still simple enough for beginners like [Image to ASCII art conversion](http://stackoverflow.com/q/32987103/2521214) – Spektre Mar 16 '17 at 09:52
2

Usually you have a large map somewhere that maps a character to its appearance, especially for such custom-made fonts. You'd then go over the input string, look up the picture for a character, put it in a buffer, and print that buffer once you're done.

You can also do it by printing directly to the screen by doing this line by line, look up each character's first line, print that, look up each character's second line, etc.

If you want to get really creative you could also draw an image in memory of the text with a specific font, and then draw that image with asterisks and spaces to the console.

Joey
  • 344,408
  • 85
  • 689
  • 683
1

Each line of text would have to be written several times. First the upper rows of the ASCII art glyphs.

#!/usr/bin/python

import sys

# Each item in `font` is a list of `font_height` strings.
# The glyphs are not limited to asterisks and spaces.
font_height = 7
margin_left = 1
margin_right = 1
font = {
    'N': [
        "*   *",
        "*   *",
        "**  *",
        "* * *",
        "*  **",
        "*   *",
        "*   *",
    ],
    'U': [
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        " *** ",
    ],
    'O': [
        " *** ",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        "*   *",
        " *** ",
    ],
    'S': [
        " *** ",
        "*   *",
        "*    ",
        " *** ",
        "    *",
        "*   *",
        " *** ",
    ],
    'P': [
        "**** ",
        "*   *",
        "*   *",
        "**** ",
        "*    ",
        "*    ",
        "*    ",
    ],
    'I': [
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
        "  *  ",
    ],
}

def printline(text):
    for row in range(font_height):
        for ch in text:
            sys.stdout.write(' ' * margin_left)
            sys.stdout.write(font[ch][row])
            sys.stdout.write(' ' * margin_right)
        sys.stdout.write('\n')

printline('NUOSPIN')

Gives me

sshoskar@oskog97:~$ ./test.py 
 *   *  *   *   ***    ***   ****     *    *   * 
 *   *  *   *  *   *  *   *  *   *    *    *   * 
 **  *  *   *  *   *  *      *   *    *    **  * 
 * * *  *   *  *   *   ***   ****     *    * * * 
 *  **  *   *  *   *      *  *        *    *  ** 
 *   *  *   *  *   *  *   *  *        *    *   * 
 *   *   ***    ***    ***   *        *    *   * 
Oskar Skog
  • 332
  • 5
  • 16