Vibrating rectangles: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Perl 6}}: Add an SDL2 graphics lib version)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(36 intermediate revisions by 19 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 42 ⟶ 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]
 
<syntaxhighlight>
on timer
sz -= 2
if sz < 0
sz = 49
color randint 1000 - 1
.
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
</syntaxhighlight>
 
=={{header|Factor}}==
<syntaxhighlight 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 ;
IN: rosetta-code.vibrating-squares
 
TUPLE: vibrating < gadget
{ old-color initial: COLOR: black }
{ new-color initial: COLOR: red }
{ frame initial: 0 } ;
 
DEFER: on-tick
 
: <vibrating-gadget> ( -- gadget )
vibrating new COLOR: black <solid> >>interior COLOR: red
>>new-color COLOR: black >>old-color dup [ on-tick ] curry f
250 milliseconds <timer> start-timer ;
 
M: vibrating pref-dim* drop { 420 420 } ;
 
: draw-squares ( loc dim n -- loc' dim' )
[ 2dup gl-rect [ { 10 10 } v+ ] [ { -20 -20 } v+ ] bi* ]
times ;
 
M:: vibrating draw-gadget* ( GADGET -- )
GADGET frame>> 20 mod :> n
GADGET new-color>> gl-color
{ 10 10 } { 400 400 } n draw-squares
GADGET old-color>> gl-color
20 n - draw-squares 2drop ;
 
:: on-tick ( GADGET -- )
GADGET relayout-1
GADGET [ 1 + ] change-frame frame>> 20 mod zero? [
GADGET new-color>> GADGET old-color<<
GADGET new-color>> {
{ COLOR: red [ COLOR: green ] }
{ COLOR: green [ COLOR: blue ] }
[ drop COLOR: red ]
} case GADGET new-color<<
] when ;
 
MAIN-WINDOW: vibrating-squares
{
{ title "Vibrating Squares" }
{ window-controls
{ normal-title-bar close-button minimize-button } }
} <vibrating-gadget> >>gadgets ;</syntaxhighlight>
 
 
 
