Polyspiral: Difference between revisions

→‎SDL full animation: More fiddling. Add several more control options and parameters. Now adjusts scale to stay within window viewport
m (→‎SDL full animation: Made window resizeable, compensates for resized widows by centering the polyspiral in the window)
(→‎SDL full animation: More fiddling. Add several more control options and parameters. Now adjusts scale to stay within window viewport)
Line 1,475:
 
===SDL full animation===
Uses the same basic algorithm but fully animated. Use the up / down arrow keys to speed up / slow down the update speed. Use PgUp / PgDn keys to increment / decrement animation speed by large amounts. Use left / right arrow keys to reverse the "direction" of angle change. Press Space bar to toggle animation / reset to minimum speed. Left Control key to toggle stationary / rotating center. Use + / - keys to add remove line segments.
 
<lang perl6>use SDL2::Raw;
 
my $width = 800900;
my $height = 800900;
 
SDL_Init(VIDEO);
Line 1,497:
 
enum KEY_CODES (
K_UP => 82,
K_DOWN => 81,
K_LEFT => 80,
K_RIGHT => 79,
K_SPACE => 44,
K_PGUP => 75,
K_PGDN => 78,
K_LCTRL => 224,
K_PLUS => 87,
K_MINUS => 86,
K_SPLUS => 46,
K_SMINUS => 45,
);
 
my $angle = 0;
my $lines = 240;
my @rgb = palette(^$lines).map: { hsv2rgb(($_ * 360/$lines % 360)/360, 1, 1).list };
my ($x1, $y1);
my $dir = 1;
my $rot = 0;
my $incr = .0001/π;
my $step = $incr*10070;
 
main: loop {
Line 1,522 ⟶ 1,528:
if KEY_CODES(.scancode) -> $comm {
given $comm {
when 'K_LEFT' { $dir = $rot ?? 1 !! -1 }
when 'K_RIGHT' { $dir = $rot ?? -1 !! 1 }
when 'K_UP' { $step += $incr }
when 'K_DOWN' { $step -= $incr if $step > $incr }
when 'K_PGUP' { $step += $incr*50 }
when 'K_PGDN' { $step -= $incr*50; $step = $step < $incr ?? $incr !! $step }
when 'K_SPACE' { $step = $step ?? 0 !! $incr }
when 'K_LCTRL' { $rot = $rot ?? 0 !! -1; $dir *= -1 }
when 'K_PLUS' { $lines = ($lines + 5) min 360; @rgb = palette($lines) }
when 'K_SPLUS' { $lines = ($lines + 5) min 360; @rgb = palette($lines) }
when 'K_MINUS' { $lines = ($lines - 5) max 60; @rgb = palette($lines) }
when 'K_SMINUS' { $lines = ($lines - 5) max 60; @rgb = palette($lines) }
}
}
#say .scancode; # unknown key pressed
}
when *.type == WINDOWEVENT {
if .event == 5 {
$width = .data1;
$height = .data2;
}
Line 1,543 ⟶ 1,555:
$angle = ($angle + $dir * $step) % τ;
($x1, $y1) = $width div 2, $height div 2;
my $dim = $width min $height;
my $scale = (2 + .33 * abs(π - $angle)) * $dim / $lines;
$scale *= ($angle > π) ?? (1 - $angle/τ) !! $angle/τ;
$scale max= $dim/$lines/$lines;
for ^$lines {
my $length = 3$scale + 3$scale * $_;
my ($x2, $y2) = ($x1, $y1) «+« cis(($angle * $rot * $lines) + $angle * $_).reals »*» $length;
SDL_SetRenderDrawColor($render, |@rgb[$_], 255);
SDL_RenderDrawLine($render, |($x1, $y1, $x2, $y2)».round(1));
($x1, $y1) = $x2, $y2;
}
@rgb.=rotate($lines/60);
SDL_RenderPresent($render);
SDL_SetRenderDrawColor($render, 0, 0, 0, 0);
Line 1,557 ⟶ 1,573:
 
SDL_Quit();
 
sub palette ($l) { (^$l).map: { hsv2rgb(($_ * 360/$l % 360)/360, 1, 1).list } };
 
sub hsv2rgb ( $h, $s, $v ){ # inputs normalized 0-1
10,327

edits