https://mht.wtf/post/navigate/ mht.wtf Computer science, programming, and whatnot. Navigate Gates July 27, 2025 back to posts Here's an interesting leetcode-style problem: we are given two points $p_0$ and $p_1$ and a list of $n$ line segments $G=\{g_i\}$ which we call gates. We want to find the shortest path from $p_0$ to $p_1$ that crosses every gate $g_i$ in order. Imagine a boat sailing from port to port through gates to avoid running aground in shallow waters. A Simple Solution A simple solution is to create a graph where the vertices are the two points and the endpoints of the lines (which we'll call $l_i$ and $r_i$). Then we connect up vertices that follow the rules: * $\{v_0, l_i\}\in E $ if the line $(v_0, l_i)$ intersects all gates $g_k$ for $k=1\dots i-1$ (and same with $r$). * $\{l_i ,r_j\} \in E, i bool { let to_p = p - line.root; let rot = [-line.dir.y, line.dir.x].into(); // 90deg CCW 0 < rot.dot(to_p) } Thanks for reading. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License mht | contact