Skip to content

Commit a3fea07

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Turtle.get_History ( Fixes PoshWeb#279 )
1 parent ab8840d commit a3fea07

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed

Turtle.types.ps1xml

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4277,6 +4277,225 @@ $this.ViewBox = $viewBox[0],$viewBox[1],$viewbox[-2], $height
42774277

42784278
</SetScriptBlock>
42794279
</ScriptProperty>
4280+
<ScriptProperty>
4281+
<Name>History</Name>
4282+
<GetScriptBlock>
4283+
&lt;#
4284+
.SYNOPSIS
4285+
Gets a Turtle's history
4286+
.DESCRIPTION
4287+
Gets an annotated history of a turtle's movements.
4288+
4289+
This is an SVG path translated into back into human readable text and coordinates.
4290+
#&gt;
4291+
$currentPosition = [Numerics.Vector2]::new(0,0)
4292+
$historyList = [Collections.Generic.List[PSObject]]::new()
4293+
$startStack = [Collections.Stack]::new()
4294+
foreach ($pathStep in $this.PathData -join ' ' -split '(?=[\p{L}-[E]])' -ne '') {
4295+
$letter = $pathStep[0]
4296+
$isUpper = "$letter".ToLower() -cne $letter
4297+
$isLower = -not $isUpper
4298+
$toBy = if ($isUpper) { 'to'} else { 'by'}
4299+
$stepPoints = $pathStep -replace $letter -replace ',', ' ' -split '\s{1,}' -ne '' -as [float[]]
4300+
4301+
$historyEntry =
4302+
switch ($letter) {
4303+
a {
4304+
for ($stepIndex = 0; $stepIndex -lt $stepPoints.Length; $stepIndex+=7) {
4305+
$sequence = $stepPoints[$stepIndex..($stepIndex + 6)]
4306+
$comment = "arc $toBy $sequence"
4307+
$delta = [Numerics.Vector2]::new.Invoke($sequence[-2,-1])
4308+
if ($isUpper) { $delta -= $currentPosition }
4309+
[PSCustomObject]@{
4310+
PSTypeName='Turtle.History'
4311+
Letter = "$letter"
4312+
Start = $currentPosition
4313+
End = $currentPosition + $delta
4314+
Delta = $delta
4315+
Instruction = "$Letter $sequence"
4316+
Comment = $comment
4317+
}
4318+
$currentPosition += $delta
4319+
}
4320+
}
4321+
c {
4322+
4323+
for ($stepIndex = 0; $stepIndex -lt $stepPoints.Length; $stepIndex+=6) {
4324+
$sequence = $stepPoints[$stepIndex..($stepIndex + 5)]
4325+
$comment = "cubic curve $toBy $sequence"
4326+
$delta = [Numerics.Vector2]::new.Invoke($sequence[-2,-1])
4327+
if ($isUpper) { $delta -= $currentPosition }
4328+
[PSCustomObject]@{
4329+
PSTypeName='Turtle.History'
4330+
Letter = "$letter"
4331+
Start = $currentPosition
4332+
End = $currentPosition + $delta
4333+
Delta = $delta
4334+
Instruction = "$Letter $sequence"
4335+
Comment = $comment
4336+
}
4337+
$currentPosition += $delta
4338+
}
4339+
}
4340+
l {
4341+
# line segment
4342+
for ($stepIndex = 0; $stepIndex -lt $stepPoints.Length; $stepIndex+=2) {
4343+
$sequence = $stepPoints[$stepIndex..($stepIndex + 1)]
4344+
$comment = "line $toBy $sequence"
4345+
$delta = [Numerics.Vector2]::new.Invoke($sequence[-2,-1])
4346+
if ($isUpper) { $delta -= $currentPosition }
4347+
[PSCustomObject]@{
4348+
PSTypeName='Turtle.History'
4349+
Letter = "$letter"
4350+
Start = $currentPosition
4351+
End = $currentPosition + $delta
4352+
Delta = $delta
4353+
Instruction = "$Letter $sequence"
4354+
Comment = $comment
4355+
}
4356+
$currentPosition += $delta
4357+
}
4358+
}
4359+
m {
4360+
# movement
4361+
for ($stepIndex = 0; $stepIndex -lt $stepPoints.Length; $stepIndex+=2) {
4362+
$sequence = $stepPoints[$stepIndex..($stepIndex + 1)]
4363+
4364+
$comment = "line $toBy $sequence"
4365+
4366+
$delta = [Numerics.Vector2]::new.Invoke($sequence[-2,-1])
4367+
4368+
if ($isUpper) { $delta -= $currentPosition }
4369+
4370+
if ($stepIndex -gt 0) {
4371+
if ($letter -eq 'm') {
4372+
if ($isUpper) { $letter = 'L' }
4373+
else { $letter = 'l'}
4374+
}
4375+
$comment = "line $toBy $sequence"
4376+
} else {
4377+
$comment = "move $toBy $sequence"
4378+
$startStack.Push($currentPosition + $delta)
4379+
}
4380+
4381+
[PSCustomObject]@{
4382+
PSTypeName='Turtle.History'
4383+
Letter = "$letter"
4384+
Start = $currentPosition
4385+
End = $currentPosition + $delta
4386+
Delta = $delta
4387+
Instruction = "$Letter $sequence"
4388+
Comment = $comment
4389+
}
4390+
$currentPosition += $delta
4391+
}
4392+
}
4393+
s {
4394+
# simple bezier curve
4395+
for ($stepIndex = 0; $stepIndex -lt $stepPoints.Length; $stepIndex+=4) {
4396+
$sequence = $stepPoints[$stepIndex..($stepIndex + 3)]
4397+
$comment = "simple bezier curve $toBy $sequence"
4398+
$delta = [Numerics.Vector2]::new.Invoke($sequence[-2,-1])
4399+
if ($isUpper) { $delta -= $currentPosition }
4400+
[PSCustomObject]@{
4401+
PSTypeName='Turtle.History'
4402+
Letter = "$letter"
4403+
Start = $currentPosition
4404+
End = $currentPosition + $delta
4405+
Delta = $delta
4406+
Instruction = "$Letter $sequence"
4407+
Comment = $comment
4408+
}
4409+
$currentPosition += $delta
4410+
}
4411+
}
4412+
t {
4413+
# continue simple bezier curve
4414+
for ($stepIndex = 0; $stepIndex -lt $stepPoints.Length; $stepIndex+=2) {
4415+
$sequence = $stepPoints[$stepIndex..($stepIndex + 1)]
4416+
$comment = "continue bezier curve $toBy $sequence"
4417+
$delta = [Numerics.Vector2]::new.Invoke($sequence[-2,-1])
4418+
if ($isUpper) { $delta -= $currentPosition }
4419+
[PSCustomObject]@{
4420+
PSTypeName='Turtle.History'
4421+
Letter = "$letter"
4422+
Start = $currentPosition
4423+
End = $currentPosition + $delta
4424+
Delta = $delta
4425+
Instruction = "$Letter $sequence"
4426+
Comment = $comment
4427+
}
4428+
$currentPosition += $delta
4429+
}
4430+
}
4431+
q {
4432+
for ($stepIndex = 0; $stepIndex -lt $stepPoints.Length; $stepIndex+=4) {
4433+
4434+
$sequence = $stepPoints[$stepIndex..($stepIndex + 3)]
4435+
$comment = "quadratic bezier curve $toBy $sequence"
4436+
$delta = [Numerics.Vector2]::new.Invoke($sequence[-2,-1])
4437+
if ($isUpper) { $delta -= $currentPosition }
4438+
[PSCustomObject]@{
4439+
PSTypeName='Turtle.History'
4440+
Letter = "$letter"
4441+
Start = $currentPosition
4442+
End = $currentPosition + $delta
4443+
Delta = $delta
4444+
Instruction = "$Letter $sequence"
4445+
Comment = $comment
4446+
}
4447+
$currentPosition += $delta
4448+
}
4449+
}
4450+
{ $_ -in 'h', 'v' } {
4451+
for ($stepIndex = 0; $stepIndex -lt $stepPoints.Length; $stepIndex++) {
4452+
$sequence = $stepPoints[$stepIndex..$stepIndex]
4453+
$comment = "$(
4454+
if ($letter -eq 'v') { 'vertical' } else {'horizontal'}
4455+
) line $toBy $sequence"
4456+
$delta =
4457+
if ($letter -eq 'v') {
4458+
[Numerics.Vector2]::new(0, $sequence[0])
4459+
} else {
4460+
[Numerics.Vector2]::new($sequence[0], 0)
4461+
}
4462+
if ($isUpper) { $delta -= $currentPosition }
4463+
[PSCustomObject]@{
4464+
PSTypeName='Turtle.History'
4465+
Letter = "$letter"
4466+
Start = $currentPosition
4467+
End = $currentPosition + $delta
4468+
Delta = $delta
4469+
Instruction = "$Letter $sequence"
4470+
Comment = $comment
4471+
}
4472+
$currentPosition += $delta
4473+
}
4474+
}
4475+
z {
4476+
$closePosition = $startStack.Pop()
4477+
$delta = $closePosition - $currentPosition
4478+
[PSCustomObject]@{
4479+
PSTypeName='Turtle.History'
4480+
Letter = "$letter"
4481+
Start = $currentPosition
4482+
End = $currentPosition + $delta
4483+
Delta = $delta
4484+
Instruction = "$Letter"
4485+
Comment = "close path"
4486+
}
4487+
$currentPosition += $delta
4488+
4489+
}
4490+
}
4491+
4492+
$historyList.Add($historyEntry)
4493+
}
4494+
4495+
4496+
return $historyList
4497+
</GetScriptBlock>
4498+
</ScriptProperty>
42804499
<ScriptProperty>
42814500
<Name>ID</Name>
42824501
<GetScriptBlock>

0 commit comments

Comments
 (0)