JSCAD User Group

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    function to create regular polygon

    Development Discussions
    2
    3
    254
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • gilboonet
      gilboonet last edited by

      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 1 Reply Last reply Reply Quote 0
      • gilboonet
        gilboonet last edited by

        @hrgdavor Thanks a lot that's exactly what I needed,
        Capture d’écran de 2021-12-01 11-24-28.png

        1 Reply Last reply Reply Quote 0
        • hrgdavor
          hrgdavor @gilboonet last edited by

          @gilboonet not sure if this is what u need
          7094d8db-ba0c-4bb1-8b49-8401b616acee-image.png

          α angle of the triangle is Math.PI/sidesCount in radians or 360/sidesCount/2 in degrees
          red line is A = Math.sin(α)
          blue line is A=Math.cos(α)
          the radius you are looking for the circle isR=1 in this drawing

          R=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}
          
          
          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Powered by NodeBB | Contributors