Perl: the Markov chain saw PerlMonks

### Re^3: Creating a circle in a data structure

by BrowserUk (Patriarch)
 on May 29, 2007 at 03:50 UTC Need Help??

in reply to Re^2: Creating a circle in a data structure
in thread Creating a circle in a data structure

Try this. I'm not certain it's exactly equivalent to your original as I had some trouble understanding your 'drawing' code, but it's not far off. Your use of %curpass confused me as it seems unnecessary.

You should be able to relate the sub genEndPoints() to the code at the end of the page I linked above. Rather than generating all the points on the circumference for all integer radii upto max radius, it generates only the endpoints (8 at a time using symmetry), of the raster lines required to 'fill' the circle and then draws those lines. Let me know if anything is unclear.

```#!/usr/bin/perl

use strict;
use warnings;

use CGI ();
my \$q = CGI->new;

# use CGI::Carp 'fatalsToBrowser';

my %circle;

my \$prev_data;

# Read the previous circles
my @prev_circles;
{
my @x_vals = \$q->param('x');
my @y_vals = \$q->param('y');
my @r_vals = \$q->param('r');
for(my \$i = 0; \$i < @r_vals; \$i++) {
foreach (\$r_vals[\$i], \$x_vals[\$i], \$y_vals[\$i]) {
die "All values must be positive integers!\n" if /[^\d\.]/
}
push(@prev_circles, {
r => \$r_vals[\$i],
x => \$x_vals[\$i],
y => \$y_vals[\$i],
});
}
}

sub genEndPoints {
my( \$radius, \$xCenter, \$yCenter ) = @_;

my %endPoints;
my( \$x, \$y, \$p ) = ( 0, \$radius, 1 - \$radius );

\$endPoints{ \$yCenter  } = [ \$xCenter - \$radius, \$xCenter + \$radius
+ ];
while( \$x < \$y ) {
\$x++;
if( \$p < 0 ) {
\$p += 2 * \$x + 1;
} else {
\$y--;
\$p += 2 * ( \$x - \$y + 1 );
}
\$endPoints{ \$yCenter - \$y  } = [ \$xCenter - \$x, \$xCenter + \$x
+];
\$endPoints{ \$yCenter + \$y  } = [ \$xCenter - \$x, \$xCenter + \$x
+];
\$endPoints{ \$yCenter - \$x  } = [ \$xCenter - \$y, \$xCenter + \$y
+];
\$endPoints{ \$yCenter + \$x  } = [ \$xCenter - \$y, \$xCenter + \$y
+];
}
return \%endPoints;
}

# 'Draw' the circle in the hash.
foreach my \$circle (@prev_circles) {
my \$rmax     = \$circle->{'r'};
my \$center_x = \$circle->{'x'};
my \$center_y = \$circle->{'y'};
\$prev_data .= qq~
<input type="hidden" name="r" value="\$rmax" />
<input type="hidden" name="x" value="\$center_x" />
<input type="hidden" name="y" value="\$center_y" />\n
~;

my \$endPoints = genEndPoints( \$rmax, \$center_x, \$center_y );

for my \$y ( keys %{ \$endPoints } ) {
my( \$startX, \$endX ) = @{ \$endPoints->{ \$y } };
for my \$x ( \$startX .. \$endX ) {
\$circle{ \$y }{ \$x }++;
}
}
}

my \$output = q~<html>
<title>A circle as a table</title>
<style type="text/css">
body {background: #888;}
.c1 {background: #000;}
.c2 {background: #008;}
.c3 {background: #00f;}
.c4 {background: #08f;}
.c5 {background: #0ff;}
.c6 {background: #8ff;}
.c7 {background: #fff;}
</style>
<body>
<table>
~;
foreach my \$rowid (0 .. max(keys %circle)) {
my \$row = \$circle{\$rowid};
\$output .= "<tr>";
foreach my \$columnid (0 .. max(keys %{\$row})) {
my \$column = \$row->{\$columnid};
my \$class = '';
\$column = 7 if \$column > 7;
\$class = qq~ class="c\$column"~ if \$column;
\$output .= qq~<td\$class></td>~;
}
\$output .= "</tr>\n";
}

\$output .= qq~
</table>
<br />
<br />
<form method="post" >
\$prev_data
<br />New circle's radius: <input type="text" size="10" name="r" value
+="20" />
<br />New circle's center: (<input type="text" size="10" name="x" valu
+e="50" />,
<input type="text" size="10" name="y" value="50" />)
<br /><input type="submit" />
</form>
</body>
</html>
~;

sub max {
my \$max = 0;
foreach (@_) { \$max = \$_ if \$_ > \$max; }
return \$max;
}

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: Creating a circle in a data structure
by AK108 (Friar) on May 29, 2007 at 04:32 UTC
This is exactly what I was looking for. I was able to modify \$circle{ \$y }{ \$x }++; to read \$circle{ \$y }{ \$x } += int sqrt((\$x - \$center_x) ** 2 + (\$y - \$center_y) ** 2);. That color-codes the center going out (and would go to the edge of the circle, if I had enough CSS classes).

My use of %curpass earlier was to ensure that any pixel in the original circle didn't get ++'d twice for the same circle entry.

That color-codes the center going out (and would go to the edge of the circle, if I had enough CSS classes).

You could use the code from A hierarchy of color (intensity)? to generate your CSS classes to form a Color ramp. Below I've used 256 which seems to strike the best balance between smoothness and rendering speed.

I also added cellspacing=0 to the table definition as it much improves the appearance.

```sub colorRamp {
my( \$v, \$vmin, \$vmax ) = @_;
my( \$r, \$g, \$b ) = (1) x 3;
\$v = \$vmax if \$v > \$vmax;
\$v = \$vmin if \$v < \$vmin;

## Uncomment below to invert the color range
\$v = \$vmax + \$vmin - \$v;

my \$dv = \$vmax - \$vmin;
if( \$v < ( \$vmin + 0.25*\$dv ) ) {
\$r = 0;
\$g = 4 * (\$v - \$vmin) / \$dv;
}
elsif( \$v < ( \$vmin + 0.5 * \$dv ) ) {
\$r = 0;
\$b = 1 + 4 * (\$vmin + 0.25 * \$dv - \$v) / \$dv;
}
elsif( \$v < ( \$vmin + 0.75 * \$dv ) ) {
\$r = 4 * (\$v - \$vmin - 0.5 * \$dv) / \$dv;
\$b = 0;
}
else {
\$g = 1 + 4 * (\$vmin + 0.75 * \$dv - \$v) / \$dv;
\$b = 0;
}
return sprintf "#%02x%02x%02x", \$r*255, \$g*255, \$b*255;
}

