Building a Regular Heptagon Construction Animation on the Web

    This article explains how to implement a regular heptagon construction animation in the browser. It covers geometric object modeling, line-circle and circle-circle intersection, numerical root finding for a marked-ruler neusis construction, animation interpolation, trace-layer management, and numerical correctness checks. The focus is on separating geometry, constraint solving, and rendering so the construction remains maintainable and verifiable.

    1. Problem Background and Technical Decomposition

    A regular heptagon cannot be constructed exactly with a compass and an unmarked straightedge alone. Gauss proved the condition for compass-and-straightedge constructibility of a regular n-gon: n must be the product of a power of 2 and distinct Fermat primes. Since 7 does not satisfy this condition, a regular heptagon requires other tools. The ancient Greeks used a marked straightedge to perform a neusis construction. This kind of method allows the straightedge to pass through a known point while also satisfying a fixed mark-distance constraint, producing intersections that cannot be obtained directly with compass and straightedge.

    In a software implementation, this type of construction can be divided into three layers:

    1. Geometry object layer: represent points, lines, circles, and their relationships with data structures.
    2. Constraint solving layer: compute intersections and find positions that satisfy constraints under constrained motion.
    3. Presentation layer: draw the solving process to the canvas and support animation, trace control, and export.

    Separating these three layers means that later changes to one step do not require changes to the other parts.

    2. Geometric Objects and Intersection Calculation

    The most basic objects are points, lines, and circles. A line is defined by two points, and a circle is defined by its center and radius. The core operations of all compass-and-straightedge constructions reduce to two types of intersection: line-circle and circle-circle.

    Line-circle intersection can be converted into a quadratic equation. Let the parametric equation of the line be P(t) = P1 + t(P2 − P1). Substituting this into the circle equation gives a quadratic expression in t. When the discriminant is less than zero, there is no intersection; when it equals zero, the line is tangent; when it is greater than zero, there are two intersection points.

    1. // Intersections of line (p1, p2) and circle (center, r); the line is treated as infinite
    2. function lineCircleIntersect(p1, p2, center, r) {
    3. const dx = p2.x - p1.x;
    4. const dy = p2.y - p1.y;
    5. const fx = p1.x - center.x;
    6. const fy = p1.y - center.y;
    7. const a = dx * dx + dy * dy;
    8. const b = 2 * (fx * dx + fy * dy);
    9. const c = fx * fx + fy * fy - r * r;
    10. const disc = b * b - 4 * a * c;
    11. if (disc < -1e-12) return [];
    12. const sq = Math.sqrt(Math.max(disc, 0));
    13. const ts = disc < 1e-12 ? [-b / (2 * a)] : [(-b - sq) / (2 * a), (-b + sq) / (2 * a)];
    14. return ts.map(t => ({ x: p1.x + t * dx, y: p1.y + t * dy }));
    15. }

    A tolerance is used here to detect tangency so that floating-point error does not cause a “nearly tangent but missing intersection” problem. When the coordinate scale is large, the tolerance should be scaled proportionally to the canvas size.

    Circle-circle intersection follows the same idea: first compute the projection distance along the line connecting the two centers, then obtain the perpendicular offset from the Pythagorean relationship. When implementing this, pay attention to boundary cases such as concentric or disjoint circles. These cases should return an empty result directly rather than causing a division-by-zero error.

    3. Numerical Solving for the Marked Ruler

    A marked ruler can be abstracted as two marked points on a line whose distance is fixed at a. During construction, the ruler rotates around a pivot point D. The first mark P is constrained to a circle, and the second mark Q must lie on a specified target line.

    This problem has only one degree of freedom, namely the angle θ of P on the circle. Let the circle center be E and the radius be r. Then:

    • P(θ) = E + r(cos θ, sin θ)
    • Q(θ) lies on the ray DP, and |PQ| = a

    The goal is to find θ such that Q(θ) lies on line l. Define a residual function f(θ), equal to the signed distance from Q(θ) to line l. The constraint is satisfied when the residual is zero.

    1. // Pivot D, first mark P; move distance a along DP to obtain the second mark Q
    2. function secondMark(D, P, a) {
    3. const len = Math.hypot(P.x - D.x, P.y - D.y);
    4. const ux = (P.x - D.x) / len;
    5. const uy = (P.y - D.y) / len;
    6. return { x: P.x + a * ux, y: P.y + a * uy };
    7. }
    8. // Bisection root finding: f must have opposite signs on [lo, hi]
    9. function bisect(f, lo, hi, iters = 60) {
    10. let flo = f(lo);
    11. for (let i = 0; i < iters; i++) {
    12. const mid = (lo + hi) / 2;
    13. const fm = f(mid);
    14. if (Math.sign(fm) === Math.sign(flo)) {
    15. lo = mid;
    16. flo = fm;
    17. } else {
    18. hi = mid;
    19. }
    20. }
    21. return (lo + hi) / 2;
    22. }

    The residual function may have multiple zeros. In practice, you should first sample uniformly on [0, 2π) to find intervals where the sign changes, then apply bisection to each interval. This obtains all solutions, and geometric conditions, such as whether Q lies outside a line segment, can be used to select the one that matches the construction semantics.

    4. Animation and Interpolation

    The essence of animation is mapping a continuous parameter to time. For a point on a circle, the most direct method is to let θ change from a start angle to an end angle over time. To make the motion more natural, an easing function can be added so that the velocity is slower at the start and end.

    1. function animateAngle(startAngle, endAngle, durationMs, onFrame) {
    2. const t0 = performance.now();
    3. function tick(now) {
    4. const p = Math.min((now - t0) / durationMs, 1);
    5. // Quadratic ease-in-out
    6. const eased = p < 0.5 ? 2 * p * p : 1 - Math.pow(-2 * p + 2, 2) / 2;
    7. onFrame(startAngle + (endAngle - startAngle) * eased);
    8. if (p < 1) requestAnimationFrame(tick);
    9. }
    10. requestAnimationFrame(tick);
    11. }

    The animation callback should only update state, not perform repeated calculations. Each frame should call the solving function again and refresh the drawing. In this way, the output matches the current parameters and the visuals do not become disconnected from the data.

    5. Trace Control

    Many geometric demonstrations require that a compass leaves a trace as it moves, but that only necessary parts of the trace are retained. The key to implementing this is to separate “drawing” from “retention.”

    It is recommended to add a trace flag to each construction step, indicating whether the trace produced by that step should be retained. During drawing, temporary traces are drawn on a one-time layer that is cleared every frame; traces marked for retention are written to a persistent layer that is appended to but never cleared.

    1. const persistentLayer = []; // Retained traces
    2. const transientLayer = []; // Temporary graphics cleared every frame
    3. function commitStep(step, points) {
    4. if (step.trace) {
    5. persistentLayer.push(...points);
    6. } else {
    7. transientLayer.push(...points);
    8. }
    9. }

    This structure also makes undo, replay, and step-by-step display easier. The persistent layer and the temporary layer do not interfere with each other. When rolling back state, only the persistent layer needs to be rebuilt.

    6. Correctness Verification

    The most common problem in geometric demonstrations is “it looks right, but the numbers are wrong.” Therefore, a numerical check should be performed after each construction is completed.

    For a regular polygon, there are two criteria:

    1. All side lengths are equal within tolerance.
    2. The angle subtended by adjacent vertices at the center is equal to 2π/n.