Vibrating rectangles: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Processing implementation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(11 intermediate revisions by 4 users not shown)
Line 10:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC DrawRectangles()
BYTE i,x1,y1,x2,y2
Line 45:
OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Vibrating_rectangles.png Screenshot from Atari 8-bit computer]
Line 51:
=={{header|AutoHotkey}}==
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">Vibrating_rectangles()
OnExit, Exit
return
Line 118:
ExitApp
Return
;----------------------------------------------------------------</langsyntaxhighlight>
 
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight lang="freebasic">ancho = 200 : alto = ancho
 
graphsize ancho, alto
Line 134:
pause 0.05
next sz
end while</langsyntaxhighlight>
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">Const ancho = 400, alto = ancho
Screenres ancho, alto, 16
Windowtitle "Rosetta Code Vibrating rectangles"
Line 148:
Sleep 40
Next sz
Loop Until Inkey = Chr(27) 'ESC</langsyntaxhighlight>
 
==={{header|True BASIC}}===
<langsyntaxhighlight QBasiclang="qbasic">SET WINDOW -90, 90, -90, 90
 
DO
Line 160:
NEXT i
LOOP
END</langsyntaxhighlight>
 
 
=={{header|C}}==
Dimensions of the rectangles, their number and the animation delay can be configured. Requires the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<syntaxhighlight lang="c">
<lang C>
/*Abhishek Ghosh, 15th September 2018*/
 
Line 197:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
The program works by drawing a rectangle the size of the window and then shrinking it using the Windows InflateRect API call. Each time the rectangle shrinks, it steps through a 48 entry color palette. Each time the program goes through the color palette, it offsets the palette index by one, which causes the color pattern to move inward continously. By allowing the rectangle to shrink past zero, it turns inside out and give the exotic pattern seen in the image.
 
[[File:DelphiVibratingRectangles.png|frame|none]]
<syntaxhighlight lang="Delphi">
 
 
 
