Skip to content

Commit f686feb

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Turtle.ArcRight StepCount ( Fixes #272 )
1 parent 0190bf3 commit f686feb

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

Turtle.types.ps1xml

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ $Radius = 10,
304304

305305
# The angle of the arc
306306
[double]
307-
$Angle = 60
307+
$Angle = 60,
308+
309+
# The number of steps. If not provided, will default to half of the angle.
310+
[int]
311+
$StepCount
308312
)
309313

310314
# Determine the absolute angle, for this operation
@@ -315,25 +319,32 @@ if ($absAngle -eq 0) { return $this }
315319
# Determine the circumference of a circle of this radius
316320
$Circumference = ((2 * $Radius) * [Math]::PI)
317321

318-
# Clamp the angle, as arcs beyond 360 just continue to circle
319-
$ClampedAngle =
320-
if ($absAngle -gt 360) { 360 }
321-
elseif ($absAngle -lt -360) { -360}
322-
else { $absAngle }
323-
# The circumference step is the circumference divided by our clamped angle
324-
$CircumferenceStep = $Circumference / [Math]::Floor($ClampedAngle)
322+
# The circumference step is the circumference times
323+
# the number of revolutions
324+
$revolutionCount = $angle/360
325+
# divided by the angle
326+
$CircumferenceStep = ($Circumference * $revolutionCount) / $Angle
327+
325328
# The iteration is as close to one or negative one as possible
326329
$iteration = $angle / [Math]::Floor($absAngle)
327-
# Start off at iteration 1
328-
$angleDelta = $iteration
329-
# while we have not reached the angle
330-
while ([Math]::Abs($angleDelta) -le $absAngle) {
331-
# Rotate and move forward
332-
$null = $this.Rotate($iteration).Forward($CircumferenceStep)
333-
$angleDelta+=$iteration
330+
331+
# If we have no step count
332+
if (-not $StepCount) {
333+
# default to half of the angle.
334+
$StepCount = [Math]::Round($absAngle / 2)
335+
}
336+
# Turn this into a ratio (by default, this ratio would be `2`).
337+
$stepSize = $absAngle / $StepCount
338+
339+
# Starting at zero, keep turning until we have reached the number.
340+
# Increase our angle by iteration * stepSize each time.
341+
for ($angleDelta = 0; [Math]::Abs($angleDelta) -lt $absAngle; $angleDelta+=($iteration*$stepSize)) {
342+
$this = $this. # In each step,
343+
Forward($CircumferenceStep*$StepSize). # move forward a fraction of the circumference,
344+
Rotate($iteration*$StepSize) # and rotate a fraction of the total angle.
334345
}
335346

336-
# Return this so we can keep the chain.
347+
# When we are done, return $this so we never break the chain.
337348
return $this
338349
</Script>
339350
</ScriptMethod>

0 commit comments

Comments
 (0)