More Pixel Art: Run Length Encoding

by sonone, May 14, 2021, 10:04 PM

I have made a new pixel art function using Run Length Encoding (I more used the Run Length without the encoding :P). This is the source code: Click for reveal hidden code

The first input is the width of your drawing. The second is the string coding your picture (explained below). The third is either true or false, giving you the option to show a palette. This is optional, with the default being no palette. The last inputs are your color palette, the first being a, the second b, and so on (you can go as far as z).

For the actual coding, you put the number of pixels you want, followed by the letter corresponding to a color on your palette. Like the previous versions, a dash (-) is an invisible mark.
Example
[asy]
//Made by sonone
int parse_digit(string n) {
	if (n == "0")
    	return 0;
    else if (n == "1")
    	return 1;
    else if (n == "2")
    	return 2;
    else if (n == "3")
    	return 3;
    else if (n == "4")
    	return 4;
    else if (n == "5")
    	return 5;
    else if (n == "6")
    	return 6;
    else if (n == "7")
    	return 7;
    else if (n == "8")
    	return 8;
    else if (n == "9")
    	return 9;
    else
    	return -1;
} 
 
bool is_int(string idk) {
	bool yes = true;
    string n;
    
    for (int i = 0; i < length(idk); ++i) {
    	n = substr(idk, i, 1);
    	if (parse_digit(n) == -1) {
        	yes = false;
            break;
        }
    }
    
    return yes;
}

int parse_int(string n) {
	if (!is_int(n)) {
    	return -1;
    }
    else {
    	int result = 0;
    	for (int i = 0; i < length(n); ++i) {
        	result += parse_digit(substr(n,i,1))*10^(length(n) - i - 1);
        }
        return result;
    }
}
void draw_RLE_string(int width, string px, bool palette = false...pen[] colors) { // converts a rle string into a pixel drawing with specified demensions and color palette
    string letters = "abcdefghijklmnopqrstuvwxyz-";
    int x = 0, y = 0;
    string bit;
    int curr_int;
    int num_digits = 0;
    string color_letter = "a";
    
    for (int i = 0; i < length(px); ++i) {
    	bit = substr(px, i, 1);
        
        if (is_int(bit) && num_digits == 0) {
        	curr_int = parse_int(substr(px, i, 1));
            ++num_digits;
            continue;;
        }
        else if (is_int(bit)) {
        	curr_int = parse_int(substr(px, i - num_digits, num_digits + 1));
            ++num_digits;
            continue;
        }
        else if (find(letters,bit) == -1) {
        	continue;
        }
        else {
        	num_digits = 0;
            color_letter = substr(px, i, 1);
        }
        
        for (int i = 0; i < curr_int; ++i) {
        	if (x >= width) {
            	x = 0;
                y -= 1;
            }
            
            if (color_letter == "-")
            	fill(shift(x,y)*unitsquare, invisible);
            else
        		fill(shift(x,y)*unitsquare, colors[find(letters, color_letter)]);
            
          	++x;
        }
    }
    if (palette) {
        for (int i = 0; i < colors.length; ++i) {
            label(scale(.7)*substr(letters,i,1), (i,2),2*N);
            filldraw(shift(i-.5,1.5)*scale(.8)*unitsquare, colors[i],linewidth(.3));
        }
    }
}

draw_RLE_string(12,"
3-5a6-9a3-3d2b1d1b4-
1d1b1d3b1d3b2-1d1b2d
3b1d3b1-2d4b4d4-7b4-
2c1a3c5-3c1a2c1a3c1-
4c4a4c2b1c1a1e2a1e1a
1c5b6a5b8a2b2-3a2-3a
3-3c4-3c1-4c4-4c
", springgreen,olive+brown,brown,black, yellow);
[/asy]
This post has been edited 3 times. Last edited by sonone, May 14, 2021, 11:28 PM

Comment

2 Comments

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
[asy]
fill((0,0)--(0,5)--(7,5)--(7,0)--cycle,cyan);
fill((2,0)--(3,0)--(3,2)--(7,2)--(7,3)--(3,3)--(3,5)--(2,5)--(2,3)--(0,3)--(0,2)--(2,2)--cycle,yellow);
[/asy]

by smileapple, Jul 29, 2021, 1:44 AM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
[asy]
draw((0,0)--(0,5)--(7,5)--(7,0)--cycle);
fill((2,0)--(3,0)--(3,2)--(7,2)--(7,3)--(3,3)--(3,5)--(2,5)--(2,3)--(0,3)--(0,2)--(2,2)--cycle,blue);
[/asy]

by smileapple, Jul 29, 2021, 1:45 AM

Old material is mostly Asymptote, new material is calculator programming

avatar

sonone
Archives
+ April 2023
+ August 2022
+ April 2021
+ August 2020
Shouts
Submit
  • I still exist as well.

    by G.G.Otto, Aug 11, 2023, 2:44 AM

  • hello I'm still here lol

    by player01, Aug 6, 2022, 6:24 PM

  • [REVIVAL] I will start posting more calculator relating posts very soon. Even though school has been busy, I have been programming my calculators a decent amount, so I have a lot to share...

    by sonone, Feb 18, 2022, 10:29 PM

  • wow its been like 2.5 years since geo class

    by pieMax2713, Feb 4, 2022, 8:38 PM

  • @violin21, I've been very busy with school lately and haven't been able to add another lesson. I will when i get a free moment

    by sonone, Aug 19, 2021, 12:45 AM

  • ORZ CODER

    by samrocksnature, Aug 9, 2021, 9:57 PM

  • Could you make more Asymptote lessons on your "How to do Asymptote" blog?

    by violin21, Aug 9, 2021, 7:26 PM

  • You can take it, just C&P the CSS into your CSS area

    by sonone, Apr 17, 2021, 10:08 PM

  • how can we take the CSS if we have permission to not take it?

    by GoogleNebula, Apr 17, 2021, 5:22 PM

  • That is awesome!

    by sonone, Apr 15, 2021, 10:09 PM

  • I modified your dodecahedron and got:
    [asy]
    import three;
    import solids;
    size(300);
    currentprojection=orthographic(0,1.3,1.2);
    light(0,5,10);

    real phi=(sqrt(6)+1)/3;
    real g=(phi-1)/2;
    real s=1/2;
    real a=sqrt(1-phi*phi/4-g*g)+phi/2;

    triple[] d;
    d[0]=(phi

    by Andrew2019, Mar 26, 2021, 12:15 AM

  • Not too many, just changing the color here and there. I really like your CSS!

    by sonone, Feb 2, 2021, 10:35 AM

  • Nice!

    I see you're making changes to the CSS. :)

    by G.G.Otto, Feb 1, 2021, 9:26 PM

  • I'm learning Java now!

    by sonone, Feb 1, 2021, 5:56 PM

  • And I took part of it from CaptainFlint and then added a ton of modifications. ;)

    by G.G.Otto, Dec 1, 2020, 8:56 AM

98 shouts
Tags
About Owner
  • Posts: 2106
  • Joined: Aug 20, 2016
Blog Stats
  • Blog created: Mar 28, 2020
  • Total entries: 61
  • Total visits: 4852
  • Total comments: 146
Search Blog
a