Skip to content

Commit e134391

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Turtle.Morph() ( Fixes #154 )
1 parent 6afc516 commit e134391

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

Turtle.types.ps1xml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,107 @@ return $this.LSystem(
10821082

10831083
</Script>
10841084
</ScriptMethod>
1085+
<ScriptMethod>
1086+
<Name>Morph</Name>
1087+
<Script>
1088+
&lt;#
1089+
.SYNOPSIS
1090+
Morphs a Turtle
1091+
.DESCRIPTION
1092+
Morphs a Turtle by animating its path.
1093+
1094+
Any two paths with the same number of points can be morphed into each other.
1095+
.EXAMPLE
1096+
$sierpinskiTriangle = turtle SierpinskiTriangle 42 4
1097+
$SierpinskiTriangleFlipped = turtle rotate 180 SierpinskiTriangle 42 4
1098+
turtle SierpinskiTriangle 42 4 morph (
1099+
$SierpinskiTriangle,
1100+
$SierpinskiTriangleFlipped,
1101+
$sierpinskiTriangle
1102+
) save ./SierpinskiTriangleFlip.svg
1103+
.EXAMPLE
1104+
$sideCount = (3..24 | Get-Random )
1105+
$stepCount = 36
1106+
1107+
$flower = turtle rotate ((Get-Random -Max 180) * -1) flower 42 10 $sideCount $stepCount
1108+
$flower2 = turtle rotate ((Get-Random -Max 180)) flower 42 50 $sideCount $stepCount
1109+
$flower3 = turtle rotate ((Get-Random -Max 90)) flower 42 20 $sideCount $stepCount
1110+
turtle flower 42 10 $sideCount $stepCount duration ($sideCount * 3) morph ($flower, $flower2,$flower) |
1111+
save-turtle ./flowerMorph.svg Pattern
1112+
.EXAMPLE
1113+
$flowerAngle = (40..60 | Get-Random )
1114+
$stepCount = 36
1115+
$radius = 23..42 | Get-Random
1116+
1117+
$flowerPetals = turtle rotate ((Get-Random -Max 180) * -1) flowerPetal $radius 10 $flowerAngle $stepCount
1118+
$flowerPetals3 = turtle rotate ((Get-Random -Max 180)) flowerPetal $radius 40 $flowerAngle $stepCount
1119+
turtle flowerPetal $radius 10 $flowerAngle $stepCount duration $radius morph (
1120+
$flowerPetals,
1121+
$flowerPetals3,
1122+
$flowerPetals
1123+
) | Save-Turtle ./flowerPetalMorph.svg Pattern
1124+
#&gt;
1125+
param(
1126+
[Parameter(ValueFromRemainingArguments)]
1127+
$Arguments
1128+
)
1129+
1130+
$durationArgument = $null
1131+
1132+
$newPaths = @(foreach ($arg in $Arguments) {
1133+
if ($arg -is [string]) {
1134+
if ($arg -match '^\s{0,}m') {
1135+
$arg
1136+
}
1137+
} elseif ($arg.PathData) {
1138+
$arg.PathData
1139+
} elseif ($arg.D) {
1140+
$arg.D
1141+
} elseif ($arg -is [TimeSpan]) {
1142+
$durationArgument = $arg
1143+
}
1144+
elseif ($arg -is [double] -or $arg -is [int]) {
1145+
$durationArgument = [TimeSpan]::FromSeconds($arg)
1146+
}
1147+
})
1148+
1149+
if (-not $newPaths) {
1150+
return $this
1151+
&lt;#$pathSegments = @($this.PathData -split '(?=\p{L})')
1152+
$newPaths = @(for ($segmentNumber = 0; $segmentNumber -lt $pathSegments.Count; $segmentNumber++) {
1153+
$pathSegments[0..$segmentNumber] -join ' '
1154+
}) -join ';'#&gt;
1155+
}
1156+
1157+
if ($this.PathAnimation) {
1158+
$updatedAnimations =
1159+
@(foreach ($animationXML in $this.PathAnimation -split '(?&lt;=/&gt;)') {
1160+
$animationXML = $animationXML -as [xml]
1161+
if (-not $animationXML) { continue }
1162+
if ($animationXML.attributeName -eq 'd') {
1163+
$animationXML.values = "$($newPaths -join ';')"
1164+
}
1165+
$animationXML.OuterXml
1166+
})
1167+
$this.PathAnimation = $updatedAnimations
1168+
} else {
1169+
$this.PathAnimation += [Ordered]@{
1170+
attributeName = 'd' ; values = "$($newPaths -join ';')" ; repeatCount = 'indefinite'; dur = $(
1171+
if ($durationArgument) {
1172+
"$($durationArgument.TotalSeconds)s"
1173+
} elseif ($this.Duration) {
1174+
"$($this.Duration.TotalSeconds)s"
1175+
} else {
1176+
"4.2s"
1177+
}
1178+
1179+
)
1180+
}
1181+
}
1182+
1183+
return $this
1184+
</Script>
1185+
</ScriptMethod>
10851186
<ScriptMethod>
10861187
<Name>PeanoCurve</Name>
10871188
<Script>

0 commit comments

Comments
 (0)