Hi
sg,
I was going to suggest the "mathematical approach", but when I finished coding it, saw that roboticus had "beat me to the punch". So, at least one other person interpreted your question the same way that I did :-)
Here's my solution:
#!/usr/bin/perl -w
# Strict
use strict;
use warnings;
use Data::Dumper;
# User defined
my $text = "
It's
been said
ever so often
by many an expert
that in ol' plain text
one neer can get a
round tuit, but
just look
here
";
# Main program
$text =~ s/(?<!\s) (?!\s)/@/gms; # Turn inner spaces into
+'@'
print "Debug1> text = [$text]\n"; # Show text
$text =~ s/\s+//; # Strip all other spaces
$text =~ s/\n/@/gs; # Convert newlines to '@'
my @text = grep { /\S/ms } split //, $text; # Split text into charact
+ers
# Find the mass of the diamond
my $mass = (my $temp = $text) =~ s/\S/./g;
print "Debug2> mass = [$mass]\n";
# Circle mass 'm' = pi * r^2, so the radius 'r' = sqrt(m / pi)
my $radius = sqrt($mass / 3.14159265358);
print "Debug3> radius = $radius\n";
# Format the text into a circle,
my $circle = "";
my $aspect = 1.0;
for (my $row = -($radius + 10); $row <= ($radius + 10); $row++) {
for (my $col = -($radius + 10); $col <= ($radius + 10); $col++) {
my $char = " ";
if (($aspect * $row) ** 2 + $col ** 2 <= $aspect * ($radius **
+ 2)) {
$char = shift @text || '*';
}
$circle .= ($char eq '@'? ' ': $char);
}
$circle .= "\n";
}
# Show the resulting circle
print "$circle\n";
__END__
Output:
Debug1> text = [
It's
been@said
ever@so@often
by@many@an@expert
that@in@ol'@plain@text
one@neer@can@get@a
round@tuit,@but
just@look
here
]
Debug2> mass = [120]
Debug3> radius = 6.18038723238067
It's be
en said e
ver so ofte
n by many a
n expert tha
t in ol' pla
in text one
neer can get
a round tu
it, but ju
st look
here
What the above solution boils down to is this: count up the number of characters in the original text, excluding spaces which are outside of the "block" of text. Calculate the "mass" (which in your case is 120), and then use it to find the characters which are within the circle.
You can test the veracity of this solution by changing the text.
For example, if you choose $text to be:
my $text = "
This document is intended to give you a quick overview of the Perl
programming language, along with pointers to further documentation.
It is intended as a \"bootstrap\" guide for those who are new to the
language, and provides just enough information for you to be able to
read other peoples' Perl and understand roughly what it's doing, or
write your own simple scripts.
This introductory document does not aim to be complete. It does not
even aim to be entirely accurate. In some cases perfection has been
sacrificed in the goal of getting the general idea across. You are
*strongly* advised to follow this introduction with more information
from the full Perl manual, the table of contents to which can be
found in perltoc.
";
then you'll get a nice, circular output of:
This docume
nt is intended
to give you a quic
k overview of the Per
l programming language,
along with pointers to
further documentation. It
is intended as a "bootstrap
" guide for those who are ne
w to the language, and provid
es just enough information fo
r you to be able to read other
peoples' Perl and understand
roughly what it's doing, or wr
ite your own simple scripts.
This introductory document doe
s not aim to be complete. It d
oes not even aim to be entirel
y accurate. In some cases per
fection has been sacrificed i
n the goal of getting the ge
neral idea across. You are
*strongly* advised to foll
ow this introduction with
more information from
the full Perl manual,
the table of conte
nts to which can
be found in p
erltoc.
You can vary $aspect to change the aspect ratio of the circle as well. For example, with $aspect = 1.7, the last example looks even more "circular":
This docume
nt is intended to g
ive you a quick overview
of the Perl programming lan
guage, along with pointers to f
urther documentation. It is intend
ed as a "bootstrap" guide for those
who are new to the language, and pro
vides just enough information for you
to be able to read other peoples' Perl
and understand roughly what it's doing,
or write your own simple scripts. This
introductory document does not aim to b
e complete. It does not even aim to be
entirely accurate. In some cases perfec
tion has been sacrificed in the goal
of getting the general idea across.
You are *strongly* advised to follo
w this introduction with more in
formation from the full Perl
manual, the table of conte
nts to which can be fo
und in perltoc.
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.