For a mini game I want to have a map resembling the H-Tree Fractal. The line would be road and you can drive around but only on the road. The map is infinite and needs to be generated as you drive around.
So given a coordinate (x,y) how do I decide if it's road or not road?
Lets define some constants:
The length of each cul-de-sac is L. The line segments in each iteration grow by a factor of 1.5, no sqrt(2), to keep everything rational. And a road is any point at a distance <= 1 from the line. max(dx, dy) <= 1 if that is simpler.
The start point is (0,0) and can be any cul-de-sac on the H-tree, your choice.
Signed distance field implemented in Shader Editor for Android from f-Droid:
This is using single precision floating point with an approximate $\sqrt{2}$. Other inflation factors don't look so good.
The shader code uses a fixed maximum iteration count because unbounded loops on a GPU can be problematic. Moreover with floating point, rounding errors may accumulate. With exact arithmetic and enough memory, this limit can be increased (each increment of 1 doubles the area covered by the tree) or even removed (risking memory exhaustion from large integer storage at extreme distances from the origin).
There is a way to do exact arithmetic with numbers like $a + b \sqrt{2} \quad a,b \in \mathbb{Q}$:
$$(a + b \sqrt{2}) + (x + y \sqrt{2}) = (a + x) + (b + y) \sqrt{2}$$ $$(a + b \sqrt{2}) - (x + y \sqrt{2}) = (a - x) + (b - y) \sqrt{2}$$ $$(a + b \sqrt{2}) (x + y \sqrt{2}) = (a x + 2 b y) + (a y + b x) \sqrt{2}$$ $$\frac{1}{a + b \sqrt{2}} = \frac{a}{d} - \frac{b}{d} \sqrt{2} \text{ where } d = a^2 - 2 b^2$$ $$a + b \sqrt{2} > 0 \text{ when } a > 0 \wedge b > 0$$ $$a + b \sqrt{2} < 0 \text { when } a < 0 \wedge b < 0$$ $$a + b \sqrt{2} > 0 \equiv a^2 > 2 b^2 \text{ when } a > 0 \wedge b < 0 $$ $$a + b \sqrt{2} > 0 \equiv a^2 < 2 b^2 \text{ when } a < 0 \wedge b > 0 $$
You don't need to evaluate or store the $\sqrt{2}$, it's symbolic.
Make sure to use unbounded integers for the rational numbers (overflow of fixed size integral types is catastrophic here).