Instead of going round the circle, it may be easier to iterate over all values in one direction, i.e. something like:
foreach $ydist ( 0..$radius ) { # using the formula: # xdist*xdist + ydist*ydist = radius*radius $xdist = int(sqrt( $radius*$radius - $ydist*$ydist)); # Take advantage of symmetry to plot 4 points at once # (for circle outline) $circle{$x_center-$xdist}{$y_center-$ydist}++; $circle{$x_center+$xdist}{$y_center-$ydist}++; $circle{$x_center-$xdist}{$y_center+$ydist}++; $circle{$x_center+$xdist}{$y_center+$ydist}++; # Or to fill up the whole disc... foreach $x ( $x_center-$xdist .. $x_center+$xdist ) { $circle{$x}{$y_center-$ydist}++; $circle{$x}{$y_center+$ydist}++; } }
Of course this assumes you're dealing with a grid of integers. Right now it also counts every point on the axis twice but that's easy enough to fix.
In reply to Re: Creating a circle in a data structure
by Crackers2
in thread Creating a circle in a data structure
by AK108
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |