41
\$\begingroup\$

Task description:

Write a program as short as possible to draw a radial gradient in ASCII art. The size of the output grid is predefined. The center point and the characters to be used for the gradient are provided as input to the program.

The gradient will be 70×25 character cells in size with the following specifications

  • The upper-left corner of the grid has the coordinates (0, 0).
  • The gradient, consisting of the characters provided, is mapped into 35 length units. A length unit is one character tall and two characters wide (since monospaced fonts usually have characters twice as high as they are wide).
  • Within those 35 LU the current distance from the center point is multiplied by number of gradient characters/35 to get the index of the character to draw at that point. Fractional numbers for the index are rounded towards zero here. The length units are to be left as a real number. (Of course, if the results are the same, the implementation doesn't matter.)
  • Beyond those 35 LU only the last gradient character appears. However, since the last gradient »band« already starts within the 35 LU, the last character starts appearing short of 35 LU already.

Input:

The input is given on standard input and consists of three lines, each terminated by a line break:

  • The x coordinate of the gradient center point
  • The y coordinate of the gradient center point
  • The characters to use for drawing the gradient. Those may include spaces.

Output:

Output is the gradient as defined by the rules above on standard output. The standard error stream is ignored. Each line of the gradient is terminated by a line break. No other characters except those defined by the input may occur.

Sample input 1:

58
14
 .:;+=xX$&

Sample output 1:

&&$$$$$$$$XXXXXXXXxxxxxxxxx===========++++++++++++++++++++++++++++++++
&$$$$$$$$XXXXXXXXxxxxxxxxx=========+++++++++++++;;;;;;;;;;;;;;;;;;;;;+
$$$$$$$$XXXXXXXXxxxxxxxx=========+++++++++++;;;;;;;;;;;;;;;;;;;;;;;;;;
$$$$$$$XXXXXXXXxxxxxxxx========++++++++++;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$$$$$$XXXXXXXXxxxxxxxx========+++++++++;;;;;;;;;;;;;:::::::::::::;;;;;
$$$$$XXXXXXXXxxxxxxxx=======+++++++++;;;;;;;;;;;:::::::::::::::::::::;
$$$$$XXXXXXXxxxxxxxx=======+++++++++;;;;;;;;;:::::::::::::::::::::::::
$$$$XXXXXXXXxxxxxxx=======++++++++;;;;;;;;;:::::::::::::::::::::::::::
$$$$XXXXXXXxxxxxxx========+++++++;;;;;;;;::::::::::...............::::
$$$XXXXXXXXxxxxxxx=======+++++++;;;;;;;;:::::::::...................::
$$$XXXXXXXxxxxxxx=======++++++++;;;;;;;::::::::.......................
$$$XXXXXXXxxxxxxx=======+++++++;;;;;;;::::::::.........       ........
$$$XXXXXXXxxxxxxx=======+++++++;;;;;;;:::::::........           ......
$$$XXXXXXXxxxxxxx=======+++++++;;;;;;;:::::::.......             .....
$$$XXXXXXXxxxxxxx=======+++++++;;;;;;;:::::::.......             .....
$$$XXXXXXXxxxxxxx=======+++++++;;;;;;;:::::::.......             .....
$$$XXXXXXXxxxxxxx=======+++++++;;;;;;;:::::::........           ......
$$$XXXXXXXxxxxxxx=======+++++++;;;;;;;::::::::.........       ........
$$$XXXXXXXxxxxxxx=======++++++++;;;;;;;::::::::.......................
$$$XXXXXXXXxxxxxxx=======+++++++;;;;;;;;:::::::::...................::
$$$$XXXXXXXxxxxxxx========+++++++;;;;;;;;::::::::::...............::::
$$$$XXXXXXXXxxxxxxx=======++++++++;;;;;;;;;:::::::::::::::::::::::::::
$$$$$XXXXXXXxxxxxxxx=======+++++++++;;;;;;;;;:::::::::::::::::::::::::
$$$$$XXXXXXXXxxxxxxxx=======+++++++++;;;;;;;;;;;:::::::::::::::::::::;
$$$$$$XXXXXXXXxxxxxxxx========+++++++++;;;;;;;;;;;;;:::::::::::::;;;;;

Sample input 2:

0
0
X.X.X.X

Sample output 2:

XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
XXXXXXXX............XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
XXXXXX.............XXXXXXXXXX...........XXXXXXXXXX..........XXXXXXXXXX
..................XXXXXXXXXXX..........XXXXXXXXXX...........XXXXXXXXXX
................XXXXXXXXXXXX...........XXXXXXXXXX..........XXXXXXXXXXX
...............XXXXXXXXXXXX...........XXXXXXXXXX...........XXXXXXXXXXX
............XXXXXXXXXXXXXX...........XXXXXXXXXXX..........XXXXXXXXXXXX
.........XXXXXXXXXXXXXXX............XXXXXXXXXXX...........XXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX............XXXXXXXXXXX...........XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXX.............XXXXXXXXXXX...........XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX..............XXXXXXXXXXXX...........XXXXXXXXXXXXXXX
XXXXXXXXXXXXXXX................XXXXXXXXXXXX............XXXXXXXXXXXXXXX
XXXXXXXXXXX..................XXXXXXXXXXXXX............XXXXXXXXXXXXXXXX
...........................XXXXXXXXXXXXX............XXXXXXXXXXXXXXXXXX
........................XXXXXXXXXXXXXXX............XXXXXXXXXXXXXXXXXXX
......................XXXXXXXXXXXXXXX.............XXXXXXXXXXXXXXXXXXXX
..................XXXXXXXXXXXXXXXXX.............XXXXXXXXXXXXXXXXXXXXXX
.............XXXXXXXXXXXXXXXXXXXX..............XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...............XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXX...............XXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX.................XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX...................XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXX......................XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Sample input 3:

70
25
.:+#

Sample output 3:

######################################################++++++++++++++++
#################################################+++++++++++++++++++++
#############################################+++++++++++++++++++++++++
##########################################++++++++++++++++++++++++++++
#######################################+++++++++++++++++++++++++++++++
####################################++++++++++++++++++++++++++++++++++
##################################++++++++++++++++++++++++++++++++++++
################################++++++++++++++++++++++++++++++++++++++
##############################++++++++++++++++++++++++++++++++::::::::
#############################+++++++++++++++++++++++++++::::::::::::::
###########################+++++++++++++++++++++++++::::::::::::::::::
##########################++++++++++++++++++++++++::::::::::::::::::::
#########################++++++++++++++++++++++:::::::::::::::::::::::
########################+++++++++++++++++++++:::::::::::::::::::::::::
#######################++++++++++++++++++++:::::::::::::::::::::::::::
######################++++++++++++++++++++::::::::::::::::::::::::::::
#####################+++++++++++++++++++::::::::::::::::::::::::::::::
####################+++++++++++++++++++::::::::::::::::::::::::.......
####################++++++++++++++++++::::::::::::::::::::::..........
###################+++++++++++++++++++::::::::::::::::::::............
###################++++++++++++++++++:::::::::::::::::::..............
###################+++++++++++++++++:::::::::::::::::::...............
##################++++++++++++++++++::::::::::::::::::................
##################++++++++++++++++++:::::::::::::::::.................
##################++++++++++++++++++:::::::::::::::::.................

Sample input 4

59
1
 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<>|,.-#+!$%&/()=?*'_:;

Sample output 4

!+#-,|><87654210ZYWVUTRQPONLKJIHFEDCBzyxwutsrqonmlkjhgfedcbbbcdefghjkl
!+#-,|><87654210ZYWVUTRQPONLKJIHFEDCAzyxwutsrqonmljihgfdcba abcdfghijl
!+#-,|><87654210ZYWVUTRQPONLKJIHFEDCBzyxwutsrqonmlkjhgfedcbbbcdefghjkl
!+#-,|><97654310ZYXVUTSQPONMKJIHGEDCBAyxwvutrqponmkjihgffeedeeffghijkm
$+#-.|><98654320ZYXWUTSRQONMLKIHGFEDBAzyxwutsrqponmlkjihhggggghhijklmn
$!#-.,|<987643210YXWVUSRQPONLKJIHGEDCBAzywvutsrqponmllkjjjiiijjjkllmno
$!+#.,|><87654210ZYXVUTSRQONMLKJHGFEDCBAzywvutsrrqponnmmlllllllmmnnopq
%!+#-.|><987543210YXWVUTRQPONMLJIHGFEDCBAzyxwvutsrrqppooonnnnnoooppqrr
%$!+-.,|><87654310ZYXWVTSRQPONMLJIHGFEDCBAzyxxwvuttssrrqqqqqqqqqrrsstt
&%!+#-.,><987643210ZYXVUTSRQPONMLKJIHGFEDCBAzyyxwvvuutttssssssstttuuvv
&%$!+#.,|><986543210ZYWVUTSRQPONMLKJIHGFEDDCBAzzyyxwwwvvvuuuuuvvvwwwxy
/&%$!#-.,|><976543210ZYXVUTSRQPONMLKKJIHGFEEDCBBAAzzyyyxxxxxxxxxyyyzzA
(/&%!+#-.,|><876543210ZYXWVUTSRQPONMLKJJIHGGFEEDCCBBBAAAzzzzzzzAAABBBC
)(/%$!+#-.,|><876543210ZYXWVUTSRQPPONMLKKJIIHGGFFEEDDDCCCCCCCCCCCDDDEE
=)(&%$!+#-.,|><986543210ZYYXWVUTSRQPPONMMLKKJIIHHGGGFFFEEEEEEEEEFFFGGG
?=)(&%$!+#-.,|><9876543210ZYXWVVUTSRRQPOONMMLLKJJJIIIHHHHHHHHHHHHHIIIJ
*?=)(/%$!+#-.,|><98765432210ZYXWWVUTSSRQQPOONNMMLLLKKKJJJJJJJJJJJKKKLL
'*?=)(/&%$!+#-.,|><98765432110ZYXXWVUUTSSRRQPPOOONNNMMMMMLLLLLMMMMMNNN
_'*?=)(/&%$!+#-.,|><988765432210ZYYXWWVUUTTSSRRQQQPPPOOOOOOOOOOOOOPPPQ
:_'*?=)(/&%$!+##-.,|><9877654332100ZYYXXWVVUUTTTSSSRRRRQQQQQQQQQRRRRSS
;;:_'*?=)(/&%$!+#-.,,|><98876554322100ZZYYXXWWVVVUUUTTTTTTTTTTTTTTTUUU
;;;:_'*?=)(/&&%$!+#-.,,|><9987665443321100ZZYYYXXXWWWWVVVVVVVVVVVWWWWX
;;;;;:_'*?=)(/&%$$!+#-..,|>><9887665544322211000ZZZYYYYYYYYYYYYYYYYYZZ
;;;;;;:_'*??=)(/&%%$!+##-.,,|><<99877665544333222111100000000000001111
;;;;;;;;:_'*?==)(/&&%$!++#--.,,|>><<9887766655544433333322222223333334

A week has passed. It is time to unveil the solution lengths from our university's contest:

167 – Python
189 – Haskell
203 – C
210 – VB.NET
219 – C

And our own solutions:

  91 – GolfScript
125 – Ruby
157 – PowerShell

\$\endgroup\$
13
  • 9
    \$\begingroup\$ I was thinking it would be cool to have a tag for ascii-art questions too \$\endgroup\$
    – gnibbler
    Commented Feb 3, 2011 at 22:24
  • \$\begingroup\$ I'm either having trouble with the concept of LU or you sample outputs are incorrect. From what I understood in each band there should be an even number of characters, yet in your samples this isn't true. Can you help me understand this? \$\endgroup\$
    – Champo
    Commented Feb 4, 2011 at 0:16
  • \$\begingroup\$ @Juan: What exactly do you mean? I suck at explaining and did so last year already, to be honest. The last band extends to the rest of the image. Otherwise, the second example makes it most clear, I think. Since the 35 LU are 70 characters wide, each band (of which there are 7) is 10 characters wide in the top line. Along the left edge each band is 5 characters tall (but only the first 5 bands fit into the height of the grid). \$\endgroup\$
    – Joey
    Commented Feb 4, 2011 at 0:22
  • \$\begingroup\$ @Joey What I mean is, in a given line, the width of a band should be even, since a band would be composed by n LU (each 2 characters wide). Thus we have 2*n characters per band per line. Is this right? \$\endgroup\$
    – Champo
    Commented Feb 4, 2011 at 0:29
  • \$\begingroup\$ @Juan: Hm, the rounding should be applied to the character index, not the length units. I'll try rephrasing in a way that's less ambiguous. Another point: The entire grid is to be taken as a character grid, not a SLU (square length unit :-)) grid. The LU thing is just to help making the difference between horizontal and vertical distances clearer. Doesn't seem to work very well. Originally I had an explicit formula in there, it was removed since it was deemed too easy. \$\endgroup\$
    – Joey
    Commented Feb 4, 2011 at 0:31

13 Answers 13

31
\$\begingroup\$

Brainfuck - 1286

This is one of my favorite creations yet. Includes a working (for some definitions of working) square root function.

>,>,<<<<<<<+>,[<+[<+>-],]<-[>>[>]>>>+<<<<[<]<-]>>[<+<+<+>>>-]<<<[>>>+<
<<-]>[>]>>>>>>>+++++[<+++++>-]<[>>+++++++[<++++++++++>-]<[>>+++++++[<+
+++++++++>-]<<[>->+<<-]>>[<<+>>-]<<<<<[>>>>->+<<<<<-]>>>>>[<<<<<+>>>>>
-]<[>+>+<<-]>[<+>-]>>+++++++[<<++++++++++>>-]>[-]>[-]+>[-]<<<<[>+>+<<-
]>[<+>-]<<[>>+<<-]+>>>[>-]>[<<<<->>[-]>>->]<+<<[>-[>-]>[<<<<->>[-]+>>-
>]<+<<-]>[-]>[-]<<<[-]<[-<+[+>+<]>+[<+>-]]++<[->-[>+>>]>[+[-<+>]>+>>]<
<<<<]>[-]>[-]>[<<<+>>>-]<+++++[<+++++>-]<<<<[>>>->+<<<<-]>>>>[<<<<+>>>
>-]<<<<<[>>>>->+<<<<<-]>>>>>[<<<<<+>>>>>-]<[>+>+<<-]>[<+>-]>>+++++[<<+
++++>>-]>[-]>[-]+>[-]<<<<[>+>+<<-]>[<+>-]<<[>>+<<-]+>>>[>-]>[<<<<->>[-
]>>->]<+<<[>-[>-]>[<<<<->>[-]+>>->]<+<<-]>[-]>[-]<<<[-]<[-<+[+>+<]>+[<
+>-]]<<[>>+>+<<<-]>>[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<[-]<[<<+>>-]<
[>+>+<<-]>[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>-]>-]<<[-]<[<<+>>-]<<>+>>+<<<[
[>>+>>+<<<<-]>>>>[<<<<+>>>>-]>[-]>[-]+>[-]<<<<[>+>+<<-]>>[<<+>>-]<<<[>
>>+<<<-]>>>[>-]>[<<<<+>>[-]>>->]<+<<[>-[>-]>[<<<<+>>[-]+>>->]<+<<-]>[-
]>[-]<<+<<[>>-<[-]<-<<[-]>>]>>[-<<<[>+>+<<-]+>[<+>>+<-]>+>]<<<<]>[<+>-
]<-<<<<<[<+>->>>>>>+<<<<<<]<[>+<-]>>>>>>[>>>+<<<-]>>>[<<[<+>>+<-]>[<+>
-]>-]<+++++++<[-]>[<+++++>-]<<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>[-]>[
<<<<<<<<<<<+>>>>>>>>>>>-]<<<<<<<<<<<[-<[>>+<<-]>[<+>-]<]<.>>>[[<<+>>-]
>]>>>>>-]++++++++++.[-]<-]

Output is a bit off due to rounding errors, but still recognizable. Floating point is beyond my current skill level. Unfortunately this will only work with 16 bit cells, which means it is going to be dog slow.

Output 1:

&$$$$$$$$XXXXXXXXxxxxxxxxxx========++++++++++++++++++++++;;;++++++++++
$$$$$$$$$XXXXXXxxxxxxxxxx========++++++++++++++;;;;;;;;;;;;;;;;;;;;;;;
$$$$$$$XXXXXXXXxxxxxxxx========++++++++++++;;;;;;;;;;;;;;;;;;;;;;;;;;;
$$$$$$$XXXXXXxxxxxxxxxx======++++++++++++;;;;;;;;;;;;;;;;:::;;;;;;;;;;
$$$$$XXXXXXXXxxxxxxxx========++++++++++;;;;;;;;;;:::::::::::::::::::;;
$$$$$XXXXXXxxxxxxxxxx======++++++++++;;;;;;;;:::::::::::::::::::::::::
$$$$$XXXXXXxxxxxxxx======++++++++++;;;;;;;;:::::::::::::::::::::::::::
$$$XXXXXXxxxxxxxxxx======++++++++;;;;;;;;::::::::::::::::...::::::::::
$$$XXXXXXxxxxxxxx========++++++++;;;;;;::::::::::::...............::::
$$$XXXXXXxxxxxxxx======++++++++;;;;;;;;::::::::::...................::
$$$XXXXXXxxxxxxxx======++++++++;;;;;;::::::::::..........   ..........
$$$XXXXXXxxxxxxxx======++++++++;;;;;;::::::::........           ......
$$$XXXXXXxxxxxxxx======++++++++;;;;;;::::::::......               ....
$$$XXXXXXxxxxxxxx======++++++++;;;;;;::::::::......               ....
$XXXXXXxxxxxxxx======++++++++;;;;;;::::::::......                   ..
$$$XXXXXXxxxxxxxx======++++++++;;;;;;::::::::......               ....
$$$XXXXXXxxxxxxxx======++++++++;;;;;;::::::::......               ....
$$$XXXXXXxxxxxxxx======++++++++;;;;;;::::::::........           ......
$$$XXXXXXxxxxxxxx======++++++++;;;;;;::::::::::..........   ..........
$$$XXXXXXxxxxxxxx======++++++++;;;;;;;;::::::::::...................::
$$$XXXXXXxxxxxxxx========++++++++;;;;;;::::::::::::...............::::
$$$XXXXXXxxxxxxxxxx======++++++++;;;;;;;;::::::::::::::::...::::::::::
$$$$$XXXXXXxxxxxxxx======++++++++++;;;;;;;;:::::::::::::::::::::::::::
$$$$$XXXXXXxxxxxxxxxx======++++++++++;;;;;;;;:::::::::::::::::::::::::
$$$$$XXXXXXXXxxxxxxxx========++++++++++;;;;;;;;;;:::::::::::::::::::;;

Output 2:

XXXXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXX
XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
XXXXXXXX............XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
XX................XXXXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
..................XXXXXXXXXX............XXXXXXXXXX..........XXXXXXXXXX
................XXXXXXXXXXXX..........XXXXXXXXXXXX..........XXXXXXXXXX
..............XXXXXXXXXXXX............XXXXXXXXXX..........XXXXXXXXXXXX
..........XXXXXXXXXXXXXXXX..........XXXXXXXXXXXX..........XXXXXXXXXXXX
..XXXXXXXXXXXXXXXXXXXXXX............XXXXXXXXXX............XXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXX............XXXXXXXXXXXX..........XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX..............XXXXXXXXXX............XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX................XXXXXXXXXXXX............XXXXXXXXXXXXXX
XXXXXXXXXXXX..................XXXXXXXXXXXX............XXXXXXXXXXXXXXXX
XX..........................XXXXXXXXXXXXXX..........XXXXXXXXXXXXXXXXXX
..........................XXXXXXXXXXXXXX............XXXXXXXXXXXXXXXXXX
......................XXXXXXXXXXXXXXXX............XXXXXXXXXXXXXXXXXXXX
..................XXXXXXXXXXXXXXXXXX..............XXXXXXXXXXXXXXXXXXXX
..............XXXXXXXXXXXXXXXXXXXX..............XXXXXXXXXXXXXXXXXXXXXX
..XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..............XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXX................XXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX..................XXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXX....................XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX......................XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
\$\endgroup\$
2
  • 3
    \$\begingroup\$ I'm torn between considering this awful and awesome :D. I guess an accurate square root could be done only with a lookup table, or with much more code \$\endgroup\$
    – Joey
    Commented Jan 23, 2013 at 7:13
  • \$\begingroup\$ Lookup wouldn't help. I can calculate the integer below the square root just fine. You need a new data representation to handle floating point, and I'm just not ready to make that jump. \$\endgroup\$
    – captncraig
    Commented Jan 23, 2013 at 15:53
12
\$\begingroup\$

Python - 141 chars

x=input();y=input();z=raw_input();w=len(z)
for i in range(-y,25-y):print"".join(z[min(w-1,int((i*i*4+j*j)**.5*w/70))]for j in range(-x,70-x))
\$\endgroup\$
11
\$\begingroup\$

Ruby 1.9, 116 114 108 101 characters

x,y,z=*$<;25.times{|r|70.times{|c|$><<z[[(c-x.to_i+2.i*(r-y.to_i)).abs/70.0*k=z=~/$/,k-1].min]};puts}
\$\endgroup\$
8
\$\begingroup\$

Delphi, 200 (and 185)

Since I like ascii-art here a Delphi version of this code golf:

uses Math;var G:string;X,Y,l,i,j:Int16;begin ReadLn(X,Y);ReadLn(G);l:=Length(G);for j:=-y to 24-y do for i:=-x to 70-x do if i=70-x then WriteLn else Write(g[Min(l,1+Trunc(l*sqrt(i*i/4+j*j)/35))])end.

Not very impressive character-wise, as I had to use the Math unit to link in the Min() function. Also, ReadLn() somehow doesn't read integers and strings in one call, so that's quite expensive too. The newline needs a lot of characters too. Also, Delphi needs quite a lot of whitespace around it's keywords. Not very proud of this one.

Anyway, the output of sample 4 gives me :

!+#-,|><87654210ZYWVUTRQPONLKJIHFEDCBzyxwutsrqonmlkjhgfedcbbbcdefghjkl
!+#-,|><87654210ZYWVUTRQPONLKJIHFEDCAzyxwutsrqonmljihgfdcba abcdfghijl
!+#-,|><87654210ZYWVUTRQPONLKJIHFEDCBzyxwutsrqonmlkjhgfedcbbbcdefghjkl
!+#-,|><97654310ZYXVUTSQPONMKJIHGEDCBAyxwvutrqponmkjihgffeedeeffghijkm
$+#-.|><98654320ZYXWUTSRQONMLKIHGFEDBAzyxwutsrqponmlkjihhggggghhijklmn
$!#-.,|<987643210YXWVUSRQPONLKJIHGEDCBAzywvutsrqponmllkjjjiiijjjkllmno
$!+#.,|><87654210ZYXVUTSRQONMLKJHGFEDCBAzywvutsrrqponnmmlllllllmmnnopq
%!+#-.|><987543210YXWVUTRQPONMLJIHGFEDCBAzyxwvutsrrqppooonnnnnoooppqrr
%$!+-.,|><87654310ZYXWVTSRQPONMLJIHGFEDCBAzyxxwvuttssrrqqqqqqqqqrrsstt
&%!+#-.,><987643210ZYXVUTSRQPONMLKJIHGFEDCBAzyyxwvvuutttssssssstttuuvv
&%$!+#.,|><986543210ZYWVUTSRQPONMLKJIHGFEDDCBAzzyyxwwwvvvuuuuuvvvwwwxy
/&%$!#-.,|><976543210ZYXVUTSRQPONMLKKJIHGFEEDCBBAAzzyyyxxxxxxxxxyyyzzA
(/&%!+#-.,|><876543210ZYXWVUTSRQPONMLKJJIHGGFEEDCCBBBAAAzzzzzzzAAABBBC
)(/%$!+#-.,|><876543210ZYXWVUTSRQPPONMLKKJIIHGGFFEEDDDCCCCCCCCCCCDDDEE
=)(&%$!+#-.,|><986543210ZYYXWVUTSRQPPONMMLKKJIIHHGGGFFFEEEEEEEEEFFFGGG
?=)(&%$!+#-.,|><9876543210ZYXWVVUTSRRQPOONMMLLKJJJIIIHHHHHHHHHHHHHIIIJ
*?=)(/%$!+#-.,|><98765432210ZYXWWVUTSSRQQPOONNMMLLLKKKJJJJJJJJJJJKKKLL
'*?=)(/&%$!+#-.,|><98765432110ZYXXWVUUTSSRRQPPOOONNNMMMMMLLLLLMMMMMNNN
_'*?=)(/&%$!+#-.,|><988765432210ZYYXWWVUUTTSSRRQQQPPPOOOOOOOOOOOOOPPPQ
:_'*?=)(/&%$!+##-.,|><9877654332100ZYYXXWVVUUTTTSSSRRRRQQQQQQQQQRRRRSS
;;:_'*?=)(/&%$!+#-.,,|><98876554322100ZZYYXXWWVVVUUUTTTTTTTTTTTTTTTUUU
;;;:_'*?=)(/&&%$!+#-.,,|><9987665443321100ZZYYYXXXWWWWVVVVVVVVVVVWWWWX
;;;;;:_'*?=)(/&%$$!+#-..,|>><9887665544322211000ZZZYYYYYYYYYYYYYYYYYZZ
;;;;;;:_'*??=)(/&%%$!+##-.,,|><<99877665544333222111100000000000001111
;;;;;;;;:_'*?==)(/&&%$!++#--.,,|>><<9887766655544433333322222223333334

If you would accept indented output, this version is a bit shorter by changing the newline into an indent that leads to a 80-character wrap (simulating a newline on standard 80x25 consoles) :

uses Math;var G:string;X,Y,l,i,j:Int16;begin ReadLn(X,Y);ReadLn(G);l:=Length(G);for j:=-y to 24-y do for i:=-x to 70-x do Write(g[Min(l,1+Trunc(l*sqrt(i*i/4+j*j)/35))]:11*Ord(i=-x))end.

(this saves 15 characters, for a total of 185 characters). It's output for "0 0 X.X.X.X" is :

          XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
          XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
          XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
          XXXXXXXX............XXXXXXXXXX..........XXXXXXXXXX..........XXXXXXXXXX
          XXXXXX.............XXXXXXXXXX...........XXXXXXXXXX..........XXXXXXXXXX
          ..................XXXXXXXXXXX..........XXXXXXXXXX...........XXXXXXXXXX
          ................XXXXXXXXXXXX...........XXXXXXXXXX..........XXXXXXXXXXX
          ...............XXXXXXXXXXXX...........XXXXXXXXXX...........XXXXXXXXXXX
          ............XXXXXXXXXXXXXX...........XXXXXXXXXXX..........XXXXXXXXXXXX
          .........XXXXXXXXXXXXXXX............XXXXXXXXXXX...........XXXXXXXXXXXX
          XXXXXXXXXXXXXXXXXXXXXXX............XXXXXXXXXXX...........XXXXXXXXXXXXX
          XXXXXXXXXXXXXXXXXXXXX.............XXXXXXXXXXX...........XXXXXXXXXXXXXX
          XXXXXXXXXXXXXXXXXX..............XXXXXXXXXXXX...........XXXXXXXXXXXXXXX
          XXXXXXXXXXXXXXX................XXXXXXXXXXXX............XXXXXXXXXXXXXXX
          XXXXXXXXXXX..................XXXXXXXXXXXXX............XXXXXXXXXXXXXXXX
          ...........................XXXXXXXXXXXXX............XXXXXXXXXXXXXXXXXX
          ........................XXXXXXXXXXXXXXX............XXXXXXXXXXXXXXXXXXX
          ......................XXXXXXXXXXXXXXX.............XXXXXXXXXXXXXXXXXXXX
          ..................XXXXXXXXXXXXXXXXX.............XXXXXXXXXXXXXXXXXXXXXX
          .............XXXXXXXXXXXXXXXXXXXX..............XXXXXXXXXXXXXXXXXXXXXXX
          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...............XXXXXXXXXXXXXXXXXXXXXXXXX
          XXXXXXXXXXXXXXXXXXXXXXXXXXXX...............XXXXXXXXXXXXXXXXXXXXXXXXXXX
          XXXXXXXXXXXXXXXXXXXXXXXX.................XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
          XXXXXXXXXXXXXXXXXXXX...................XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
          XXXXXXXXXXXXXX......................XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

(Can you see the indent?! ;-) )

\$\endgroup\$
8
\$\begingroup\$

APL (74)

{L←⍴⊃C Y X⎕IO←⍞⎕⎕0⋄⎕←{C[⊃⌊L⌊35÷⍨L×.5*⍨+/2*⍨1 2÷⍨⍵-Y X]}¨⍳⍵}25 70

The reason it's wrapped in a function is that the modification to ⎕IO doesn't apply to the rest of the system.

Explanation:

  • L←⍴⊃C Y X⎕IO←⍞⎕⎕0: Set ⎕IO to 0 (making arrays 0-based instead of 1-based), set X to (first line read), set Y to (second line read), set C to (third line read, with no formatting), and set L to the length () of C.
  • 25 70: the dimensions of the matrix.
  • ¨⍳⍵: for each element in the matrix where each element is its own coordinates...
  • ⍵-Y X: difference between the current point and the center point
  • 1 2÷⍨: divide the X coordinate by 2 (because a char is half as wide as it is tall)
  • .5*⍨+/2*⍨: take the square root of the sum of the squares
  • 35÷⍨: divide by 35
  • ⌊L⌊: take the minimum of the amount of characters and the current value and round it down
  • ,/: The values are still wrapped in a list (of only one element), and this will create spacing in the output, so 'free' the values from their lists.
  • C[...]: use the value we found as an index into the character list
  • ⎕←: we now have a matrix where each element (x,y) is the character for (x,y), so output the matrix.
\$\endgroup\$
6
\$\begingroup\$

Perl 5.10, 103 chars

$x=<>;$y=<>;@C=<>=~/./g;for$j(-$y..24-$y){print+(map$C[@C/35*sqrt$_**2/4+$j**2]//$C[-1],-$x..69-$x),$/}
\$\endgroup\$
9
  • \$\begingroup\$ Thanks to @J B for the nice trick at codegolf.stackexchange.com/questions/678/… \$\endgroup\$
    – ninjalj
    Commented Feb 10, 2011 at 23:28
  • \$\begingroup\$ Also thanks to @gnibbler for his "substraction precalculated in range" trick. \$\endgroup\$
    – ninjalj
    Commented Feb 10, 2011 at 23:33
  • \$\begingroup\$ You got an empty line before the output. So currently this is counted as invalid. \$\endgroup\$
    – Joey
    Commented Feb 15, 2011 at 15:32
  • \$\begingroup\$ @Joey: fixed ... \$\endgroup\$
    – ninjalj
    Commented Feb 15, 2011 at 20:26
  • \$\begingroup\$ Still fails a test case of mine, here, though. I added it to the question (Sample 4). Somehow the zeroes in the output get replaced by a semicolon. \$\endgroup\$
    – Joey
    Commented Feb 15, 2011 at 20:44
5
\$\begingroup\$

Perl, 120 chars

$x,$y=<>,<>;@C=split'',<>;for$j(0..24){print+(map$C[($c=$#C/35*sqrt(($x/2-$_/2)**2+($y-$j)**2))<$#C?$c:$#C-1],0..69),$/}
\$\endgroup\$
3
  • \$\begingroup\$ fails on the first and third examples. abs is there for a reason. \$\endgroup\$
    – ninjalj
    Commented Feb 6, 2011 at 11:27
  • 1
    \$\begingroup\$ Squaring removes the sign anyway, I don't see how abs would change anything \$\endgroup\$ Commented Feb 7, 2011 at 8:05
  • 1
    \$\begingroup\$ D'oh! You're completely right. What's failing on my system is $x,$y=<>,<>;. \$\endgroup\$
    – ninjalj
    Commented Feb 7, 2011 at 20:32
4
\$\begingroup\$

Windows PowerShell, 157

Nothing noteworthy. Beaten to death already:

$x,$y,$c=@($input)
$l=$c.Length
$c+=(""+$c[-1])*90
0..24|%{$r=$_
-join(0..69|%{$c[[math]::truncate([math]::sqrt(($x-$_)*($x-$_)+4*($y-$r)*($y-$r))*$l/70)]})}
\$\endgroup\$
2
\$\begingroup\$

C, 176

Here a translation of my Delphi solution to C, saving 24 characters :

X,Y,l,i,j,t;char G[99];main(){scanf("%d\n%d\n",&X,&Y);gets(G);l=strlen(G);for(j=-Y;j<25-Y;j++)for(i=-X-1;i<70-X;)t=floor(l*sqrt(i*i++/4+j*j)/35),putchar(!i+X?10:G[t<l?t:l-1]);}

You can test this code here : http://www.ideone.com/oTvHt

\$\endgroup\$
2
  • 3
    \$\begingroup\$ You can put the declaration of the int variables in the argument list of main() I guess and save a semicolon that way. \$\endgroup\$
    – Joey
    Commented May 29, 2012 at 15:49
  • \$\begingroup\$ 171 bytes \$\endgroup\$
    – ceilingcat
    Commented Mar 30 at 9:02
2
\$\begingroup\$

Common Lisp, 173 characters

(let*((c(read))(v(read))(g(read-line))(l(length g)))(dotimes(y 25)(dotimes(x 70)(princ(elt g(min(floor(*(sqrt(+(expt(/(- c x)2)2)(expt(- v y)2)))l)35)(1- l)))))(princ #\
)))

The only real trick I use here is using

#\[actual newline]

as a newline character literal

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

scala 223 (204 without App-Wrapper)

object C extends App{
import math._
def r=readInt
val p=r
val q=r
val m=readLine
(1 to 70)map(x=>(0 to 25)map(y=>{
printf("%c[%d;%dH%s",27,y,x,m(({
val a=abs(p-x)
val b=abs(q-y)
sqrt(a*a+2*b*b)
}*(m.size-1)/74).toInt))}
))}

Having cols and rows (70, 25) set dynamically would allow for screenfilling gradients.

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

C# 311

Thought I'd make a long program to make the others feel better:

using System;class P{static void Main(){Func<string>r=Console.ReadLine;int x=int.Parse(r()),y=int.Parse(r());var c=r();for(int j=0;j<25;j++){for(int i=0;i<70;i++){var l=c.Length;Console.Write(c[(int)Math.Min(l*Math.Sqrt(Math.Pow(Math.Abs(x-i)/2.0,2)+Math.Pow(Math.Abs(y-j),2))/35,l-1)]);}Console.Write("\n");}}}

Input is taken from the console, one line at a time (two lines for the coordinates and one for the gradient chars).

Thanks to Joey for the tips.

Sample 1: http://ideone.com/X0jIZ

Sample 2: http://ideone.com/RvICt

\$\endgroup\$
8
  • \$\begingroup\$ @Joey Right. I fixed it and realized why trying this in C# was a pretty bad idea. It was fun though. :-) \$\endgroup\$ Commented May 29, 2012 at 16:03
  • \$\begingroup\$ @Joey you're right, neither the class nor the method need to be marked as public \$\endgroup\$ Commented May 29, 2012 at 19:08
  • \$\begingroup\$ @Joey if we use int instead of float, the expressions x-i and y-j become of type int. Due to this, the `int Math.Abs(int value) will be called instead of the float one, leading to some loss of precision. I've tried it and got som e rough edges: ideone.com/wQcMT \$\endgroup\$ Commented May 29, 2012 at 19:14
  • \$\begingroup\$ @Joey Right, Abs does not affect precision, but it returns an int, which, when divided by 2 will lose precision, as opposed to a float. \$\endgroup\$ Commented May 29, 2012 at 19:20
  • \$\begingroup\$ Sorry, I forgot a modification that will work the way you want (costs two characters, but the ints will save more ;)). Oh, and *.5 costs only one char. \$\endgroup\$
    – Joey
    Commented May 29, 2012 at 19:21
0
\$\begingroup\$

JavaScript (from image url or data-url)

Following script accept url or data-url (svg has own problems with viewBox) and gradient of 20 symbols, which represents 10 pixels by 2 chars in each.

And returns an array of rows (including \n) with representation of provided image in ASCII art. I though, that will be right way to use it with Baili characters (My gradient: ⠂⠂⠁⢀⢁⢁⢌⢌⢕⢕⢗⢗⢟⢟⢷⢷⢿⢿) and put in alt attribute of same image.

async function imageToColorfulASCII(imageUrl, chars) {
  const img = new Image();
  img.crossOrigin = "Anonymous"; // To avoid CORS issues
  img.src = imageUrl;

  await img.decode();

  const width = img.width;
  const height = img.height;

  const offscreenCanvas = new OffscreenCanvas(width, height);
  const ctx = offscreenCanvas.getContext("2d");
  ctx.drawImage(img, 0, 0, width, height);

  const imageData = ctx.getImageData(0, 0, width, height).data;

  const darknesses = [];
  const colors = [];

  for (let y = 0; y < height; y++) {
    for (let x = 0; x < width; x++) {
      const index = (y * width + x) * 4;
      const r = imageData[index];
      const g = imageData[index + 1];
      const b = imageData[index + 2];
      const a = imageData[index + 3];

      // Skip transparent pixels
      if (a === 0) continue;

      // Calculate the darkness value (0..20)
      const brightness = (r + g + b) / 3;
      const darkness = Math.floor((1 - brightness / 255) * 9);
      const safeCheck = (char) => (char === " " ? "\xA0" : char);

      darknesses.push(
        safeCheck(chars[darkness * 2]) + safeCheck(chars[darkness * 2 + 1])
      );
      colors.push(
        `#${r.toString(16).padStart(2, "0")}${g
          .toString(16)
          .padStart(2, "0")}${b.toString(16).padStart(2, "0")}`
      );
    }
    darknesses[darknesses.length - 1] += "\n";
  }

  // Log the results in the console
  console.log(
    darknesses.map((d) => `%c${d}`).join(""),
    ...colors.map((color) => `background-color: ${color};font-family:monospace;`)
  );

  return darknesses.join("");
}

Here is an example:

⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂  ⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⢁⢁⢁⢁  ⠂⠂⢌⢌⢕⢕⠁⢀⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⢕⢕⢌⢌⠂⠂⢕⢕⢕⢕⢌⢌⢕⢕⠂⠂  
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⢕⢕⢌⢌⠂⠂⢷⢷⢁⢁  ⠂⠂⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⢕⢕⢌⢌  ⢁⢁⢿⢿⢕⢕⠁⢀⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⢕⢕⢌⢌  ⠂⠂⠁⢀⢗⢗⢿⢿⢁⢁  
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⢕⢕⢌⢌⠂⠂⠁⢀⠂⠂⠂⠂⢕⢕⢕⢕  
⠂⠂⠂⠂⠂⠂⠂⠂⢁⢁⢿⢿⢌⢌⢿⢿⢁⢁⠂⠂⢿⢿⢕⢕⢌⢌⢿⢿⢁⢁  
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⢁⢁⢕⢕⢁⢁⠂⠂⠂⠂⠁⢀⢌⢌⢕⢕⢁⢁⠂⠂⠂⠂
⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂

https://codepen.io/iegik/pen/xxoddBB

\$\endgroup\$

Your Answer

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

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