-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: add an animated spirograph.v using gg
- Loading branch information
Showing
2 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import gg | ||
import math | ||
|
||
const pixel_count = 15000 | ||
const p = 3 | ||
const s = math.pi * 2 / f32(pixel_count) | ||
const color = gg.Color{255, 255, 255, 255} | ||
const background = gg.Color{20, 20, 64, 255} | ||
const colors = [ | ||
gg.Color{255, 255, 255, 255}, | ||
gg.Color{255, 0, 255, 255}, | ||
gg.Color{255, 255, 0, 255}, | ||
gg.Color{0, 255, 255, 255}, | ||
gg.Color{0, 0, 255, 255}, | ||
gg.Color{0, 0, 0, 255}, | ||
]! | ||
|
||
mut k := 497 | ||
mut scale := 200 | ||
gg.start( | ||
window_title: 'Spirograph' | ||
bg_color: background | ||
width: 900 | ||
height: 950 | ||
sample_count: 2 | ||
frame_fn: fn [mut k, mut scale] (ctx &gg.Context) { | ||
wsize := gg.window_size() | ||
ctx.begin() | ||
d := math.sin(f64(ctx.frame) / 300) | ||
if math.abs(d) < 0.0015 { | ||
k++ | ||
} | ||
if ctx.pressed_keys[int(gg.KeyCode.left)] { | ||
k-- | ||
} | ||
if ctx.pressed_keys[int(gg.KeyCode.right)] { | ||
k++ | ||
} | ||
if ctx.pressed_keys[int(gg.KeyCode.up)] { | ||
scale++ | ||
} | ||
if ctx.pressed_keys[int(gg.KeyCode.down)] { | ||
scale-- | ||
} | ||
d600 := math.sin(f64(ctx.frame) / 60) | ||
cd600 := math.cos(d600) | ||
mut pdj, mut pkj := [p]f64{}, [p]f64{} | ||
for j := 0; j < p; j++ { | ||
pdj[j], pkj[j] = math.pow(d, j), math.pow(k, j) | ||
} | ||
for i := 0; i < pixel_count; i++ { | ||
mut x, mut y := 0.0, 0.0 | ||
for j := 0; j < p; j++ { | ||
a := pkj[j] * i * s | ||
x += pdj[j] * math.sin(a) * cd600 | ||
y += pdj[j] * math.cos(a) * cd600 | ||
} | ||
ctx.draw_rect_filled(f32(wsize.width / 2 + scale * x), f32(wsize.height / 2 + scale * y), | ||
1, 1, colors[i % colors.len]) | ||
} | ||
ctx.draw_text(wsize.width / 2 - 170, wsize.height - 15, 'frame: ${ctx.frame:06}, k: ${k:4}, scale: ${scale:4}, arrows to change') | ||
ctx.end() | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters