13

Bouncing Lines

Another classic staple of the era was the bouncing line, and here were two of my own attempts.

A bouncing line, twice

Two saves forty-nine minutes apart, and the edit between them is entirely in how a bounce is handled.

.BAK, 05:21.PAS, 06:10
starting speed random(10)-5 → −5…4 random(2)+1 → 1 or 2
on a bounce f := -f mirrored random(2)-3 or random(2)+1 re-rolled
colorrandom(10)+4 per line a red ramp indexed by frame

Both files set f[x+2] := f[x], so each endpoint uses one value for both axes and therefore travels at exactly 45°. The .BAK keeps whatever magnitude it was dealt, forever; the .PAS throws it away at every wall and draws a new one.

The .BAK can deal itself a speed of zero

random(10)-5 spans −5 to 4, and that range includes 0. When it comes up, that endpoint has no velocity on either axis and never moves for the rest of the run. It is a one-in-ten chance per endpoint, so roughly one run in five starts with an endpoint nailed down and the bouncing line becomes a fan sweeping about a fixed point, which is a completely different picture from the one the program is trying to draw.

The .PAS cannot do this: random(2)+1 is 1 or 2, and every re-roll after a bounce is drawn from {1, 2} or {−3, −2}. Zero is not reachable. Whether that was the reason for the change or a side-effect of it, the rewrite removes the only way the program could stall.

ASCTXT.BAK

1996-03-02 05:21 · 586 bytes · Random colors, mirror bounce.

uses jmodex,crt;var p:array[1..2]of integer;q:array[1..2]of integer;
x,c:integer;f:array[1..4]of shortint;
begin if set_vga_modex(7,360,480,1)=0 then halt(0);clear_vga_screen(0);
randomize;for x:=1to 2do begin p[x]:=random(360);q[x]:=random(480);
f[x]:=random(10)-5;f[x+2]:=f[x];end;


repeat for x:=1to 2do begin
if (p[x]>=(359-f[x]))or(p[x]<=(1-f[x]))then f[x]:=-f[x];
if (q[x]>=(479-f[x+2]))or(q[x]<=(1-f[x+2]))then f[x+2]:=-f[x+2];
inc(p[x],f[x]);inc(q[x],f[x+2]);end;

draw_line(p[1],q[1],p[2],q[2],random(10)+4);
until keypressed;asm mov ah,0;mov al,3;int 16;end;end.
ASCTXT.PAS

1996-03-02 06:10 · 767 bytes · Red ramp, randomized bounce.

uses jmodex,crt;var p:array[1..2]of integer;q:array[1..2]of integer;
x,c:integer;f:array[1..4]of shortint;a:byte;
begin if set_vga_modex(7,360,480,1)=0 then halt(0);clear_vga_screen(0);
randomize;for x:=1to 2do begin p[x]:=random(360);q[x]:=random(480);
f[x]:=random(2)+1;f[x+2]:=f[x];end;c:=0;
for x:=0to 127do begin set_dac_register(x,x div 2,0,0);
set_dac_register(x+128,63-(x div 2),0,0);end;

repeat for x:=1to 2do begin
if (p[x]>=(359-f[x]))then f[x]:=random(2)-3 else
if (p[x]<=(1-f[x]))then f[x]:=random(2)+1;
if (q[x]>=(479-f[x+2]))then f[x+2]:=random(2)-3 else
if (q[x]<=(1-f[x+2]))then f[x+2]:=random(2)+1;
inc(p[x],f[x]);inc(q[x],f[x+2]);end;
inc(c);draw_line(p[1],q[1],p[2],q[2],c);

until keypressed;asm mov ah,0;mov al,3;int 16;end;end.