=={{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 54 ⟶ 419:
$ eog vibrating2.gif
</pre>
<langsyntaxhighlight lang="go">package main
 
import (
Line 137 ⟶ 502:
log.Fatal(err2)
}
}</langsyntaxhighlight>
 
=={{header|J}}==
<pre>
NB. warning: overwrites /tmp/10[0-3][0-9].jpg
NB. and /tmp/r.gif
 
NB. uses imagemagic convert and a browser to display the animation.
NB. works on linux
 
NB. https://rosettacode.org/wiki/Ulam_spiral_(for_primes)#J
 
require'jpeg'
 
spiral =: ,~ $ [: /: }.@(2 # >:@i.@-) +/\@# <:@+: $ (, -)@(1&,)
ulamspiral =: *: - spiral
 
NB. Corners are the squares of every other odd number.
NB. Rectangles ulams with first column < and <: second column of CORNERS
[CORNERS=: *: 1 2 p. i. _20 2
5929 6241
5329 5625
4761 5041
4225 4489
3721 3969
3249 3481
2809 3025
2401 2601
2025 2209
1681 1849
1369 1521
1089 1225
841 961
625 729
441 529
289 361
169 225
81 121
25 49
1 9
 
NB. S is a sufficiently large Ulam spiral matrix
S=: ulamspiral 81
NB. A are 20 Boolean bitmaps of squares
A =: CORNERS ((> {.)~ *. (<: {:)~)"1 _ S
NB. B is a bitmap of all the squares
B =: +/ A
 
NB. C is a running sum. first 6 upper left corners shown
<"2 ] 6 10 10 {. C =: B +"2 +/\ A
┌───────────────────┬───────────────────┬───────────────────┬───────────────────┬───────────────────┬───────────────────┐
│0 0 0 0 0 0 0 0 0 0│0 0 0 0 0 0 0 0 0 0│0 0 0 0 0 0 0 0 0 0│0 0 0 0 0 0 0 0 0 0│0 0 0 0 0 0 0 0 0 0│0 0 0 0 0 0 0 0 0 0│
│0 2 2 2 2 2 2 2 2 2│0 2 2 2 2 2 2 2 2 2│0 2 2 2 2 2 2 2 2 2│0 2 2 2 2 2 2 2 2 2│0 2 2 2 2 2 2 2 2 2│0 2 2 2 2 2 2 2 2 2│
│0 2 0 0 0 0 0 0 0 0│0 2 0 0 0 0 0 0 0 0│0 2 0 0 0 0 0 0 0 0│0 2 0 0 0 0 0 0 0 0│0 2 0 0 0 0 0 0 0 0│0 2 0 0 0 0 0 0 0 0│
│0 2 0 1 1 1 1 1 1 1│0 2 0 2 2 2 2 2 2 2│0 2 0 2 2 2 2 2 2 2│0 2 0 2 2 2 2 2 2 2│0 2 0 2 2 2 2 2 2 2│0 2 0 2 2 2 2 2 2 2│
│0 2 0 1 0 0 0 0 0 0│0 2 0 2 0 0 0 0 0 0│0 2 0 2 0 0 0 0 0 0│0 2 0 2 0 0 0 0 0 0│0 2 0 2 0 0 0 0 0 0│0 2 0 2 0 0 0 0 0 0│
│0 2 0 1 0 1 1 1 1 1│0 2 0 2 0 1 1 1 1 1│0 2 0 2 0 2 2 2 2 2│0 2 0 2 0 2 2 2 2 2│0 2 0 2 0 2 2 2 2 2│0 2 0 2 0 2 2 2 2 2│
│0 2 0 1 0 1 0 0 0 0│0 2 0 2 0 1 0 0 0 0│0 2 0 2 0 2 0 0 0 0│0 2 0 2 0 2 0 0 0 0│0 2 0 2 0 2 0 0 0 0│0 2 0 2 0 2 0 0 0 0│
│0 2 0 1 0 1 0 1 1 1│0 2 0 2 0 1 0 1 1 1│0 2 0 2 0 2 0 1 1 1│0 2 0 2 0 2 0 2 2 2│0 2 0 2 0 2 0 2 2 2│0 2 0 2 0 2 0 2 2 2│
│0 2 0 1 0 1 0 1 0 0│0 2 0 2 0 1 0 1 0 0│0 2 0 2 0 2 0 1 0 0│0 2 0 2 0 2 0 2 0 0│0 2 0 2 0 2 0 2 0 0│0 2 0 2 0 2 0 2 0 0│
│0 2 0 1 0 1 0 1 0 1│0 2 0 2 0 1 0 1 0 1│0 2 0 2 0 2 0 1 0 1│0 2 0 2 0 2 0 2 0 1│0 2 0 2 0 2 0 2 0 2│0 2 0 2 0 2 0 2 0 2│
└───────────────────┴───────────────────┴───────────────────┴───────────────────┴───────────────────┴───────────────────┘
 
NB. D is C catenated to itself with the ones and twos swapped.
D=: (, ]`(0,2,:1"0)}) C
 
NB. E is the 40 matrices with ID, in 3 levels
E=: (;"2 0 (1000 + i.@#))@:(*&16b301070) D
 
NB. overwrite the files then get some help from the shell
empty@:(writejpeg('.jpg' ,~ '/tmp/' , ":))&>/"1 E
2!:0'convert -resize 600% /tmp/10[0-3][0-9].jpg -delay 10 -loop 0 /tmp/r.gif'
 
echo 'please view the animation /tmp/r.gif in chrome browser'
</pre>
 
=={{header|Javascript}}==
Line 159 ⟶ 601:
</html>
</pre>
<langsyntaxhighlight lang="javascript">
const SIZE = 400, WAIT = .025;
class VibRects {
Line 218 ⟶ 660:
vibRects.start();
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Gtk, Graphics, Colors
const height, width, x0, y0 = 480, 640, 320, 240
const can = @GtkCanvas()
const win = GtkWindow(can, "Vibrating Rectangles", width, height)
const colrs = colormap("rdBu")
const sizes = collect(2:4:div(width, 2))
const params = [1, 2]
 
draw(can) do widget
ctx = getgc(can)
set_line_width(ctx, 1)
c = colrs[params[1]]
set_source_rgb(ctx, c.r, c.g, c.b)
i = sizes[params[2]]
rectangle(ctx, x0 - i, y0 - i, 2i, div(8i, 3))
stroke(ctx)
end
 
while true
params[1] = params[1] % 99 + 1
params[2] = params[2] % (length(sizes) - 1) + 1
draw(can)
show(can)
sleep(0.25)
end
</syntaxhighlight>
 
=={{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.
<syntaxhighlight lang="objeck">use Game.SDL2;
use Game.Framework;
 
class Vibrating {
@framework : GameFramework;
@rec_offset : Int;
@rec_colors : Color[];
@rec_color_index : Int;
 
function : Main(args : String[]) ~ Nil {
vibrating := Vibrating->New();
vibrating->Run();
}
 
New() {
@framework := GameFramework->New(GameConsts->SCREEN_WIDTH, GameConsts->SCREEN_HEIGHT, "Vibrating Rectangles");
@framework->SetClearColor(Color->New(0, 0, 0));
@rec_colors := Color->New[5];
@rec_colors[0] := Color->New(255, 240, 1);
@rec_colors[1] := Color->New(253, 25, 153);
@rec_colors[2] := Color->New(153, 252, 32);
@rec_colors[3] := Color->New(0, 230, 254);
@rec_colors[4] := Color->New(161, 14, 236);
}
method : Run() ~ Nil {
if(@framework->IsOk()) {
e := @framework->GetEvent();
frame_count := 0;
quit := false;
while(<>quit) {
@framework->FrameStart();
# process input
while(e->Poll() <> 0) {
if(e->GetType() = EventType->SDL_QUIT) {
quit := true;
};
};
 
Render(frame_count);
 
@framework->FrameEnd();
 
frame_count += 1;
if(frame_count >= @framework->GetFps()) {
frame_count := 0;
};
};
}
else {
"--- Error Initializing Environment ---"->ErrorLine();
return;
};
 
leaving {
@framework->Quit();
};
}
 
method : Render(frame_count : Int) ~ Nil {
# rectangle offsets
if(frame_count % GameConsts->REC_REFRESH = 0) {
@rec_offset += 1;
if(@rec_offset >= GameConsts->REC_MAX) {
@rec_offset := 0;
@rec_color_index += 1;
};
};
 
# rectangle colors
first_color := @rec_colors[@rec_color_index];
second_color : Color;
if(@rec_color_index + 1 < @rec_colors->Size()) {
second_color := @rec_colors[@rec_color_index + 1];
}
else {
second_color := @rec_colors[0];
@rec_color_index := 0;
};
 
@framework->Clear();
 
for(i := 1; i < GameConsts->REC_MAX; i += 1;) {
if(i < @rec_offset) {
DrawRectangle(i, first_color);
}
else {
DrawRectangle(i, second_color);
};
};
 
@framework->Show();
}
 
method : DrawRectangle(step : Int, color : Color) ~ Nil {
x := step * GameConsts->REC_DIST; w := GameConsts->SCREEN_WIDTH - x * 2;
y := step * GameConsts->REC_DIST; h := GameConsts->SCREEN_HEIGHT - y * 2;
 
renderer := @framework->GetRenderer();
renderer->SetDrawColor(color->GetR(), color->GetG(), color->GetB(), 0);
renderer->DrawRect(Rect->New(x, y, w, h));
renderer->DrawRect(Rect->New(x + 1, y + 1, w - 2, h - 2));
renderer->DrawRect(Rect->New(x + 2, y + 2, w - 4, h - 4));
}
}
 
consts GameConsts {
SCREEN_WIDTH := 640,
SCREEN_HEIGHT := 480,
REC_DIST := 12,
REC_MAX := 20,
REC_REFRESH := 15
}
</syntaxhighlight>
 
=={{header|Perl}}==
Using the core module <code>Time::HiRres</code> to get sub-second <tt>sleep</tt>
{{trans|Perl 6Raku}}
 
<langsyntaxhighlight lang="perl">use utf8;
binmode STDOUT, ":utf8";
use Time::HiRes qw(sleep);
Line 267 ⟶ 936:
print "\n";
}
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/vibrect.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\vibrect.exw
-- ========================
--
-- Draws concentric rectangles in random colours to simulate vibration.
-- 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}}==
<syntaxhighlight lang="java">
//Aamrun, 3rd July 2022
 
int counter = 100;
 
void setup(){
size(1000,1000);
}
 
void draw(){
for(int i=0;i<20;i++){
fill(counter - 5*i);
rect(10 + 20*i,10 + 20*i,980 - 40*i,980 - 40*i);
}
counter++;
if(counter > 255)
counter = 100;
delay(100);
}
</syntaxhighlight>
 
=={{header|Python}}==
<syntaxhighlight lang="python">import turtle
from itertools import cycle
from time import sleep
 
def rect(t, x, y):
x2, y2 = x/2, y/2
t.setpos(-x2, -y2)
t.pendown()
for pos in [(-x2, y2), (x2, y2), (x2, -y2), (-x2, -y2)]:
t.goto(pos)
t.penup()
 
def rects(t, colour, wait_between_rect=0.1):
for x in range(550, 0, -25):
t.color(colour)
rect(t, x, x*.75)
sleep(wait_between_rect)
 
tl=turtle.Turtle()
screen=turtle.Screen()
screen.setup(620,620)
screen.bgcolor('black')
screen.title('Rosetta Code Vibrating Rectangles')
tl.pensize(3)
tl.speed(0)
tl.penup()
tl.ht()
colours = 'red green blue orange white yellow'.split()
for colour in cycle(colours):
rects(tl, colour)
sleep(0.5)
</syntaxhighlight>
 
{{out}}
Hmm, maybe [https://repl.it/@PaddyMcCarthy/DeafeningDroopyBusiness this]?
 
=={{header|Racket}}==
 
Via big-bang.
 
<syntaxhighlight lang="racket">#lang racket
 
(require 2htdp/image
2htdp/universe)
 
(define N 20)
(define SIZE 400)
(define OFFSET 80)
(define RATE 0.2)
 
;; a state is a pair of color index and position
 
(define colors '(red orange yellow green blue indigo violet))
(define (mod x) (modulo x (length colors)))
 
(big-bang (cons 0 (sub1 N))
[on-tick
(match-lambda
[(cons m 0) (cons (mod (add1 m)) (sub1 N))]
[(cons m n) (cons m (sub1 n))])
RATE]
[to-draw
(match-lambda
[(cons m n)
(apply
overlay
(append
(for/list ([i (in-range N 0 -1)])
(square (* i (/ (- SIZE OFFSET) N))
'outline
(if (> i n)
(list-ref colors (mod (add1 m)))
(list-ref colors m))))
(list (empty-scene SIZE SIZE 'black))))])])</syntaxhighlight>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.06}}
===ANSI graphics===
Line 279 ⟶ 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 323 ⟶ 1,174:
print "\n";
}
}</langsyntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/vibrating-rectangles-perl6.gif Vibrating rectangles] (.gif image)
 
Line 329 ⟶ 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 409 ⟶ 1,260:
}
( $r, $g, $b ).map: ((*+$m) * 255).Int
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<lang Phix>-- demo\rosetta\vibrect.exw
--
-- Draws concentric rectangles in random colours to simulate vibration.
-- 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).
--
integer numrects = 125 -- (max non-touching for a height of 500)
 
include pGUI.e
 
Ihandle dlg, canvas
cdCanvas cddbuffer, cdcanvas
 
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
integer {w,h} = IupGetIntInt(canvas, "DRAWSIZE")
atom dw = w/(numrects*2+1),
dh = h/(numrects*2+1)
 
cdCanvasActivate(cddbuffer)
for i=1 to numrects do
cdCanvasSetForeground(cddbuffer,rand(#FFFFFF))
atom wd = i*dw,
hd = i*dh
cdCanvasRect(cddbuffer, wd, w-wd, hd, h-hd)
end for
cdCanvasFlush(cddbuffer)
return IUP_DEFAULT
end function
 
function map_cb(Ihandle ih)
cdcanvas = cdCreateCanvas(CD_IUP, ih)
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}}==
<lang python>import turtle
from itertools import cycle
from time import sleep
 
def rect(t, x, y):
x2, y2 = x/2, y/2
t.setpos(-x2, -y2)
t.pendown()
for pos in [(-x2, y2), (x2, y2), (x2, -y2), (-x2, -y2)]:
t.goto(pos)
t.penup()
 
def rects(t, colour, wait_between_rect=0.1):
for x in range(550, 0, -25):
t.color(colour)
rect(t, x, x*.75)
sleep(wait_between_rect)
 
tl=turtle.Turtle()
screen=turtle.Screen()
screen.setup(620,620)
screen.bgcolor('black')
screen.title('Rosetta Code Vibrating Rectangles')
tl.pensize(3)
tl.speed(0)
tl.penup()
tl.ht()
colours = 'red green blue orange white yellow'.split()
for colour in cycle(colours):
rects(tl, colour)
sleep(0.5)
</lang>
 
{{out}}
Hmm, maybe [https://repl.it/@PaddyMcCarthy/DeafeningDroopyBusiness this]?
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Vibrating rectangles
 
Line 595 ⟶ 1,334:
win1.show()
return
</syntaxhighlight>
</lang>
Output:
 
[https://1drv.ms/v/s!AqDUIunCqVnIg1MxKPi5DzwUbJEf Vibrating rectangles]
 
=={{header|Wren}}==
{{libheader|DOME}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "dome" for Window
 
var Palette = [
Color.red, Color.green, Color.blue, Color.orange,
Color.indigo, Color.pink, Color.yellow, Color.white
]
 
class VibratingRectangles {
construct new(width, height) {
Window.title = "Vibrating Rectangles"
Window.resize(width, height)
Canvas.resize(width, height)
_w = width
_h = height
}
 
init() {
_c = 0
_r = 0
_frame = 0
}
 
update() {
_frame = _frame + 1
if (_frame == 60) _frame = 0
if (_frame % 15 == 0) {
_r = _r + 1
if (_r > 20) {
_r = 0
_c = (_c + 1) % 8
}
}
}
 
draw(alpha) {
var x = (_w * (_r + 1) / 50).floor
var y = (_h * (_r + 1) / 50).floor
var w = _w - 2 * x
var h = _h - 2 * y
Canvas.rect(x, y, w, h, Palette[_c])
}
}
 
var Game = VibratingRectangles.new(500, 500)</syntaxhighlight>
 
=={{header|X86 Assembly}}==
<syntaxhighlight lang="asm"> 1 ;Tanslation of XPL0 and EasyLang. A 75-byte program.
2 ;Assemble with: tasm, tlink /t
3 0000 .model tiny
4 0000 .code
5 .386
6 org 100h
7 ;assume: ax=0, bx=0, cx=00FFh, bp=09??h, direction flag is clear
8
9 0100 B0 13 start: mov al, 13h ;set 320x200x8 graphics mode
10 0102 CD 10 int 10h
11 0104 68 A000 push 0A000h ;set segment register to graphics memory
12 0107 07 pop es
13
14 ;ax = misc, color
15 ;bx = size
16 ;cx = loop counter
17 ;dx = delay
18 ;si = direction
19 ;di = pen location
20 ;bp = color
21
22 0108 B4 86 main: mov ah, 86h ;delay 0.2 seconds
23 010A B1 03 mov cl, 3 ;200000 = 30D40h; (ch=0)
24 010C BA 0D40 mov dx, 0D40h
25 010F CD 15 int 15h
26
27 0111 80 EB 02 sub bl, 2 ;Size:= Size - 2
28 0114 7D 06 jge draw ;if Size < 0 then
29 0116 B3 31 mov bl, 49 ; Size:= 49
30 0118 0F AF EB imul bp, bx ;color:= Random; (bh=0)
31 011B 45 inc bp
32 011C draw: ;Move(50-Size, 50-Size)
33 011C 69 FB FEBF imul di, bx, -321 ;di:= 50-Size + (50-Size)*320
34 0120 81 C7 3EB2 add di, 50*321 ;di:= (50-Size)*321
35 ;di:= Size*-321 + 50*321
36 0124 BE 0006 mov si, 3*2 ;set index for dir table
37 0127 8B C5 mov ax, bp ;Line(50-Size, 50+Size, Color);
38 0129 6B CB 02 lin20: imul cx, bx, 2 ;length = Size*2
39 012C AA lin30: stosb ;es:[di++]:= al
40 012D 03 BC 0143r add di, [si+dir] ;next pixel position
41 0131 E2 F9 loop lin30 ;cx--
42 0133 4E dec si
43 0134 4E dec si
44 0135 79 F2 jns lin20
45
46 0137 B4 01 mov ah, 01h ;loop until keystroke
47 0139 CD 16 int 16h
48 013B 74 CB jz main
49
50 013D B8 0003 mov ax, 0003h ;restore normal text-mode screen
51 0140 CD 10 int 10h
52 0142 C3 ret ;return to DOS
53
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>
 
=={{header|XPL0}}==
Translation of EasyLang.
<syntaxhighlight lang="xpl0">int Sz, Color;
 
proc OnTimer;
[Sz:= Sz - 2;
if Sz < 0 then
[Sz:= 49;
Color:= Ran(256);
];
Move(50-Sz, 50-Sz);
Line(50+Sz, 50-Sz, Color);
Line(50+Sz, 50+Sz, Color);
Line(50-Sz, 50+Sz, Color);
Line(50-Sz, 50-Sz, Color);
];
 
[SetVid($13); \set video to VGA 320x200 in 256 colors
Sz:= 0;
repeat DelayUS(200_000);
OnTimer;
until KeyHit;
]</syntaxhighlight>
 
 
=={{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