Syntactic Confectionery Delight PerlMonks

### comment on

 Need Help??
Intro: The following post might not make many sense to the innocent bypasser. It is the conclusion of a lengthy discussion on approaches with Limbic~Region on the ChatterBox, and it describes in a code example how I would attack this. Instead of printing to the screen, or a page, I'd build a bitmap graphic out of it, save it to a file, and look at it / postprocess using graphic viewer programs. Anyway, Limbic~Region seems to be very happy with the result, as it is completely different from what he used to do, 10 or so years ago.

OK, I think I've got something... I think this function will properly calculate Euclidian coordinates for each number you feed it. At least, it calculates the correct coordinates for the first 16 values, and it completely covers the test area, with no holes: if I make each calculated point black for my whole integer range, I get a completely black area, with no spots.

```sub position {
my(\$i) = @_;
my \$e = int sqrt (--\$i);
my \$r = \$i - \$e*\$e;
if(\$e & 1) {  # \$e is odd
if(\$r <= \$e) {
\$x = (\$e+1)/2;
\$y = (\$e-1)/2 - \$r;
} else {
\$x = (\$e+1)/2 - (\$r-\$e);
\$y = -(\$e+1)/2;
}
} else { # \$e is even
if(\$r <= \$e) {
\$x = -\$e/2;
\$y = -\$e/2 + \$r;
} else {
\$x = -\$e/2 + (\$r-\$e);
\$y = \$e/2;
}
}
return (\$x, \$y);
}
Update: Well, it should produce Euclidian coordinates now. My y's were upside down. Habit from work... :-)

Now, my idea was to combine this with GD, so you can place a pixel for each number you want marked. For example, this will mark all numbers divisible by 16. It is largely an adaption of the synopsis for GD.

You need, of course, to combine this with the above sub.

```my \$edge = 100;

use GD;
# create a new image
my \$im = new GD::Image(\$edge+2, \$edge+2);

my \$white = \$im->colorAllocate(255,255,255);
my \$red = \$im->colorAllocate(255,0,0);

# make the background transparent white
\$im->transparent(\$white);

# This is mine :-)
# plot the points
my \$edge_squared = \$edge * \$edge;
for (my \$i = 1; \$i <= \$edge_squared; \$i++) {
if(\$i % 16 == 0) {
my(\$x, \$y) = position(\$i);
\$im->setPixel(\$edge/2+\$x, \$edge/2-\$y, \$red);
}
}

# Save the image file
open PNG, ">result.png" or die "Can't write to file: \$!";
# make sure we are writing to a binary stream
binmode PNG;

# Convert the image to PNG and save it
print PNG \$im->png;

If eventually you still need to cut it up to print, you could use one of the graphics modules to split this image up into subimages.

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• 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 or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2021-09-27 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?