JSCAD User Group
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    function to create regular polygon

    Scheduled Pinned Locked Moved Development Discussions
    3 Posts 2 Posters 603 Views 2 Watching
    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.
    • gilboonetG Offline
      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 ?

      hrgdavorH 1 Reply Last reply Reply Quote 0
      • hrgdavorH Offline
        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
        • gilboonetG Offline
          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

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          Powered by NodeBB | Contributors