Skip to content

Commit e43d289

Browse files
feat: Turtle.Towards() targets ( Fixes #211 )
1 parent ca6b722 commit e43d289

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

Types/Turtle/Towards.ps1

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,36 @@
44
.DESCRIPTION
55
Determines the angle from the turtle's current heading towards a point.
66
#>
7-
param(
8-
# The X-coordinate
9-
[double]$X = 0,
10-
# The Y-coordinate
11-
[double]$Y = 0
12-
)
7+
param()
8+
9+
$towards = $args
10+
11+
$tx = 0.0
12+
$ty = 0.0
13+
14+
$nCount = 0
15+
foreach ($toward in $towards) {
16+
if ($toward -is [double] -or $toward -is [float] -or $toward -is [int]) {
17+
if (-not ($nCount % 2)) {
18+
$tx = $toward
19+
} else {
20+
$ty = $toward
21+
}
22+
$nCount++
23+
}
24+
elseif ($null -ne $toward.X -and $null -ne $toward.Y) {
25+
$tx = $toward.x
26+
$ty = $toward.y
27+
$nCount+= 2
28+
}
29+
}
30+
31+
$tx/=($nCount/2)
32+
$ty/=($nCount/2)
1333

1434
# Determine the delta from the turtle's current position to the specified point
15-
$deltaX = $X - $this.Position.X
16-
$deltaY = $Y - $this.Position.Y
35+
$deltaX = $tx - $this.Position.X
36+
$deltaY = $ty - $this.Position.Y
1737
# Calculate the angle in radians and convert to degrees
1838
$angle = [Math]::Atan2($deltaY, $deltaX) * 180 / [Math]::PI
1939
# Return the angle minus the current heading (modulo 360)

0 commit comments

Comments
 (0)