line drawing?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi everyone,
I'm looking for a way to draw a line that will zig-zag its way thru an  
image randomly, crisscrossing itself many times, based on random numbers  
_and_ php.

To give you an idea of what I'd like to do, here's a link to a gif :

I've spent a lot of time googling about how to draw that kind of line  
with php but I only came across how to draw texts, charts and pies.

Is there a way to do that or am I barking up the wrong tree?

Thanks for your answers


Re: line drawing?

marko wrote:
Quoted text here. Click to load it

I guess javascript or even java would be a better option, what I can think of  
you can do with php, would be place out one pixel large images by somewhat  
controlled random (has to have one side connected to another black dot) with  
the help of div-tags (html).


Re: line drawing?

J.O. Aho schreef:

Quoted text here. Click to load it

Hmm, I wouldn't do that...  I guess there's some overhead then...

Why not use the image library from php? ( )
I don't know about it much, but I use it to dynamically create captchas
and buttons on my site.



Re: line drawing?

Bart op de grote markt said the following on 24/11/06 14:32:
Quoted text here. Click to load it

I think you're on the right track as this little number is in there.


The OP only needs short lines, the end coordinates of the first line  
then become the start coordinates of the next line, the end coordinates  
being part of a random routine all in a short loop.


"Naturally the common people don't want war, but they can always be
brought to the bidding of the leaders.  Tell them they are being
attacked and denounce the pacifists for lack of patriotism and
endangering the country.  It works the same in every country."
Reichsmarshall Herman Goering, Nuremberg, 1946

Re: line drawing?

An noise sounding like marko said:
Quoted text here. Click to load it
Use gd to draw the lines
start point = randx randy
for(1 - how many lines)
    end point = randx randy
    draw line from start point to end point
    start point = end point
    next loop


Trees with square roots don't have very natural logs.
What's the difference between ignorance and apathy? Who knows? Who cares?

Re: line drawing?

Quoted text here. Click to load it

You need to use the GD image functions:

0) pick number (random?) of iterations for entire image
1) pick a random start point ($startX, $startY)
2) pick a random direction to move in ($directionX, $directionY), could
be -1, 0, or 1 for each
3) pick a random number of moves ($move)
4) loop $move times placing pixels in the image
5) loop back to 2

basic code snippet (uses no 'real' math), you'll need to read up on some
geometric math to do fancier.

// create image here

$directions = array(-1,0,1);

$startX = rand(0, $imageW); // image width
$startY = rand(0, $imageH); // image height
$iterations = rand(5000,15000); // 10000 iterations
for ($main_loop = 0; $main_loop <= $iterations, $main_loop++)
    $tmp = rand(0,2);
    $directionX = $directions[$tmp];
    $tmp = rand(0,2);
    $directionY = $directions[$tmp];

    $moves = rand(5,100);

    for ($draw_loop = 0, $draw_loop <= $moves, $draw_loop++)
        if ((($startX + $directionX) >= 0) AND (($startX + $directionX) <=
            $startX += $directionX;
        if ((($startY + $directionY) >= 0) AND (($startY + $directionY) <=
            $startY += $directionY;
        // place pixel draw function here using $startX, and $startY as the
// output image here

Can probably be done with less code, but this is just off the top of my head
to get you started...


Re: line drawing?

Norman Peelman a écrit :
<snip code>

Quoted text here. Click to load it

thank you very much, this is really interesting.
I'll try to add in some curves by looking up at some math.

thank you!


Site Timeline