Skip to content

Commit a4a0329

Browse files
authored
Added convenience Polar() constructor (#84)
1 parent 685b4f4 commit a4a0329

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/coordinatesystems.jl

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ function Polar(r, θ)
1717
return Polar{typeof(r2), typeof(θ2)}(r2, θ2)
1818
end
1919

20+
"""
21+
`Polar{T,T}(x::AbstractVector)` - 2D polar coordinates from an AbstractVector of length 2
22+
"""
23+
function Polar(x::AbstractVector)
24+
return PolarFromCartesian()(x)
25+
end
26+
2027
Base.show(io::IO, x::Polar) = print(io, "Polar(r=$(x.r), θ=$(x.θ) rad)")
2128
Base.isapprox(p1::Polar, p2::Polar; kwargs...) = isapprox(p1.r, p2.r; kwargs...) && isapprox(p1.θ, p2.θ; kwargs...)
2229

test/coordinatesystems.jl

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@
127127
@test p_from_c(collect(xy))
128128
@test c_from_p(rθ) xy
129129
end
130+
131+
@testset "Convenience Polar() constructor" begin
132+
@test typeof(Polar(SVector(1.0,2.0))) == typeof(Polar(1.0, 1.0))
133+
@test typeof(Polar(SVector(1.0f0,2.0f0))) == typeof(Polar(1.0f0, 1.0f0))
134+
@test Polar(SVector(0,2)) Polar(2/2)
135+
@test Polar(SVector(1,1)) Polar(sqrt(2),π/4)
136+
@test Polar([1,1]) Polar(sqrt(2),π/4)
137+
@test_throws "Polar transform takes a 2D coordinate" Polar([1,1,1])
138+
end
130139
end
131140

132141
@testset "3D" begin

0 commit comments

Comments
 (0)