Create Devanagari ASCII Art: C++, JS, Nepali

  • JavaScript
  • Thread starter shivajikobardan
  • Start date
  • Tags
    Art
In summary: You can try it out like so:./a.outIn summary, Nepali is based on Devanagari Script and has 33 letters, each with a variety of forms.
  • #1
shivajikobardan
674
54
TL;DR Summary
Devanagari ASCII Art?
This is an example of Nepali sentence. Nepali is based on Devanagari Script.

यो फोरम मलाई एकदम मन पर्छ|

https://nepalilanguage.org/alphabet/#:~:text=Each of the following 33,/, फ /pʰa/,
These are the vowels and consonants in Nepali.

A Nepali consonant can take these many forms. "Ka" is first letter of Nepali consonant.
Q2Gw_Rtv6zvzNRVbINyyDklEVbOajYmgPkLJmK_WlESqJgtupY.png
Expected Input

यो फोरम मलाई एकदम मन पर्छ|

And any character like a,b,c,.,/,*,+,- based on which the ASCII art will be generated.

Expected Output

Every letter should be scaled and drawn with the given character, say for example "*". Like below.

Plan 1:

1) Accept Nepali characters as input. And parse each character.

example यो फोरम मलाई एकदम मन पर्छ|

2) Get the shape of each character.

3) Scale the shape.

4) Redraw the shape using "*" or anything given by user as input.

Plan 2:

1) Take Nepali character as input

2) Convert to Unicode.

3) Process the Unicode.

4) Display Nepali as output.

I'm wondering how could I do this? Language doesn't matter but I'd prefer, C, C++ or Javascript.
bYc2U8GQfaCDAJ2gdh5jt3PwfJ2GgLu9ouUSTPbF95G6zhBIdM.png
 
Technology news on Phys.org
  • #2
Your plan 2 is the better one, IMO, but you'll need to figure out how to use the appropriate input and output functions to work with Unicode, and specifically for the code pages for Nepali characters.

If you decide that you want to go with plan 1, I would advise you to omit the part about scaling the characters. Instead, decide on a certain size grid for the characters and go with that. Also, you should use graph paper instead of lined paper to lay out the characters.

Having said all that, I would also advise starting with a simpler project -- writing a program to display the characters of the Roman alphabet. There are 26 uppercase letters and the same number of lowercase letters, with none of the complexities of the Nepali characters.
 
  • #3
Mark44 said:
Having said all that, I would also advise starting with a simpler project -- writing a program to display the characters of the Roman alphabet. There are 26 uppercase letters and the same number of lowercase letters, with none of the complexities of the Nepali characters.
I'll first make ASCII art generator for a-z then only I'll go to Nepali.
 
  • #4
Mark44 said:
you should use graph paper instead of lined paper to lay out the characters.
Or a text editor, using X's (or whatever) and blank spaces in a monospace font. Easier to tweak your design than by erasing stuff on graph paper. When you're done, save the design in a text file that your program reads when it starts up.
 
  • #5
jtbell said:
Or a text editor, using X's (or whatever) and blank spaces in a monospace font. Easier to tweak your design than by erasing stuff on graph paper. When you're done, save the design in a text file that your program reads when it starts up.
Can you share an example?
 
  • #6
An example of what, specifically? How to read the text file?
 
  • #7
OK, here's a simple example of what I wrote about. First I created a text file named letter.txt, with 8 rows of 10 characters, depicting a crude letter 'A'. Each row does indeed have 10 characters, although you can't see the blank spaces at the ends of most of the lines.
Code:
    XX    
   XXXX   
  XX  XX  
  XX  XX  
 XXXXXXXX 
 XX    XX 
XX      XX
XX      XX
The program below reads and displays the contents of this file, using techniques that should be in most any reasonably modern C++ textbook. Some people would choose to use a 2-dimensional array of char to represent the letter; I prefer to use a vector of strings.
C++:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

// Reads an 8-line text file which happens to contain an ASCII-art letter 'A', 
// and displays it.  Uses traditional for-loops to read and write the rows of 
// 'X's and ' 's.

int main ()
{
    const int numRows = 8;

    std::vector<std::string> letter (numRows);

    std::ifstream letterFile;
    letterFile.open ("letter.txt");
    if (letterFile.good())
    {
        for (int rowNum = 0; rowNum < numRows; ++rowNum)
        {
            std::getline (letterFile, letter[rowNum]);
        }
        letterFile.close ();

        for (int rowNum = 0; rowNum < numRows; ++rowNum)
        {
            std::cout << letter[rowNum] << std::endl;
        }
    }
    else
    {
        std::cout << "Can't open letter.txt!" << std::endl;
    }

    return 0;
}
I developed and tested this code using the g++ compiler in Ubuntu 22.04 Linux, in a virtual machine under Parallels Desktop on MacOS.
 
  • #8
Here's a version of the program that uses "ranged for-loops" instead of traditional for-loops. These became part of C++ in 2011. I don't know how common they are in current textbooks.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

// Reads an 8-line text file which happens to contain an ASCII-art letter 'A', 
// and displays it.  Uses ranged for-loops to read and write the rows of 
// 'X's and ' 's.

int main ()
{
    const int numRows = 8;

    std::vector<std::string> letter (numRows);

    std::ifstream letterFile;
    letterFile.open ("letter.txt");
    if (letterFile.good())
    {
        // note 'row' needs to be a reference ('&') so we can modify its 
        // contents inside the vector.  This is similar to passing an argument 
        // to a function by reference.
        for (std::string& row : letter)
        {
            std::getline (letterFile, row);
        }
        letterFile.close ();

        // 'row' is not a reference here, so it makes a copy of each element
        // of the vector. For efficiency  we could make this a reference too,
        // and make it 'const' to emphasize that we don't intend to modify
        // the contents (unlike the preceding loop):
        // for (const std::string& row: letter)
        for (std::string row : letter)        
        {
            std::cout << row << std::endl;
        }
    }
    else
    {
        std::cout << "Can't open letter.txt!" << std::endl;
    }

    return 0;
}
 
  • Informative
Likes Mark44
  • #9
jtbell said:
OK, here's a simple example of what I wrote about. First I created a text file named letter.txt, with 8 rows of 10 characters, depicting a crude letter 'A'. Each row does indeed have 10 characters, although you can't see the blank spaces at the ends of most of the lines.
Code:
    XX   
   XXXX  
  XX  XX 
  XX  XX 
 XXXXXXXX
 XX    XX
XX      XX
XX      XX
The program below reads and displays the contents of this file, using techniques that should be in most any reasonably modern C++ textbook. Some people would choose to use a 2-dimensional array of char to represent the letter; I prefer to use a vector of strings.
C++:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

// Reads an 8-line text file which happens to contain an ASCII-art letter 'A',
// and displays it.  Uses traditional for-loops to read and write the rows of
// 'X's and ' 's.

int main ()
{
    const int numRows = 8;

    std::vector<std::string> letter (numRows);

    std::ifstream letterFile;
    letterFile.open ("letter.txt");
    if (letterFile.good())
    {
        for (int rowNum = 0; rowNum < numRows; ++rowNum)
        {
            std::getline (letterFile, letter[rowNum]);
        }
        letterFile.close ();

        for (int rowNum = 0; rowNum < numRows; ++rowNum)
        {
            std::cout << letter[rowNum] << std::endl;
        }
    }
    else
    {
        std::cout << "Can't open letter.txt!" << std::endl;
    }

    return 0;
}
I developed and tested this code using the g++ compiler in Ubuntu 22.04 Linux, in a virtual machine under Parallels Desktop on MacOS.
I know it's a bad practice but I really hate not writing using namespace std; I think it makes the code hell lot clearer and clutter free. I understand the conflict that can be cause by namespaces. Although I forgot them clearly atm.
 
  • #10
shivajikobardan said:
I know it's a bad practice but I really hate not writing using namespace std; I think it makes the code hell lot clearer and clutter free.
It's bad practice for a reason. "using namespace std:" provides access to a ton of classes and whatnot that could conceivably cause unintended collisions between identifiers you're using in your code and others that belong to the std namespace.

I did a search with the string ' "using namespace std" considered bad'. Here's a quote from one of the many pages I found - https://fluentprogrammer.com/dont-use-using-namespace-std/
It is okay to import the whole std library in toy programs but in production-grade code, It is bad. using namespace std; makes every symbol declared in the namespace std accessible without the namespace qualifier. Now, let’s say that you upgrade to a newer version of C++ and more new std namespace symbols are injected into your program which you have no idea about. You may already have those symbols used in your program. Now the compiler will have a hard time figuring out whether the symbol declared belongs to your own implementation or from the namespace you imported without any idea. Some compilers throw errors. If you are unlucky, the compiler chose the wrong implementation and compile it which for sure leads to run time crashes.
 
Last edited:
  • Informative
Likes berkeman
  • #11
As a less obtrusive or laborious alternative to slapping 'std::' on all those identifiers, every time you use them, you can do it once per identifier, in a 'using' declaration.

using std::string;
using std::cout;
using std::endl;

When I do this, I place them at the beginning of the program, right after the '#include' directives.

[added] Aha! I've discovered that since C++2017, you can combine multiple declarations in a single 'using' statement.

using std::string, std::cout, std::endl;
 
Last edited:
  • Like
Likes shivajikobardan and Vanadium 50

1. How do I create Devanagari ASCII art using C++?

To create Devanagari ASCII art using C++, you can use the ASCII codes for each Devanagari character and print them out in the desired pattern. You can also use libraries such as ncurses or conio to manipulate the output on the terminal.

2. Can I create Devanagari ASCII art using JavaScript?

Yes, you can create Devanagari ASCII art using JavaScript. You can use the String.fromCharCode() method to convert ASCII codes to characters and print them out in the desired pattern. You can also use HTML canvas to draw the ASCII characters in the form of art.

3. Is it possible to create Devanagari ASCII art in Nepali language?

Yes, it is possible to create Devanagari ASCII art in Nepali language. You can use the same methods as mentioned above, but instead of ASCII codes for Hindi characters, you can use ASCII codes for Nepali characters.

4. Are there any online tools available for creating Devanagari ASCII art?

Yes, there are several online tools available that can help you create Devanagari ASCII art. You can simply search for "Devanagari ASCII art generator" and you will find many options to choose from. These tools usually allow you to input text in Devanagari and generate ASCII art based on it.

5. Can I use Devanagari ASCII art in my projects?

Yes, you can use Devanagari ASCII art in your projects. It can add a unique visual element to your work and can be used in various applications such as websites, games, or even in text-based interfaces. Just make sure to follow the proper usage guidelines and give credit to the original creator if you are using ASCII art created by someone else.

Similar threads

  • Programming and Computer Science
Replies
4
Views
15K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Computing and Technology
Replies
2
Views
1K
Replies
10
Views
2K
  • Special and General Relativity
Replies
7
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top