Skip to content

Commit 5d589a1

Browse files
committed
add more tasks
1 parent 7931244 commit 5d589a1

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

exercises/concept/tracking-turntable/.docs/hints.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22

33
Using complex numbers for rotations in 2D can leave things much cleaner and numerically more precise.
44

5-
## 1. Perform a 2D vector rotation
5+
## 1. Construct a complex number from Cartesian coordinates
66

7-
- [Euler's formula][euler] is your friend here.
87
- The inbuilt [`complex(x, y)`][complex] method is more efficient than assigning `x + im*y`.
8+
- See the introduction for a review.
9+
10+
## 2. Construct a complex number from Polar coordinates
11+
12+
- There are a few equivalent ways to do this (e.g. `exp`, `cis`, `cispi`).
13+
- See the introduction and instructions for further review.
14+
15+
## 3. Perform a 2D vector rotation
16+
17+
- [Euler's formula][euler] is your friend here and you could use your `euler` function.
918
- There are methods for retrieving the [real][real] part and [imaginary][imaginary] part of a complex number, or both!
1019

11-
## 2. Perform a radial displacement
20+
## 4. Perform a radial displacement
1221

22+
- Your `euler` function can be of use, or not...
1323
- The inbulit [`angle`][angle] method can be used to quickly find an argument from a complex number.
1424
- The [`abs`][abs] method can be used to find the magnitude of a complex number.
1525

16-
## 3. Find desired song
26+
## 5. Find the desired song
1727

1828
- This is a good opportunity to compose your `rotate` and `rdisplace` functions.
1929

exercises/concept/tracking-turntable/.docs/instructions.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,44 @@ Turndit needs to know how to find the new `(x, y)` coordinates to which the need
1414

1515
These operations can be done through trigonometric functions and/or rotation matrices, but they can be made simpler (and more fun, I assure you!) with the use of complex numbers via rotations and radial displacements.
1616

17-
This ease results from Euler's elegant formula, `ℯ^(iθ) = cos(θ) + isin(θ) = x + iy`, where `i = √-1` is the imaginary unit.
17+
This ease results from Euler's elegant formula, `ℯ^(iθ) = cos(θ) + isin(θ) = x + iy`, where `i = √-1` is the imaginary unit and `|x + iy| = 1`.
18+
With `r = |x + iy|`, we have the more general polar form of `r * ℯ^(iθ) = r * (cos(θ) + isin(θ)) = x + iy`.
19+
1820

1921
For rotations, the complex number `z = x + iy`, can be rotated an angle `θ` about the origin with a simple multiplication: `z * ℯ^(iθ)`.
2022
Note that the `x` and `y` here are just the usual coordinates on the real 2D Cartesian plane, and a positive angle results in a *counterclockwise* rotation, while a negative angle results in a *clockwise* one.
2123

2224
Likewise simply, a radial displacement `Δr` can be made by adding it to the magnitude `r` of a complex number in the polar form (eg. `z = r * ℯ^(iθ)` -> `z' = (r + Δr) * ℯ^(iθ)`).
2325
Note how the angular part stays the same and only the magnitude, `r`, is varied, as expected.
2426

25-
## 1. Perform a 2D vector rotation
27+
## 1. Construct a complex number from Cartesian coordinates
28+
29+
Implement the `z(x, y)` function which takes an `x` coordinate, a `y` coordinate from the complex plane and returns the equivalent complex number.
30+
31+
```julia-repl
32+
julia> z(1, 1)
33+
1.0 + 1.0im
34+
35+
julia> z(4.5, -7.3)
36+
4.5 - 7.3im
37+
```
38+
39+
## 2. Construct a complex number from Polar coordinates
40+
41+
Implement the `euler(r, θ)` function, which takes a radial coordinate `r`, an angle `θ` (in radians) and returns the equivalent complex number in rectangular form.
42+
43+
```julia-repl
44+
julia> euler(1, π)
45+
-1.0 + 0.0im
46+
47+
julia> euler(3, π/2)
48+
0.0 + 3.0im
49+
50+
julia> euler(2, -π/4)
51+
1.4142135623730951 - 1.414213562373095im # √2 - √2im
52+
```
53+
54+
## 3. Perform a 2D vector rotation
2655

2756
Implement the `rotate(x, y, θ)` function which takes an `x` coordinate, a `y` coordinate and an angle `θ` (in radians).
2857
The function should rotate the point about the origin by the given angle `θ` and return the new coordinates as a tuple.
@@ -34,7 +63,7 @@ julia> rotate(0, 1, π)
3463
julia> rotate(1, 1, -π/2)
3564
(1, -1)
3665
```
37-
## 2. Perform a radial displacement
66+
## 4. Perform a radial displacement
3867

3968
Implement the function `rdisplace(x, y, r)` which takes an `x` coordinate, a `y` coordinate and a radial displacement `r`.
4069
The function should displace the point along the radius by the amount `r` and return the new coordinates as a tuple.
@@ -46,7 +75,7 @@ julia> rdisplace(0, 1, 1)
4675
julia> rdisplace(1, 1, √2)
4776
(2, 2)
4877
```
49-
## 3. Find desired song
78+
## 5. Find the desired song
5079

5180
Implement a function `findsong(x, y, r, θ)` which takes the x and y coordinates of the needle as well as the radial and angular displacement between the needle and the beginning of the desired song. The new coordinates should be returned as a tuple.
5281

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
rotate(x, y, θ) = reim(complex(x, y)cispi/π))
2-
rdisplace(x, y, r) = reim((abs(complex(x, y)) + r)cispi(angle(complex(x, y)) / π))
1+
z = complex
2+
euler(r, θ) = r * cispi/ π)
3+
rotate(x, y, θ) = reim(z(x, y)euler(1, θ))
4+
rdisplace(x, y, r) = reim((abs(z(x, y)) + r)cispi(angle(z(x, y)) / π))
35
findsong(x, y, r, θ) = rotate(rdisplace(x, y, r)..., θ)

exercises/concept/tracking-turntable/runtests.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
using Test
22

3-
include("tracking-turntable.jl")
3+
#include("tracking-turntable.jl")
4+
include(".meta/exemplar.jl")
45

56
@testset verbose = true "tests" begin
7+
@testset "complex from cartesian" begin
8+
@test z(1, 1) 1 + 1im
9+
10+
@test z(4.5, -7.3) 4.5 - 7.3im
11+
12+
@test z(-π, 42.24) -π + 42.24im
13+
end
14+
15+
@testset "complex from polar" begin
16+
@test euler(1, π) -1.0 + 0.0im
17+
18+
@test euler(3, π/2) 0.0 + 3.0im
19+
20+
@test isapprox(euler(2, -π/4), 2 - 2im; atol=1e-2)
21+
22+
@test isapprox(euler(2.5, -4π/3), -1.25 + 2.165im; atol=1e-2)
23+
end
24+
625
@testset "rotations" begin
726
x, y = rotate(1, 0, π/2)
827
@test isapprox(x, 0.0; atol=1e-2) && isapprox(y, 1.0; atol=1e-2)

0 commit comments

Comments
 (0)