1+ <#
2+ . SYNOPSIS
3+ Draws a polygon
4+ . DESCRIPTION
5+ Draws a regular polygon with N sides.
6+
7+ To draw a closed polygon, provide a whole number of sides.
8+
9+ To draw an open polygon, provide a fractial number of sides.
10+ . EXAMPLE
11+ turtle polygon 42 3
12+ . EXAMPLE
13+ turtle polygon 42 4
14+ . EXAMPLE
15+ turtle polygon 42 6
16+ . EXAMPLE
17+ turtle polygon 42 8
18+ . EXAMPLE
19+ turtle polygon 42 3.75
20+ . EXAMPLE
21+ turtle polygon 42 3.001 morph @(
22+ turtle polygon 42 3.001
23+ turtle polygon 42 4
24+ turtle polygon 42 3.001
25+ ) save ./TriangleToSquare.svg
26+
27+ #>
128param (
2- $Size = 100 ,
3- $SideCount = 6
29+ # The default size of each segment of the polygon
30+ [double ]$Size = 42 ,
31+ # The number of sides in the polygon.
32+ # If this is not a whole number, the polygon will not be closed.
33+ [double ]$SideCount = 6
434)
535
6- $null = foreach ($n in 1 .. $SideCount ) {
7- $this.Forward ($Size )
8- $this.Rotate (360 / $SideCount )
36+ # Determine the absolute side count
37+ $absSideCount = [Math ]::Abs($SideCount )
38+ # and, for each whole number between 1 and that side count
39+ $null = foreach ($n in 1 .. ([Math ]::Floor($absSideCount ))) {
40+ # Rotate and move forward
41+ $this.Rotate (360 / $SideCount ).Forward($Size )
942}
10- return $this
43+ # Determine if there was a remainder
44+ $remainder = $SideCount - [Math ]::Floor($SideCount )
45+ # If there was not, return this polygon
46+ if (-not $remainder ) { return $this }
47+ # Otherwise, we do one more partial rotation (multiplied by the remainder)
48+ # and draw one more line segment (multiplied by the remainder)
49+ # (the effect will be like watching a polygon close)
50+ return $this.Rotate ((360 / $SideCount ) * $remainder ).Forward($remainder * $Size )
51+
0 commit comments