function to create regular polygon
-
Hello, I'm making a design made from a polyhedron and am needing to make regular polygons having same side length. I have a V1 script to do that and before to port it to V2, I was wondering whether it could be done straightly using
a function to compute the radius depending on the side length and the number of segments.
Do you thing such function is possible ?function RegularPolygon(sideLength, sidesCount){ let R = ... return circle ({radius: R, segments: sidesCount}) }
But on the other side, maybe is it better to stop using the segment trick, and by the way, I remember that there is star(), but isn't there it's counterpart that would create regular polygon ?
-
@hrgdavor Thanks a lot that's exactly what I needed,
-
@gilboonet not sure if this is what u need
α
angle of the triangle isMath.PI/sidesCount
in radians or360/sidesCount/2
in degrees
red line isA = Math.sin(α)
blue line isA=Math.cos(α)
the radius you are looking for the circle isR=1
in this drawingR=sideLength/2/Math.sin(α)
or
sideLength=(R*Math.sin(α)) * 2
const jscad = require('@jscad/modeling') // https://openjscad.xyz/docs/module-modeling_primitives.html const { circle } = jscad.primitives function main({// @jscad-params sideLength = 15, sidesCount = 6, }){ let alpha = Math.PI / sidesCount let R = sideLength/2/Math.sin(alpha) // R === sideLength in case of sideCount=6 (hexagon) console.log('R',R) return circle({radius:R, segments: sidesCount}) } module.exports = {main}