Unfortunately, it turns out that the Bresenham algorithm isn't exactly the right thing - or I've misconscrewed something. Here's my implementation of it, with extra code to detect the crossings; it does a reasonable distribution of the two arrays, but doesn't quite follow the rules that I originally stated:

#!/usr/bin/perl -w # Created by Ben Okopnik on Wed Jul 4 13:39:28 EDT 2007 # Modified and converted to code from Wikipedia entry for "Bresenham" sub brezzie { my ($x1, $y1) = @_; ($x1, $y1) = ($y1, $x1) if $y1 > $x1; my ($deltax, $deltay) = ($x1, $y1); my $error = -$deltax / 2; my $y = 0; my ($last_a, $last_b) = (0, 0); for my $x (0 .. $x1){ # print $steep ? "$y $x\n" : "$x $y\n"; if ($y1 > $x1){ ($b, $a) = ($x, $y); } else { ($a, $b) = ($y, $x); } print "A" if $a > $last_a; print "B" if $b > $last_b; ($last_a, $last_b) = ($a, $b); $error += $deltay; if ($error > 0){ $y = $y + 1; $error -= $deltax; } } } brezzie(4, 14);

Running this results in 'BABBBBABBBABBBBABB' - and what I'm looking for is more like 'ABBBBBABBBBABBBBBA'. Max distance is not quite the same thing as a fair distribution.

I really appreciate the help that you've given me so far!


In reply to Re^2: Spreading out the elements by oko1
in thread Spreading out the elements by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.