Skip to content

Commit

Permalink
projection of Line2D
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Feb 26, 2021
1 parent d306dc8 commit 37ce92c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Sets/Line2D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,29 @@ function translate(L::Line2D, v::AbstractVector; share::Bool=false)
b = L.b + dot(L.a, v)
return Line2D(a, b)
end

function project(L::Line2D{N}, block::AbstractVector{Int}) where {N}
m = length(block)
if m == 2
@assert ispermutation(block, 1:2) "invalid dimensions $block for projection"
return L # no projection
elseif m == 1
# projection to dimension i
cdims = constrained_dimensions(L)
if length(cdims) == 1
@inbounds if cdims[1] == block[1]
# L: aᵢxᵢ = b where aᵢ ≠ 0
return Singleton([L.b / L.a[cdims[1]]])
else
# L: aⱼxⱼ = b where i ≠ j
return Universe{N}(1)
end
else
# L is constrained in both dimensions
@assert length(cdims) == 2
return Universe{N}(1)
end
else
throw(ArgumentError("cannot project a two-dimensional line to $m dimensions"))
end
end
9 changes: 9 additions & 0 deletions test/unit_Line2D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ for N in [Float64, Rational{Int}, Float32]
@test lm isa Line2D{N, Vector{N}}
@test lm.a N[1/2, -2] && lm.b N(0)

# projection
L = Line2D(N[1, -1], N(0)) # x = y
@test project(L, [1]) == project(L, [2]) == Universe{N}(1)
@test project(L, [1, 2]) == L
L = Line2D(N[2, 0], N(4)) # x = 2
@test project(L, [1]) == Singleton(N[2])
@test project(L, [2]) == Universe{N}(1)
@test project(L, [1, 2]) == L

# translation
@test translate(l1, N[1, 2]) == Line2D(a1, N(3))
end

0 comments on commit 37ce92c

Please sign in to comment.