polygon
polygon(x: number, y: number, radius: number, sides: number, radius2?: number, rotation?: number) => void
Draws a regular polygon centered at the specified coordinates.
Parameters
x: Center x-coordinatey: Center y-coordinateradius: Radius (distance from center to vertices)sides: Number of sidesradius2: Optional secondary radius to create non-regular polygonsrotation: Optional rotation angle in radians (default: 0)
Example
// Triangle
polygon(100, 100, 50, 3)
// Square
polygon(250, 100, 50, 4, null, Math.PI/4) // Rotated 45 degrees
// Hexagon
polygon(400, 100, 50, 6)
// Star-like shape with different radii
polygon(250, 250, 100, 5, 50, 0)
// In JSX component
const draw = (K: KlintContext) => {
K.fillColor("purple")
K.strokeColor("white")
K.polygon(K.width/2, K.height/2, 100, 8, null, K.time * 0.01)
}
Notes
- Do not animate the
rotationparameter directly, prefer therotationfunction. - Setting
radius2creates shapes similar to stars or flower petals - With
sides = 4androtation = Math.PI/4, creates a diamond shape - Increasing
sidesto large values (>20) approximates a circle, but prefer thecircleunless you have a good reason