my \$ColorRange = 255;

my \$styles = '';
\$styles .= sprintf ".c%d { background:%s;}\n",
\$_, colorRamp( \$_, 0, \$ColorRange )
for 0 .. \$ColorRange;

my \$output = qq~<html>
<title>A circle as a table</title>
<style type="text/css">
body {background: #888;}
\$styles
</style>
<body>
<table cellspacing=0 >
~;

foreach my \$rowid (0 .. max(keys %circle)) {
my \$row = \$circle{\$rowid};
\$output .= "<tr>";
foreach my \$columnid (0 .. max(keys %{\$row})) {
my \$column = \$row->{\$columnid};
my \$class = '';
\$column = \$ColorRange if \$column > \$ColorRange;
\$class = qq~ class="c\$column"~ if \$column;
\$output .= qq~<td\$class></td>~;
}
\$output .= "</tr>\n";
}

Is your purpose to generate the structure and the table graphic is just a way of demonstrating it? Or is the display of the result the real objective?

If the latter, it would probably be much faster and use less bandwidth to render to a .png using GD(or similar) and then link the image.

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Very cool, and this is exactly what I was originally envisioning.

However, the table is just a way of displaying it graphically for a test. This will be stored in the database for things like elevation (which will not remain perfect circles -- or I would've stored them as such). Eventually, for displaying, I may have a contour map generated from different values here. For the format, I will either generate to PNG or maybe SVG and then to PNG.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://617914]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-05-25 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found