返回资讯中心
外部精选
软件工程
#开发者实时流

An O(N) implicit representation for the Koch Snowflake

The inner field is correct, but the outer field has widely varying gradients

Hacker News 最新mkeeter2026/11/24 · 今天4 分钟阅读

以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。

The inner field is correct, but the outer field has widely varying gradients (shown by the spacing between the lines).

After posting on social media, Alexa Schor came up with an elegant alternate design based on Into the Portal: Directable Fractal Self-Similarity.

The core idea here (adapted from her design) is using reflections, which construct the union of a shape and its reflection without fully copying the shape. We can use reflections to revisit our earlier hexify function.

Remember, hexify is used to construct six copies of the model at different points in space:

fn hexify(s) { union([ s.remap(x * 3, -y * 3 + 2, z), s.remap(x * 3, y * 3 + 2, z), s.remap(x * 3 + sqrt(3), -y * 3 - 1, z), s.remap(x * 3 - sqrt(3), -y * 3 - 1, z), s.remap(x * 3 - sqrt(3), y * 3 - 1, z), s.remap(x * 3 + sqrt(3), y * 3 - 1, z), ]) / 3 }

We can instead write hexify in terms of a fold operation, which performs a combined reflection + union across a particular axis:

fn fold(x, y, angle) { let nx = -sin(angle); let ny = cos(angle); let dist = x * nx + y * ny; // dot product let offset = max(dist, 0); [x - 2 * offset * nx, y - 2 * offset * ny] }

fn hexify(shape) { let f0 = fold(x, y, 120 * PI/180); let f1 = fold(f0[0], f0[1], -120 * PI/180); let f2 = fold(f1[0], f1[1], 120 * PI/180); shape.remap(x * 3, y * 3 - 2, z) // scale and transform .remap(f2[0], f2[1]) // six-fold reflection / 3 // normalize distance field }

The core thing to observe here is that we are only creating a single modified shape, instead of six transformed copies. Repeated applications of hexify are each O(1) in additional terms, thanks to common subexpression elimination.

With a little more tweaking, this produces the Best Possible Snowflake:

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →