Skip to content

Commit d4816c0

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Turtle.Polygon() partial support ( Fixes #194 )
1 parent a4891a6 commit d4816c0

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

Turtle.types.ps1xml

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,16 +1389,58 @@ return $this
13891389
<ScriptMethod>
13901390
<Name>Polygon</Name>
13911391
<Script>
1392-
param(
1393-
$Size = 100,
1394-
$SideCount = 6
1392+
&lt;#
1393+
.SYNOPSIS
1394+
Draws a polygon
1395+
.DESCRIPTION
1396+
Draws a regular polygon with N sides.
1397+
1398+
To draw a closed polygon, provide a whole number of sides.
1399+
1400+
To draw an open polygon, provide a fractial number of sides.
1401+
.EXAMPLE
1402+
turtle polygon 42 3
1403+
.EXAMPLE
1404+
turtle polygon 42 4
1405+
.EXAMPLE
1406+
turtle polygon 42 6
1407+
.EXAMPLE
1408+
turtle polygon 42 8
1409+
.EXAMPLE
1410+
turtle polygon 42 3.75
1411+
.EXAMPLE
1412+
turtle polygon 42 3.001 morph @(
1413+
turtle polygon 42 3.001
1414+
turtle polygon 42 4
1415+
turtle polygon 42 3.001
1416+
) save ./TriangleToSquare.svg
1417+
1418+
#&gt;
1419+
param(
1420+
# The default size of each segment of the polygon
1421+
[double]$Size = 42,
1422+
# The number of sides in the polygon.
1423+
# If this is not a whole number, the polygon will not be closed.
1424+
[double]$SideCount = 6
13951425
)
13961426

1397-
$null = foreach ($n in 1..$SideCount) {
1398-
$this.Forward($Size)
1399-
$this.Rotate(360 / $SideCount)
1427+
# Determine the absolute side count
1428+
$absSideCount = [Math]::Abs($SideCount)
1429+
# and, for each whole number between 1 and that side count
1430+
$null = foreach ($n in 1..([Math]::Floor($absSideCount))) {
1431+
# Rotate and move forward
1432+
$this.Rotate(360 / $SideCount).Forward($Size)
14001433
}
1401-
return $this
1434+
# Determine if there was a remainder
1435+
$remainder = $SideCount - [Math]::Floor($SideCount)
1436+
# If there was not, return this polygon
1437+
if (-not $remainder) { return $this }
1438+
# Otherwise, we do one more partial rotation (multiplied by the remainder)
1439+
# and draw one more line segment (multiplied by the remainder)
1440+
# (the effect will be like watching a polygon close)
1441+
return $this.Rotate((360 / $SideCount) * $remainder).Forward($remainder * $Size)
1442+
1443+
14021444
</Script>
14031445
</ScriptMethod>
14041446
<ScriptMethod>

0 commit comments

Comments
 (0)