Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Perl & C/C++

by zentara (Archbishop)
on Mar 28, 2003 at 16:00 UTC ( [id://246493]=note: print w/replies, xml ) Need Help??


in reply to Perl & C/C++

Hi, I didn't author this, but it will give you a good idea of how easy it would be in perl/tk.

UPDATE Got rid of huge ugly array.

#!/usr/bin/perl use Tk; my $STEP = 20; my $BOXSIZE = 70; my $WINDOWX = 700; my $WINDOWY = 500; my $mw = new MainWindow( -title => "This is a test", -width => $WINDOWX, -height => $WINDOWY, ); my $canvas = $mw->Canvas( -bg => "black", -width => $WINDOWX, -height => $WINDOWY )->pack(); $canvas->repeat( $STEP, sub { rect($canvas); } ); $canvas->repeat( $STEP, sub { oval($canvas); } ); MainLoop(); sub oval { my $c = shift; my $x = rand $WINDOWX; my $y = rand $WINDOWY; my $sizex = rand $BOXSIZE; my $sizey = rand $BOXSIZE; my $o = $c->createOval( $x, $y, $x + $sizex, $y + $sizey, -fill => getcolor(), ); push ( @ovals, $o ); while ( @ovals > 1000 ) { $c->delete( shift (@ovals) ); } } sub rect { my $c = shift; my $x = rand $WINDOWX; my $y = rand $WINDOWY; my $size = rand $BOXSIZE; my $r = $c->createRectangle( $x, $y, $x + $size, $y + $size, -fill => getcolor(), ); push ( @rects, $r ); while ( @rects > 1000 ) { $c->delete( shift (@rects) ); } } sub getcolor{ my($string) = '#'; my($length) = 7; my(@chars) = ('a' .. 'f', 0 .. 9); while (length($string) != $length) { $string .= $chars[ rand($#chars) ]; } return $string; }

Replies are listed 'Best First'.
•Re: Re: Perl & C/C++
by merlyn (Sage) on Mar 28, 2003 at 17:46 UTC
    Wouldn't it have been simpler to generate that huuge array dynamically as:
    my @COLOR; { local *ARGV; @ARGV = qw(/usr/lib/X11/rgb.txt); while (<>) { push @COLOR, $1 if /\d+\s+\d+\s+\d+\s+(\S+)/; } }
    Or better yet, just generate a random RGB on each hit?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Just wondering... is there any specific reason why you use @ARGV and <>? Methinks it would be cleaner to just open() and close() the file. Also, this way you could choose not to die if the file doesn't exist (e.g. on Windows).

        It's only a warning if the file doesn't exist. Not death.

        And for safety, I have to localize a filehandle anyway... so as long as I am doing that, if I pick *ARGV, I get the diamond for free.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Re: Perl & C/C++
by Anonymous Monk on Mar 28, 2003 at 16:10 UTC

    I'm going to have nightmares about that array.

    Clear separation of data and code is a very, very good thing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://246493]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2024-04-19 00:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found