@@ -1389,16 +1389,58 @@ return $this
1389
1389
<ScriptMethod >
1390
1390
<Name >Polygon</Name >
1391
1391
<Script >
1392
- param(
1393
- $Size = 100,
1394
- $SideCount = 6
1392
+ < #
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
+ #>
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
1395
1425
)
1396
1426
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)
1400
1433
}
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
+
1402
1444
</Script >
1403
1445
</ScriptMethod >
1404
1446
<ScriptMethod >
0 commit comments