{
"id": "256",
"prompt": "Visualize a wind field: an 8x8 grid of short line segments, each showing the local flow vector of a vortex whose center wanders on sine-table Lissajous loops - flow is the perpendicular of the offset from the vortex center (swap components, negate one, divide down, clamp) plus a constant easterly drift. Write fieldx/fieldy as reusable fns, because five tracer dots stored in state slots are advected by the SAME field each frame and wrap at the edges. Arrows via draw_line with a brighter tip pixel.",
"solution_rl": "fn clampv(v: i32) -> i32 {\n if v > 9 { return 9; }\n if v < -9 { return -9; }\n v\n}\n\nfn fieldx(y: i32, cy: i32) -> i32 {\n clampv((cy - y) / 14 + 2)\n}\n\nfn fieldy(x: i32, cx: i32) -> i32 {\n clampv((x - cx) / 14)\n}\n\nfn frame(t: i32) {\n let tab = [0, 25, 49, 71, 90, 106, 117, 124, 127, 124, 117, 106, 90, 71, 49, 25, 0, -25, -49, -71, -90, -106, -117, -124, -127, -124, -117, -106, -90, -71, -49, -25];\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n let cx: i32 = w / 2 + tab[(t / 7) & 31] * 80 / 127;\n let cy: i32 = h / 2 + tab[((t / 9) + 8) & 31] * 60 / 127;\n if host::display::state_get(0) == 0 {\n host::display::state_set(0, 1);\n let mut i: i32 = 0;\n while i < 5 {\n host::display::state_set(8 + i * 2, (60 + i * 80) * 8);\n host::display::state_set(9 + i * 2, (90 + i * 50) * 8);\n i = i + 1;\n }\n }\n host::display::clear(0x0a0c10);\n let mut gy: i32 = 0;\n while gy < 8 {\n let mut gx: i32 = 0;\n while gx < 8 {\n let px: i32 = 28 + gx * 56;\n let py: i32 = 28 + gy * 56;\n let vx: i32 = fieldx(py, cy);\n let vy: i32 = fieldy(px, cx);\n host::display::draw_line(px, py, px + vx * 2, py + vy * 2, 0x30485a);\n host::display::fill_rect(px + vx * 2 - 1, py + vy * 2 - 1, 2, 2, 0x5a7890);\n gx = gx + 1;\n }\n gy = gy + 1;\n }\n let mut i: i32 = 0;\n while i < 5 {\n let mut x: i32 = host::display::state_get(8 + i * 2);\n let mut y: i32 = host::display::state_get(9 + i * 2);\n x = x + fieldx(y / 8, cy) * 3;\n y = y + fieldy(x / 8, cx) * 3;\n if x < 0 { x = x + w * 8; }\n if x >= w * 8 { x = x - w * 8; }\n if y < 0 { y = y + h * 8; }\n if y >= h * 8 { y = y - h * 8; }\n host::display::state_set(8 + i * 2, x);\n host::display::state_set(9 + i * 2, y);\n host::display::fill_rect(x / 8 - 3, y / 8 - 3, 6, 6, 0xf0f0f0);\n i = i + 1;\n }\n host::display::draw_string(8, h - 20, \"WANDERING VORTEX\", 0x506070, 1);\n host::display::present();\n}\n",
"tags": [
"simulation",
"vector-field",
"sine-table",
"state"
]
}