Skip to content

Instantly share code, notes, and snippets.

@anoved
Created March 18, 2014 15:48
Show Gist options
  • Save anoved/9622826 to your computer and use it in GitHub Desktop.
Save anoved/9622826 to your computer and use it in GitHub Desktop.
OpenSCAD pointed star module
// points = number of points (minimum 3)
// outer = radius to outer points
// inner = radius to inner points
module Star(points, outer, inner) {
// polar to cartesian: radius/angle to x/y
function x(r, a) = r * cos(a);
function y(r, a) = r * sin(a);
// angular width of each pie slice of the star
increment = 360/points;
union() {
for (p = [0 : points-1]) {
// outer is outer point p
// inner is inner point following p
// next is next outer point following p
assign( x_outer = x(outer, increment * p),
y_outer = y(outer, increment * p),
x_inner = x(inner, (increment * p) + (increment/2)),
y_inner = y(inner, (increment * p) + (increment/2)),
x_next = x(outer, increment * (p+1)),
y_next = y(outer, increment * (p+1))) {
polygon(points = [[x_outer, y_outer], [x_inner, y_inner], [x_next, y_next], [0, 0]], paths = [[0, 1, 2, 3]]);
}
}
}
}
@sbliven
Copy link

sbliven commented Jun 14, 2020

Thanks! For regular pentagrams I defined:

module pentagram(outer) {
    inner = outer*sqrt(3.5-1.5*sqrt(5));
    Star(5,outer, inner);
}

@louigi600
Copy link

Assign is going to be deprecated so while I was at it I rewrote the module a little simpler : (it can now take either radii or diameters)

module star(p, d1=0, d2=0, r1=0, r2=0) 
{ R1= r1!=0 ? r1: d1/2;
  D1= d1!=0 ? d1: 2*r1;
  R2= r2!=0 ? r2: d2/2;
  D2= d2!=0 ? d2: 2*r2;
  
  union()
  { for(i=[0:p-1])
    { i1=[cos(i*360/p - 180/p)*R1,sin(i*360/p - 180/p )*R1]; //first inner point
      i2=[cos(i*360/p + 180/p)*R1,sin(i*360/p + 180/p)*R1]; //second inner point
      i3=[cos(i*360/p + 180)*R1,sin(i*360/p + 180)*R1];      //third inner point
      o=[cos(i*360/p)*R2,sin(i*360/p)*R2];                           //outer point
      polygon([i1,o,i2,i3],[[0,1,2,3]]);
    }
  }
}

@zorgoz
Copy link

zorgoz commented Nov 28, 2022

Have you tried it with 3? Won't work - you get a triangle. It won't behave correctly when increasing the number of tips.
This is way simpler:

module Star(p=5, r1=4, r2=10) {
  s = [for(i=[0:p*2]) 
    [
      (i % 2 == 0 ? r1 : r2)*cos(180*i/p),
      (i % 2 == 0 ? r1 : r2)*sin(180*i/p)
    ]
  ];
    
  polygon(s);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment