Skip to content

Commit aa70204

Browse files
fix: Turtle.Towards relative angle ( Fixes #123 )
1 parent b517669 commit aa70204

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

Turtle.tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,33 @@ describe Turtle {
1818
$png = New-Turtle | Move-Turtle SierpinskiTriangle 15 5 | Select-Object -ExpandProperty PNG
1919
$png[1..3] -as 'char[]' -as 'string[]' -join '' | Should -Be PNG
2020
}
21+
22+
23+
context 'Turtle Directions' {
24+
it 'Can tell you the way towards a point' {
25+
$turtle = turtle
26+
$turtle.Towards(0,1) | should -be 90
27+
$turtle.Towards(1,1) | Should -be 45
28+
$turtle.Towards(1,0) | should -be 0
29+
$turtle.Towards(-1,1) | Should -be 135
30+
$turtle.Towards(-1,0) | Should -be 180
31+
$turtle.Towards(0,-1) | Should -be -90
32+
}
33+
34+
it 'Will return a relative heading' {
35+
$turtle = turtle
36+
$turtle = $turtle.Rotate($turtle.Towards(1,1))
37+
$turtle = $turtle.Forward($turtle.Distance(1,1))
38+
$turtle.Heading | Should -be 45
39+
$turtle.Position.X | Should -be 1
40+
$turtle.Position.Y | Should -be 1
41+
$turtle = $turtle.Rotate($turtle.Towards(2,2))
42+
$turtle = $turtle.Forward($turtle.Distance(2,2))
43+
$turtle.Heading | Should -be 45
44+
$turtle.Position.X | Should -be 2
45+
$turtle.Position.Y | Should -be 2
46+
}
47+
}
48+
49+
2150
}

Types/Turtle/Distance.ps1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ param(
1212
)
1313

1414
# Determine the delta from the turtle's current position to the specified point
15-
$deltaX = $this.Position.X - $X
16-
$deltaY = $this.Position.Y - $Y
15+
$deltaX = $X - $this.Position.X
16+
$deltaY = $Y - $this.Position.Y
17+
1718
# Calculate the distance using the Pythagorean theorem
18-
return [Math]::Sqrt($deltaX * $deltaX + $deltaY * $deltaY)
19+
return [Math]::Sqrt($deltaX*$deltaX + $deltaY*$deltaY)

Types/Turtle/Towards.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.SYNOPSIS
33
Determines the angle towards a point
44
.DESCRIPTION
5-
Determines the angle from the turtle's current position towards a point.
5+
Determines the angle from the turtle's current heading towards a point.
66
#>
77
param(
88
# The X-coordinate
@@ -12,9 +12,9 @@ param(
1212
)
1313

1414
# Determine the delta from the turtle's current position to the specified point
15-
$deltaX = $this.Position.X - $X
16-
$deltaY = $this.Position.Y - $Y
15+
$deltaX = $X - $this.Position.X
16+
$deltaY = $Y - $this.Position.Y
1717
# Calculate the angle in radians and convert to degrees
1818
$angle = [Math]::Atan2($deltaY, $deltaX) * 180 / [Math]::PI
19-
# Return the angle rotate by 90 to account for the turtle's coordinate system
20-
return $angle + 90
19+
# Return the angle minus the current heading (modulo 360)
20+
return $angle - ($this.Heading % 360)

0 commit comments

Comments
 (0)