const ColorMap47: array [0..46] of TColor = (
0 or (0 shl 8) or (0 shl 16),
255 or (224 shl 8) or (224 shl 16),
255 or (212 shl 8) or (212 shl 16),
255 or (169 shl 8) or (169 shl 16),
255 or (127 shl 8) or (127 shl 16),
255 or (84 shl 8) or (84 shl 16),
255 or (42 shl 8) or (42 shl 16),
255 or (0 shl 8) or (0 shl 16),
255 or (13 shl 8) or (0 shl 16),
255 or (26 shl 8) or (0 shl 16),
255 or (40 shl 8) or (0 shl 16),
255 or (53 shl 8) or (0 shl 16),
255 or (67 shl 8) or (0 shl 16),
255 or (80 shl 8) or (0 shl 16),
255 or (93 shl 8) or (0 shl 16),
255 or (107 shl 8) or (0 shl 16),
255 or (120 shl 8) or (0 shl 16),
255 or (134 shl 8) or (0 shl 16),
255 or (147 shl 8) or (0 shl 16),
255 or (161 shl 8) or (0 shl 16),
255 or (174 shl 8) or (0 shl 16),
255 or (187 shl 8) or (0 shl 16),
255 or (201 shl 8) or (0 shl 16),
255 or (214 shl 8) or (0 shl 16),
255 or (228 shl 8) or (0 shl 16),
255 or (241 shl 8) or (0 shl 16),
255 or (255 shl 8) or (0 shl 16),
236 or (248 shl 8) or (0 shl 16),
218 or (242 shl 8) or (0 shl 16),
200 or (235 shl 8) or (0 shl 16),
183 or (229 shl 8) or (0 shl 16),
167 or (223 shl 8) or (0 shl 16),
151 or (216 shl 8) or (0 shl 16),
136 or (210 shl 8) or (0 shl 16),
122 or (204 shl 8) or (0 shl 16),
108 or (197 shl 8) or (0 shl 16),
95 or (191 shl 8) or (0 shl 16),
83 or (185 shl 8) or (0 shl 16),
71 or (178 shl 8) or (0 shl 16),
60 or (172 shl 8) or (0 shl 16),
49 or (166 shl 8) or (0 shl 16),
39 or (159 shl 8) or (0 shl 16),
30 or (153 shl 8) or (0 shl 16),
22 or (147 shl 8) or (0 shl 16),
14 or (140 shl 8) or (0 shl 16),
6 or (134 shl 8) or (0 shl 16),
0 or (128 shl 8) or (0 shl 16));
 
 
procedure DrawVibratingRectangles(Image: TImage);
var StartRect,WR: TRect;
const PenSize = 2;
var I,J,Offset: integer;
begin
StartRect:=Rect(0,0,Image.Width,Image.Height);
Image.Canvas.Pen.Width:=PenSize;
Image.Canvas.Brush.Style:=bsClear;
Offset:=0;
{Run for 100 seconds}
for J:=0 to 1000 do
begin
{Start with Window-sized rect}
WR:=StartRect;
for I:=0 to 100 do
begin
{Draw rectangle}
Image.Canvas.Pen.Color:=ColorMap47[(I+Offset) mod Length(ColorMap47)];
Image.Canvas.Rectangle(WR);
{Shrink rect by twice the pen width}
InflateRect(WR,-PenSize*2,-PenSize*2);
end;
Image.Repaint;
Inc(Offset);
Application.ProcessMessages;
if AbortFlag or Application.Terminated then break;
Sleep(100);
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
Elapsed Time: 14.692 Sec.
</pre>
 
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=hY/LCoAgEEX3fsXdizJGLYL6mOgBgimotPDrY6w2bdzM3HMuDEzwyPbcowCQCtSMjqM9mCYQw1PN6MeX1uBCRFz8Zn2GISIoGC41jzNcOwZ2qXybvbO+etnw8udVw3936iMg3Qkt3ixu Run it]
[https://easylang.online/apps/_vibrating-rectangles.html Run it]
 
<syntaxhighlight>
<lang>on timer
on timer
sz -= 2
if sz <-= 02
if sz =< 490
color random 1000sz = 49
color randint 1000 - 1
.
.
move 50 - sz 50 - sz
line move 50 +- sz 50 - sz
line 50 + sz 50 +- sz
line 50 -+ sz 50 + sz
line 50 - sz 50 -+ sz
line 50 - sz 50 - sz
timer 0.2
timer 0.2
.
timer 0.2</lang>
</syntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: accessors calendar colors.constants combinators kernel
locals math math.vectors opengl timers ui ui.gadgets
ui.gadgets.worlds ui.pens.solid ui.render ;
Line 265 ⟶ 363:
{ window-controls
{ normal-title-bar close-button minimize-button } }
} <vibrating-gadget> >>gadgets ;</langsyntaxhighlight>
 
 
 
=={{header|FutureBasic}}==
Note: The compiled macOS app runs faster than the attached GIF with a smoother fade.
<syntaxhighlight lang="futurebasic">
_window = 1
 
void local fn BuildWindow
block NSUInteger i = 0
block double red, green, blue
block CGRect r = fn CGRectMake( 0, 0, 410, 410 )
window _window, @"Vibrating Rectangles", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable + NSWindowStyleMaskMiniaturizable
r = fn CGRectMake( 20, 20, 360, 360 )
timerbegin, 0.3, YES
i++
cln red = (((double)arc4random()/0x100000000));
cln green = (((double)arc4random()/0x100000000));
cln blue = (((double)arc4random()/0x100000000));
timerbegin, 0.2, NO
rect fill r, fn ColorWithRGB( red, green, blue, 1.0 )
timerend
rect fill r, fn ColorWithRGB( 0.3, 0.3, 0.3, 1.0 )
r = fn CGRectInset( r, 3, 3 )
if i mod 60 == 0 then r = fn CGRectMake( 20, 20, 360, 360 )
timerend
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long )
select ( ev )
case _windowWillClose : end
end select
end fn
 
random
on dialog fn DoDialog
 
fn BuildWindow
 
HandleEvents
</syntaxhighlight>
[[file:Vibrating_Rectangles_FutureBasic.gif]]
 
=={{header|Go}}==
Line 277 ⟶ 419:
$ eog vibrating2.gif
</pre>
<langsyntaxhighlight lang="go">package main
 
import (
Line 360 ⟶ 502:
log.Fatal(err2)
}
}</langsyntaxhighlight>
 
