Vibrating rectangles: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Wren)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(20 intermediate revisions by 11 users not shown)
Line 8:
Create [https://1drv.ms/v/s!AqDUIunCqVnIg1MxKPi5DzwUbJEf Vibrating rectangles]
<br>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC DrawRectangles()
BYTE i,x1,y1,x2,y2
x1=40 y1=8 x2=119 y2=87
FOR i=1 TO 20
DO
Plot(x1,y1)
DrawTo(x2,y1)
DrawTo(x2,y2)
DrawTo(x1,y2)
DrawTo(x1,y1)
x1==+2 y1==+2
x2==-2 y2==-2
OD
RETURN
 
PROC Main()
BYTE CH=$02FC
BYTE hue,lum
 
Graphics(7+16)
Color=1
DO
IF Color=1 THEN
Color=2
ELSE
Color=1
FI
hue=Rand(16)
lum=Rand(8)*2
SetColor(Color-1,hue,lum)
DrawRectangles()
UNTIL CH#$FF
OD
CH=$FF
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Vibrating_rectangles.png Screenshot from Atari 8-bit computer]
 
=={{header|AutoHotkey}}==
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<syntaxhighlight lang="autohotkey">Vibrating_rectangles()
OnExit, Exit
return
Vibrating_rectangles(){
global
pToken := Gdip_Startup()
colors := ["0xFFFF0000", "0xFF00FF00", "0xFF0000FF", "0xFFFFFF00", "0xFF00FFFF", "0xFFFF00FF", "0xFFC0C0C0"
, "0xFF808080", "0xFF800000", "0xFF808000", "0xFF008000", "0xFF800080", "0xFF008080", "0xFF000080"]
Gdip1(320, 320)
loop
{
x := y := 10, w := h := 300
color := Colors[Mod(A_Index-1, 14) + 1]
pPen := Gdip_CreatePen(color, 2)
loop 20
{
if GetKeyState("Esc", "P")
ExitApp
Gdip_DrawRectangle(G, pPen, x, y, w, h)
x += 10, y += 10, w -= 20, h -= 20
UpdateLayeredWindow(hwnd, hdc)
Sleep 20
}
Sleep 200
}
UpdateLayeredWindow(hwnd, hdc)
}
;----------------------------------------------------------------
Gdip1(w:=0, h:=0){
global
w := w ? w : A_ScreenWidth
h := h ? h : A_ScreenHeight
x := A_ScreenWidth/2 - w/2
y := A_ScreenHeight/2 - h/2
Gui, gdip1: -Caption +E0x80000 +LastFound +OwnDialogs +Owner +AlwaysOnTop
Gui, gdip1: Show, w%w% h%h% x%x% y%y%
hwnd := WinExist()
hbm := CreateDIBSection(w, h)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetSmoothingMode(G, 4)
pBrush := Gdip_BrushCreateSolid("0xFF000000")
Gdip_FillRoundedRectangle(G, pBrush, 0, 0, w, h, 5)
Gdip_DeleteBrush(pBrush)
UpdateLayeredWindow(hwnd, hdc)
OnMessage(0x201, "WM_LBUTTONDOWN")
}
;----------------------------------------------------------------
Gdip2(){
global
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
}
;----------------------------------------------------------------
WM_LBUTTONDOWN(){
PostMessage, 0xA1, 2
}
;----------------------------------------------------------------
Exit:
gdip2()
ExitApp
Return
;----------------------------------------------------------------</syntaxhighlight>
 
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">ancho = 200 : alto = ancho
 
graphsize ancho, alto
clg
 
while not keypressed(16777216) #ESCape
color rgb(rand * 255, rand * 255, rand * 255), 0
for sz = 2 to ancho step 4
rect (sz, sz, ancho - sz*2, alto - sz*2)
pause 0.05
next sz
end while</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Const ancho = 400, alto = ancho
Screenres ancho, alto, 16
Windowtitle "Rosetta Code Vibrating rectangles"
Cls
 
Do
Color Rgb(Rnd * 255, Rnd * 255, Rnd * 255)
For sz As Integer = 2 To ancho Step 4
Line (sz, sz) - (ancho - sz, alto - sz),, B
Sleep 40
Next sz
Loop Until Inkey = Chr(27) 'ESC</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">SET WINDOW -90, 90, -90, 90
 
