polygonY() Helper

Function: polygonY(o, sides, radius, centerY, stepsPerEdge)

Signature

polygonY(o, sides, radius, centerY, stepsPerEdge)

Description

Returns the y-coordinate of a point that walks along the perimeter of a regular polygon. The walker index o moves along edges; each edge is split into stepsPerEdge linear segments.

Parameters

Returns

Number — the y-coordinate at step o.

Formula

Let edge = ⌊o / stepsPerEdge⌋ and t = (o % stepsPerEdge) / stepsPerEdge.
Let θ₁ = (edge · 2π) / sides and θ₂ = ((edge+1) · 2π) / sides.

Y(o) = (1 - t) · (R · sin(θ₁) + Cᵧ)  +  t · (R · sin(θ₂) + Cᵧ)

Reference implementation (JS)

function polygonY(o, sides, radius, centerY, stepsPerEdge){
  const edge = Math.floor(o / stepsPerEdge);
  const t    = (o % stepsPerEdge) / stepsPerEdge;
  const a1   = (edge     * 2*Math.PI) / sides;
  const a2   = ((edge+1) * 2*Math.PI) / sides;
  return (1 - t) * (Math.sin(a1) * radius + centerY)
       +      t  * (Math.sin(a2) * radius + centerY);
}

Usage example

// Walk a hexagon (pair with polygonX for full coordinates)
const sides = 6, R = 140, stepsPerEdge = 40;
const CX = 360, CY = 220;

function X(o){ return polygonX(o, sides, R, CX, stepsPerEdge); }
function Y(o){ return polygonY(o, sides, R, CY, stepsPerEdge); }

// Total steps around perimeter:
const total = sides * stepsPerEdge; // 240
// Sample:
X(0), Y(0);  // first vertex
X(120), Y(120); // opposite side

Tip: use o % (sides*stepsPerEdge) to loop indefinitely.

See also

polygonX() · starX() · starY()