=={{header|J}}==
Line 459 ⟶ 601:
</html>
</pre>
<langsyntaxhighlight lang="javascript">
const SIZE = 400, WAIT = .025;
class VibRects {
Line 518 ⟶ 660:
vibRects.start();
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Gtk, Graphics, Colors
const height, width, x0, y0 = 480, 640, 320, 240
Line 547 ⟶ 689:
sleep(0.25)
end
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{libheader|gintro}}
Should work on any platform where Gtk is available. Tested on Linux.
<langsyntaxhighlight Nimlang="nim">import sugar
 
import gintro/[gobject, gdk, gtk, gio, cairo]
Line 626 ⟶ 768:
let app = newApplication(Application, "Rosetta.vibrating_rectangles")
discard app.connect("activate", activate)
discard app.run()</langsyntaxhighlight>
 
=={{header|Objeck}}==
Uses SLD2 libraries and 80's neon colors.
<langsyntaxhighlight lang="objeck">use Game.SDL2;
use Game.Framework;
 
Line 745 ⟶ 887:
REC_REFRESH := 15
}
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
Line 751 ⟶ 893:
{{trans|Raku}}
 
<langsyntaxhighlight lang="perl">use utf8;
binmode STDOUT, ":utf8";
use Time::HiRes qw(sleep);
Line 794 ⟶ 936:
print "\n";
}
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 800 ⟶ 942:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/vibrect.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\vibrect.exw
Line 874 ⟶ 1,016:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">
//Aamrun, 3rd July 2022
 
Line 899 ⟶ 1,041:
delay(100);
}
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import turtle
from itertools import cycle
from time import sleep
Line 933 ⟶ 1,075:
rects(tl, colour)
sleep(0.5)
</syntaxhighlight>
</lang>
 
{{out}}
Line 942 ⟶ 1,084:
Via big-bang.
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require 2htdp/image
Line 975 ⟶ 1,117:
(list-ref colors (mod (add1 m)))
(list-ref colors m))))
(list (empty-scene SIZE SIZE 'black))))])])</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 988 ⟶ 1,130:
Draws a series of concentric rectangles then rotates through the color palette. Every three seconds, chooses new random palette colors and reverses rotation direction.
 
<syntaxhighlight lang="raku" perl6line># box drawing characters
my %r = :tl<┌>, :h<─>, :tr<┐>, :v<│>, :bl<└>, :br<┘>;
 
Line 1,032 ⟶ 1,174:
print "\n";
}
}</langsyntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/vibrating-rectangles-perl6.gif Vibrating rectangles] (.gif image)
 
Line 1,038 ⟶ 1,180:
Fully animated SDL2 graphics lib version. Will adjust rendered rectangles to fill resized windows. Hit the space bar to toggle palette rotation direction.
 
<syntaxhighlight lang="raku" perl6line>use SDL2::Raw;
 
my $width = 1200;
Line 1,118 ⟶ 1,260:
}
( $r, $g, $b ).map: ((*+$m) * 255).Int
}</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Vibrating rectangles
 
Line 1,192 ⟶ 1,334:
win1.show()
return
</syntaxhighlight>
</lang>
Output:
 
Line 1,199 ⟶ 1,341:
=={{header|Wren}}==
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color
import "dome" for Window
 
Line 1,243 ⟶ 1,385:
}
 
var Game = VibratingRectangles.new(500, 500)</langsyntaxhighlight>
 
=={{header|X86 Assembly}}==
<langsyntaxhighlight lang="asm"> 1 ;Tanslation of XPL0 and EasyLang. A 75-byte program.
2 ;Assemble with: tasm, tlink /t
3 0000 .model tiny
Line 1,301 ⟶ 1,443:
54 0143 FEBF FFFE 013F 0000 dir dw -320-1, -1-1, 320-1, 1-1 ;directions: up, right, down, left
55 end start
</syntaxhighlight>
</lang>
 
=={{header|XPL0}}==
Translation of EasyLang.
<langsyntaxhighlight XPL0lang="xpl0">int Sz, Color;
 
proc OnTimer;
Line 1,325 ⟶ 1,467:
OnTimer;
until KeyHit;
]</langsyntaxhighlight>
 
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">Width = 300 : Height = 300
 
open window Width, Height
Line 1,341 ⟶ 1,483:
wait 0.01
next
loop</langsyntaxhighlight>
9,476

edits