DO
SET COLOR INT(RND * 15)
FOR i = 2 to 90 step 4
BOX LINES i, 45-i, i, 45-i
PAUSE .2
NEXT i
LOOP
END</syntaxhighlight>
 
 
=={{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 43 ⟶ 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
set_color random 1000sz = 49
color randint 1000 - 1
.
.
move_pen 50 - sz 50 - sz
draw_line move 50 +- sz 50 - sz
draw_line line 50 + sz 50 +- sz
draw_line line 50 -+ sz 50 + sz
draw_line line 50 - sz 50 -+ sz
line 50 - sz 50 - sz
set_timer 0.2
timer 0.2
.
timer 0
set_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 111 ⟶ 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 123 ⟶ 419:
$ eog vibrating2.gif
</pre>
<langsyntaxhighlight lang="go">package main
 
import (
Line 206 ⟶ 502:
log.Fatal(err2)
}
}</langsyntaxhighlight>
 
=={{header|J}}==
Line 305 ⟶ 601:
</html>
</pre>
<langsyntaxhighlight lang="javascript">
const SIZE = 400, WAIT = .025;
class VibRects {
Line 364 ⟶ 660:
vibRects.start();
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Gtk, Graphics, Colors
const height, width, x0, y0 = 480, 640, 320, 240
Line 393 ⟶ 689:
sleep(0.25)
end
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{libheader|gintro}}
Should work on any platform where Gtk is available. Tested on Linux.
<syntaxhighlight lang="nim">import sugar
 
import gintro/[gobject, gdk, gtk, gio, cairo]
import gintro/glib except Pi
 
const
Width = 640
Height = 480
X0 = Width div 2 - 1
Y0 = Height div 2 - 1
Colors = [(0.0, 0.0, 0.0), (255.0, 0.0, 0.0), (0.0, 255.0, 0.0), (0.0, 0.0, 255.0),
(255.0, 0.0, 255.0), (0.0, 255.0, 255.0), (255.0, 255.0, 0.0), (255.0, 255.0, 255.0)]
Sizes = collect(newSeq, for size in countdown(Width, 4, 16): float(size))
 
type
 
# Description of the simulation.
Simulation = ref object
area: DrawingArea # Drawing area.
isize: Natural # Size index.
icolor: Natural # Color index.
 
#---------------------------------------------------------------------------------------------------
 
proc newSimulation(area: DrawingArea): Simulation =
## Allocate and initialize the simulation object.
 
new(result)
result.area = area
result.isize = 0
 
#---------------------------------------------------------------------------------------------------
 
proc draw(sim: Simulation; context: cairo.Context) =
## Draw the rectangles.
 
context.setSource(Colors[sim.icolor])
let width = Sizes[sim.isize]
let height = width * 3 / 4
context.rectangle(X0 - width * 0.5, Y0 - height * 0.5, width, height)
context.stroke()
 
#---------------------------------------------------------------------------------------------------
 
proc update(sim: Simulation): gboolean =
## Update the simulation state.
 
sim.isize = (sim.isize + 1) mod Sizes.len
if sim.isize == 0:
# Change color for next cycle.
sim.icolor = (sim.icolor + 1) mod Colors.len
sim.draw(sim.area.window.cairoCreate())
result = gboolean(1)
 
#---------------------------------------------------------------------------------------------------
 
proc activate(app: Application) =
## Activate the application.
 
let window = app.newApplicationWindow()
window.setSizeRequest(Width, Height)
window.setTitle("Vibrating rectangles")
 
let area = newDrawingArea()
window.add(area)
 
let sim = newSimulation(area)
timeoutAdd(40, update, sim)
window.showAll()
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
let app = newApplication(Application, "Rosetta.vibrating_rectangles")
discard app.connect("activate", activate)
discard app.run()</syntaxhighlight>
 
=={{header|Objeck}}==
Uses SLD2 libraries and 80's neon colors.
<langsyntaxhighlight lang="objeck">use Game.SDL2;
use Game.Framework;
 
Line 512 ⟶ 887:
REC_REFRESH := 15
}
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
Line 518 ⟶ 893:
{{trans|Raku}}
 
