@@ -2611,30 +2611,44 @@ def ellipse(
2611
2611
return self .eachpoint (lambda loc : e .moved (loc ), True )
2612
2612
2613
2613
def polygon (
2614
- self : T , nSides : int , diameter : float , forConstruction : bool = False
2614
+ self : T ,
2615
+ nSides : int ,
2616
+ diameter : float ,
2617
+ forConstruction : bool = False ,
2618
+ circumscribed : bool = False ,
2615
2619
) -> T :
2616
2620
"""
2617
- Creates a polygon inscribed in a circle of the specified diameter for each point on
2618
- the stack
2621
+ Make a polygon for each item on the stack.
2619
2622
2620
- The first vertex is always oriented in the x direction.
2623
+ By default, each polygon is created by inscribing it in a circle of the
2624
+ specified diameter, such that the first vertex is oriented in the x direction.
2625
+ Alternatively, each polygon can be created by circumscribing it around
2626
+ a circle of the specified diameter, such that the midpoint of the first edge
2627
+ is oriented in the x direction. Circumscribed polygons are thus rotated by
2628
+ pi/nSides radians relative to the inscribed polygon. This ensures the extent
2629
+ of the polygon along the positive x-axis is always known.
2630
+ This has the advantage of not requiring additional formulae for purposes such as
2631
+ tiling on the x-axis (at least for even sided polygons).
2621
2632
2622
2633
:param nSides: number of sides, must be >= 3
2623
- :param diameter: the size of the circle the polygon is inscribed into
2634
+ :param diameter: the diameter of the circle for constructing the polygon
2635
+ :param circumscribed: circumscribe the polygon about a circle
2636
+ :type circumscribed: true to create the polygon by circumscribing it about a circle,
2637
+ false to create the polygon by inscribing it in a circle
2624
2638
:return: a polygon wire
2625
2639
"""
2626
2640
2627
2641
# pnt is a vector in local coordinates
2628
2642
angle = 2.0 * math .pi / nSides
2643
+ radius = diameter / 2.0
2644
+ if circumscribed :
2645
+ radius /= math .cos (angle / 2.0 )
2629
2646
pnts = []
2630
2647
for i in range (nSides + 1 ):
2631
- pnts .append (
2632
- Vector (
2633
- (diameter / 2.0 * math .cos (angle * i )),
2634
- (diameter / 2.0 * math .sin (angle * i )),
2635
- 0 ,
2636
- )
2637
- )
2648
+ o = angle * i
2649
+ if circumscribed :
2650
+ o += angle / 2.0
2651
+ pnts .append (Vector (radius * math .cos (o ), radius * math .sin (o ), 0 ,))
2638
2652
p = Wire .makePolygon (pnts , forConstruction )
2639
2653
2640
2654
return self .eachpoint (lambda loc : p .moved (loc ), True )
0 commit comments