Vibrating rectangles: Difference between revisions
SqrtNegInf (talk | contribs) m (was missing first 3 lines of program) |
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: minor twiddles) |
||
Line 63: | Line 63: | ||
my %r = :tl<┌>, :h<─>, :tr<┐>, :v<│>, :bl<└>, :br<┘>; |
my %r = :tl<┌>, :h<─>, :tr<┐>, :v<│>, :bl<└>, :br<┘>; |
||
my |
my @colors = « \e[1;31m \e[1;94m \e[1;33m \e[1;35m \e[1;36m \e[1;32m \e[1;34m »; |
||
bu => "\e[1;34m", mg => "\e[1;35m", cy => "\e[1;36m"; |
|||
# color palette |
# color palette |
||
my @c = flat |
my @c = flat @colors[0] xx 12, @colors[3] xx 12, @colors[2] xx 12; |
||
⚫ | |||
⚫ | |||
print "\e[?25l"; # hide the cursor |
print "\e[?25l"; # hide the cursor |
||
Line 81: | Line 76: | ||
my $rot = 1; |
my $rot = 1; |
||
⚫ | |||
loop { |
loop { |
||
⚫ | |||
display @vibe; |
display @vibe; |
||
@c.=rotate($rot); |
@c.=rotate($rot); |
||
if ++$ %% 30 { |
if ++$ %% 30 { |
||
@c = | |
@c = |@colors.pick(3); |
||
@c = sort(flat @c xx 12); |
@c = sort(flat @c xx 12); |
||
$rot *= -1; |
$rot *= -1; |
||
} |
} |
||
rect($_, 31-$_) for 0..15; |
|||
sleep .1; |
sleep .1; |
||
} |
} |
Revision as of 21:05, 4 September 2018
Create Vibrating rectangles
Perl
Using the core module Time::HiRres
to get sub-second sleep
<lang perl>use utf8; binmode STDOUT, ":utf8"; use Time::HiRes qw(sleep);
%r = ('tl' => qw<┌>, 'tr' => qw<┐>, 'h' => qw<─>, 'v' => qw<│>, 'bl' => qw<└>, 'br' => qw<┘>); @colors = ("\e[1;31m", "\e[1;32m", "\e[1;33m", "\e[1;34m", "\e[1;35m", "\e[1;36m");
print "\e[?25l"; # hide the cursor
$SIG{INT} = sub { print "\e[0H\e[0J\e[?25h"; exit; }; # clean up on exit
while (1) {
@c = palette() unless $n % 16; rect($_, 31-$_) for 0..15; display(@vibe); sleep .20; push @c, $c[0]; shift @c; $n++;
}
sub palette {
my @c = sort { -1 + 2*int(rand 2) } @colors; ($c[0], $c[1], $c[2]) x 12;
}
sub rect {
my ($b, $e) = @_; my $c = $c[$b % @c]; my @bb = ($c.$r{tl}, (($r{h})x($e-$b-1)), $r{tr}."\e[0m"); my @ee = ($c.$r{bl}, (($r{h})x($e-$b-1)), $r{br}."\e[0m"); $vibe[$b][$_] = shift @bb for $b .. $e; $vibe[$e][$_] = shift @ee for $b .. $e; $vibe[$_][$b] = $vibe[$_][$e] = $c.$r{v}."\e[0m" for $b+1 .. $e-1;
}
sub display {
my(@rect) = @_; print "\e[0H\e[0J\n\n"; for my $row (@rect) { print "\t\t\t"; print $_ // ' ' for @$row; print "\n"; }
}</lang>
Perl 6
Ok. The task description is essentially non-existent. In looking at the reference implementation (Ring) it seems like we are supposed to draw a series of concentric rectangles and then alter the colors step-wise. No actual vibration apparent.
Could fire up a GUI but WTH, let's try it at a command line with ANSI.
Draws a series of concentric rectangles then rotates through the color palette. Every three seconds, chooses new random palette colors and reverses rotation direction.
<lang perl6># box drawing characters my %r = :tl<┌>, :h<─>, :tr<┐>, :v<│>, :bl<└>, :br<┘>;
my @colors = « \e[1;31m \e[1;94m \e[1;33m \e[1;35m \e[1;36m \e[1;32m \e[1;34m »;
- color palette
my @c = flat @colors[0] xx 12, @colors[3] xx 12, @colors[2] xx 12;
print "\e[?25l"; # hide the cursor
signal(SIGINT).tap: {
print "\e[0H\e[0J\e[?25h"; # clean up on exit exit;
}
my $rot = 1;
my @vibe;
loop {
rect($_, 31-$_) for ^15; display @vibe; @c.=rotate($rot); if ++$ %% 30 { @c = |@colors.pick(3); @c = sort(flat @c xx 12); $rot *= -1; } sleep .1;
}
sub rect ($b, $e) {
@vibe[$b;$b..$e] = @c[$b % @c]~%r<tl>, |((%r<h>) xx ($e - $b - 1)), %r~"\e[0m";
@vibe[$e;$b..$e] = @c[$b % @c]~%r<bl>, |((%r<h>) xx ($e - $b - 1)), %r
~"\e[0m";
($b ^..^ $e).map: { @vibe[$_;$b] = @vibe[$_;$e] = @c[$b % @c]~%r<v>~"\e[0m" }
}
sub display (@rect) {
print "\e[0H\e[0J\n\n";
for @rect -> @row {
print "\t\t\t";
print $_ // ' ' for @row;
print "\n";
}
}</lang>
See: Vibrating rectangles (.gif image)
Ring
<lang ring>
- Project : Vibrating rectangles
Load "guilib.ring"
color1 = new qcolor() { setrgb( 255,0,0,255 ) } pen1 = new qpen() { setcolor(color1) setwidth(2) }
color2 = new qcolor() { setrgb( 0,255,0,255 ) } pen2 = new qpen() { setcolor(color2) setwidth(2) }
color3 = new qcolor() { setrgb( 0,0,255,255 ) } pen3 = new qpen() { setcolor(color3) setwidth(2) }
penArray = [pen1, pen2, pen3] penNbr = 1
New qapp {
win1 = new qwidget() { setwindowtitle("Drawing using QPixMap") setgeometry(100,100,500,500) label1 = new qlabel(win1) { setgeometry(10,10,500,500) settext("") } Canvas = new qlabel(win1) { MonaLisa = new qPixMap2( 500,500) color = new qcolor(){ setrgb(255,0,0,255) }
daVinci = new qpainter() { begin(MonaLisa) } setpixmap(MonaLisa) }
nCounter = 0 oTimer = new qTimer(win1) { setinterval(500) settimeoutevent("DrawCounter()") start() }
show() } exec()
} DrawCounter()
func DrawCounter()
nCounter++ if nCounter < 15 Draw(penArray[penNbr]) elseif nCounter % 15 = 0 nCounter = 0 penNbr++ if penNbr > 3 penNbr = 1 ok Draw(penArray[penNbr]) ok
return
Func Draw(pen1)
daVinci.setpen(penArray[penNbr]) daVinci.drawrect(50+nCounter*10, 50+nCounter*10, 300-nCounter*20, 300-nCounter*20) Canvas.setpixmap(MonaLisa) win1.show()
return </lang> Output: