Circular Pattern
Here is an example of a circular pattern, separated into two functions: one for the positions and the other for the pattern sketch.
export function generateCircularPatternPositionStructs(
radius: number,
nrCopies: number,
{ startAngle = 0, endAngle = 2 * Math.PI, referenceAngle = 0, addLast = false } = {},
) {
const positionStructs: { position: SKYMATH.Vector2D; angle: number }[] = []
const NC = addLast ? nrCopies + 1 : nrCopies
const angleRange = endAngle - startAngle
for (let i = 0; i < NC; i++) {
const angle = (angleRange * i) / nrCopies + startAngle + referenceAngle
const position = new SKYMATH.Vector2D(radius * Math.cos(angle), radius * Math.sin(angle))
positionStructs.push({ position, angle }) // the angle is pushed here so the sketch can be rotated accordingly later
}
return positionStructs
}
export function generateCircularPatternSketch(
sketchToUseInPattern: SKYCAD.Sketch,
radius: number,
nrCopies: number,
{ startAngle = 0, endAngle = 2 * Math.PI, referenceAngle = 0, addLast = false } = {},
) {
const positionStructs = generateCircularPatternPositionStructs(radius, nrCopies, {
startAngle,
endAngle,
referenceAngle,
addLast,
})
const circularPatternSketch = new SKYCAD.Sketch()
positionStructs.forEach((struct) => {
const { position, angle } = struct
const patternSketch = sketchToUseInPattern.clone()
patternSketch.scale(radius / 30) // scale sketch according to radius
patternSketch.rotate(angle) // point inwards
patternSketch.translate(position.x, position.y)
circularPatternSketch.mergeSketch(patternSketch)
})
return circularPatternSketch
}