<langsyntaxhighlight lang="perl">use utf8;
binmode STDOUT, ":utf8";
use Time::HiRes qw(sleep);
Line 561 ⟶ 936:
print "\n";
}
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
<lang Phix>-- demo\rosetta\vibrect.exw
You can run this online [http://phix.x10.mx/p2js/vibrect.htm here].
--
<!--<syntaxhighlight lang="phix">(phixonline)-->
-- Draws concentric rectangles in random colours to simulate vibration.
<span style="color: #000080;font-style:italic;">--
-- Press +/- to increase/decrease the number of rectangles being drawn.
-- demo\rosetta\vibrect.exw
-- Resizing the window, as it turns out, achieves much the same effect
-- ========================
-- as +/-, only much quicker (by increasing/decreasing the spacing).
--
-- Draws concentric rectangles in random colours to simulate vibration.
integer numrects = 125 -- (max non-touching for a height of 500)
-- Press +/- to increase/decrease the number of rectangles being drawn.
-- Resizing the window, as it turns out, achieves much the same effect
-- as +/-, only much quicker (by increasing/decreasing the spacing).
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">numrects</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">125</span> <span style="color: #000080;font-style:italic;">-- (max non-touching for a height of 500)</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">dw</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">/(</span><span style="color: #000000;">numrects</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">dh</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">/(</span><span style="color: #000000;">numrects</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">numrects</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">#FFFFFF</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">wd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">dw</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">hd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">dh</span>
<span style="color: #7060A8;">cdCanvasRect</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">wd</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">-</span><span style="color: #000000;">wd</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hd</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">-</span><span style="color: #000000;">hd</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_WHITE</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_BLACK</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">timer_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_IGNORE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'+'</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">numrects</span><span style="color: #0000FF;">></span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">numrects</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">-</span><span style="color: #008000;">','</span>
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CONTINUE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=602x502"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Vibrating Rectangles"`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"KEY_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- release the minimum limitation</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">timer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupTimer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"timer_cb"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">40</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Processing}}==
include pGUI.e
<syntaxhighlight lang="java">
//Aamrun, 3rd July 2022
 
int counter = 100;
Ihandle dlg, canvas
cdCanvas cddbuffer, cdcanvas
 
void setup(){
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
size(1000,1000);
integer {w,h} = IupGetIntInt(canvas, "DRAWSIZE")
}
atom dw = w/(numrects*2+1),
dh = h/(numrects*2+1)
 
void draw(){
cdCanvasActivate(cddbuffer)
for i=1 to numrects do
for(int i=0;i<20;i++){
cdCanvasSetForeground(cddbuffer,rand(#FFFFFF))
fill(counter - 5*i);
atom wd = i*dw,
rect(10 + 20*i,10 + 20*i,980 - 40*i,980 - hd = i40*dhi);
}
cdCanvasRect(cddbuffer, wd, w-wd, hd, h-hd)
counter++;
end for
cdCanvasFlush(cddbuffer)
if(counter > 255)
return IUP_DEFAULT
counter = 100;
end function
 
delay(100);
function map_cb(Ihandle ih)
}
cdcanvas = cdCreateCanvas(CD_IUP, ih)
</syntaxhighlight>
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
cdCanvasSetBackground(cddbuffer, CD_WHITE)
cdCanvasSetForeground(cddbuffer, CD_BLACK)
return IUP_DEFAULT
end function
 
function timer_cb(Ihandle /*ih*/)
IupUpdate(canvas)
return IUP_IGNORE
end function
 
function esc_close(Ihandle /*ih*/, atom c)
if c=K_ESC then return IUP_CLOSE end if
if c='+' or (c='-' and numrects>3) then
numrects -= c-','
cdCanvasClear(cddbuffer)
IupUpdate(canvas)
end if
return IUP_CONTINUE
end function
 
procedure main()
IupOpen()
canvas = IupCanvas(NULL)
IupSetAttribute(canvas, "RASTERSIZE", "602x502") -- initial size
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
 
dlg = IupDialog(canvas)
IupSetAttribute(dlg, "TITLE", "Vibrating Rectangles")
IupSetCallback(dlg, "K_ANY", Icallback("esc_close"))
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
 
IupMap(dlg)
IupSetAttribute(canvas, "RASTERSIZE", NULL) -- release the minimum limitation
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
Ihandle timer = IupTimer(Icallback("timer_cb"), 40)
IupMainLoop()
IupClose()
end procedure
main()</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import turtle
from itertools import cycle
from time import sleep
Line 671 ⟶ 1,075:
rects(tl, colour)
sleep(0.5)
</syntaxhighlight>
</lang>
 
{{out}}
Line 680 ⟶ 1,084:
Via big-bang.
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require 2htdp/image
Line 713 ⟶ 1,117:
(list-ref colors (mod (add1 m)))
(list-ref colors m))))
(list (empty-scene SIZE SIZE 'black))))])])</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 726 ⟶ 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 770 ⟶ 1,174:
print "\n";
}
}</langsyntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/vibrating-rectangles-perl6.gif Vibrating rectangles] (.gif image)
 
Line 776 ⟶ 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 856 ⟶ 1,260:
}
( $r, $g, $b ).map: ((*+$m) * 255).Int
}</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Vibrating rectangles
 
Line 930 ⟶ 1,334:
win1.show()
return
</syntaxhighlight>
</lang>
Output:
 
Line 937 ⟶ 1,341:
=={{header|Wren}}==
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color
import "dome" for Window
 
Line 981 ⟶ 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,039 ⟶ 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,063 ⟶ 1,467:
OnTimer;
until KeyHit;
]</langsyntaxhighlight>
 
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">Width = 300 : Height = 300
 
open window Width, Height
backcolor 0, 0, 0
clear window
 
do
color ran(255), ran(255), ran(255)
for sz = 2 to Width step 4
rectangle sz, sz, Width - sz, Height - sz
wait 0.01
next
loop</syntaxhighlight>
9,476

edits