Skip to content

Commit cada7d8

Browse files
feat: Turtle.RightTriangle ( Fixes #367 )
1 parent d335285 commit cada7d8

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

Types/Turtle/RightTriangle.ps1

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<#
2+
.SYNOPSIS
3+
Right triangle
4+
.DESCRIPTION
5+
Draws a right triangle using two side lengths.
6+
.EXAMPLE
7+
# Draw a random right triangle
8+
turtle RightTriangle
9+
.EXAMPLE
10+
# Draw a 3-4-5 right triangle
11+
turtle RightTriangle 3 4
12+
.EXAMPLE
13+
# We can rotate after each right triangle
14+
turtle @('RightTriangle',1,4,'Rotate', 90 * 4)
15+
.EXAMPLE
16+
# We can make a parallax by putting with multiple right triangles
17+
turtle id Parallax @(foreach ($n in 1..10) {
18+
'RightTriangle',$n,-10
19+
'RightTriangle',($n*-1),-10
20+
}) save ./Parallax.svg
21+
.EXAMPLE
22+
# We can do this along two axes to make a parallax illusion
23+
turtle @(foreach ($n in 1..10) {
24+
'RightTriangle',$n,(10)
25+
'RightTriangle',($n),-10
26+
'RightTriangle',($n * -1),-10
27+
'RightTriangle',($n * -1),10
28+
}) save ./ParallaxIllusion.svg
29+
.EXAMPLE
30+
# We can create a parallax corner by scaling the triangles
31+
turtle id ParallaxCorner @(foreach ($n in 1..10) {
32+
'RightTriangle',(10 - $n),$n
33+
'RightTriangle',$n,(10-$n)
34+
}) save ./ParallaxCorner.svg
35+
.EXAMPLE
36+
# We can rotate and repeat this to make a Parallax Astroid
37+
turtle id ParallaxAstroid (@(
38+
@(foreach ($n in 1..10) {
39+
'RightTriangle',(10 - $n),$n
40+
'RightTriangle',$n,(10-$n)
41+
})
42+
'rotate', 90
43+
) * 4) save ./ParallaxAstroid.svg
44+
.EXAMPLE
45+
# We can make a pair of parallax astroids and morph them.
46+
$parallaxAstroid = turtle id ParallaxAstroid (
47+
@(
48+
foreach ($n in 1..10) {
49+
'RightTriangle',(10 - $n),$n
50+
'RightTriangle',$n,(10-$n)
51+
}
52+
'rotate', 90
53+
) * 4
54+
)
55+
56+
$parallaxAstroid2 = turtle id ParallaxAstroid (
57+
@(
58+
foreach ($n in 1..10) {
59+
'RightTriangle',(10 - $n),($n*-1)
60+
'RightTriangle',($n*-1),(10-$n)
61+
}
62+
'rotate', 90
63+
) * 4
64+
)
65+
66+
$parallaxAstroid | turtle morph @(
67+
$parallaxAstroid
68+
$parallaxAstroid2
69+
$parallaxAstroid
70+
) @(
71+
'stroke','#4488ff'
72+
'fill','#224488'
73+
'pathclass','foreground-stroke foreground-fill'
74+
'fillrule','evenodd'
75+
)
76+
#>
77+
param(
78+
# The distance along the current angle.
79+
[Alias('Opposite')]
80+
[double]
81+
$Side1 = $(
82+
(Get-Random -Minimum 42 -Maximum 84) * (1,-1 | Get-Random)
83+
),
84+
85+
# The distance along the adjacent angle.
86+
[Alias('Adjacent')]
87+
[double]
88+
$Side2 = $(
89+
(Get-Random -Minimum 42 -Maximum 84) * (1,-1 | Get-Random)
90+
)
91+
)
92+
93+
# If neither side is a number, return ourself
94+
if (-not $side1 -and -not $side2) { return $this }
95+
96+
# If one side is negative and the other is positive
97+
<#if (($side1 * $side2) -lt 0) {
98+
# flip the other side so they match.
99+
$side2 = $Side2 * -1
100+
} #>
101+
102+
# Figure out our angles with a bit of trigonometry.
103+
# (SOHCAHTOA)
104+
$angle1 = [Math]::Atan2($side2, $Side1) * (180/[Math]::Pi)
105+
$angle2 = [Math]::Atan2($side1, $side2) * (180/[Math]::Pi)
106+
107+
108+
# Figure out our hypotenuse (Pythagorean Theorem)
109+
$hypotenuse = [Math]::Sqrt(
110+
($Side1 * $side1) +
111+
($Side2 * $side2)
112+
)
113+
114+
# Now draw our triangle
115+
return $this.
116+
# move forward along our current side
117+
Forward($side1).
118+
# turn back around and then turn by our angle.
119+
Rotate(180 - $angle1).
120+
# draw the hypotenuse
121+
Forward($hypotenuse).
122+
# turn back around and then turn by our other angle
123+
Rotate(180 - $angle2).
124+
Forward($side2).
125+
Rotate(-270)
126+
127+
128+
129+
130+
131+

0 commit comments

Comments
 (0)