How can we define a dashing pattern in a plot such that it reads arbitrary text in Morse code?
For instance
Graphics[{DotDashed, Line[{{0, 0}, {5, 0}}]}]
this could be interpreted as ".-" that is "a" in Morse code.
Proper spacing would be desirable in order to distinguish ".-" (a) from "-." (n).
(Inspired by @rcollyer's comment to my answer to this question.)
Answer
First define the Morse code (from rosettacode.org with corrections by @evanb)
morsecode = (#1 -> Characters[#2]) & @@@ {
{"a", ".-"}, {"b", "-..."}, {"c", "-.-."},
{"d", "-.."}, {"e", "."}, {"f", "..-."},
{"g", "--."}, {"h", "...."}, {"i", ".."},
{"j", ".---"}, {"k", "-.-"}, {"l", ".-.."},
{"m", "--"}, {"n", "-."}, {"o", "---"},
{"p", ".--."}, {"q", "--.-"}, {"r", ".-."},
{"s", "..."}, {"t", "-"}, {"u", "..-"},
{"v", "...-"}, {"w", ".--"}, {"x", "-..-"},
{"y", "-.--"}, {"z", "--.."}, {"0", "-----"},
{"1", ".----"}, {"2", "..---"}, {"3", "...--"},
{"4", "....-"}, {"5", "....."}, {"6", "-...."},
{"7", "--..."}, {"8", "---.."}, {"9", "----."}
};
Use AbsoluteDashing
and define dots as short dashed, lines as long dashes and include proper spacing between symbols and characters.
morserule = Append[(#[[1]] -> Append[Riffle[#[[2]], 3], 12]) & /@ (morsecode /. {"." -> 2 , "-" -> 7}), " " -> {0, 17}];
toMorseDash[s_String] := Flatten[Characters[ToLowerCase@s] /. morserule]
Now we can test in a simple line.
Graphics[{Thickness[0.02], AbsoluteDashing[toMorseDash[" Morse Code "]], Line[{{0, 0}, {15, 0}}]}]
For plots, use PlotStyle-> AbsoluteDashing[toMorseDash["text"]]
Plot[Sin[t], {t, 0, 4 Pi}, PlotStyle -> {Thick, AbsoluteDashing[toMorseDash[" Sine Wave"]]}]
Comments
Post a Comment