Skip to content

Commit

Permalink
remove code that sorts
Browse files Browse the repository at this point in the history
  • Loading branch information
mforets committed Jun 29, 2019
1 parent 464da43 commit 6ed1ec5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
32 changes: 5 additions & 27 deletions src/concrete_convex_hull.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ function convex_hull!(points::Vector{VN};
if n == 1 # dimensional check
if m == 2
# two points case in 1d
return _two_points_1d!(points, false)
return _two_points_1d!(points)
else
# general case in 1d
return _convex_hull_1d!(points)
end
elseif n == 2
if m == 2
# two points case in 2d
return _two_points_2d!(points, false)
return _two_points_2d!(points)
elseif m == 3
# three points case in 2d
return _three_points_2d!(points)
Expand All @@ -128,37 +128,15 @@ function convex_hull!(points::Vector{VN};
end
end

function _two_points_1d!(points, sort=Val(true))
p1, p2 = points[1], points[2]
if _isapprox(p1, p2) # check for redundancy
pop!(points)
elseif p1[1] > p2[1]
points[1], points[2] = p2, p1
end
return points
end

function _two_points_1d!(points, sort=Val(false))
p1, p2 = points[1], points[2]
if _isapprox(p1, p2) # check for redundancy
pop!(points)
end
return points
end

function _two_points_2d!(points, sort=Val(true))
function _two_points_1d!(points)
p1, p2 = points[1], points[2]
if isapprox(p1[1], p2[1]) && _isapprox(p1[2], p2[2]) # check for redundancy
if _isapprox(p1[1], p2[1]) # check for redundancy
pop!(points)
elseif p1 <= p2
nothing
else
points[1], points[2] = p2, p1
end
return points
end

function _two_points_2d!(points, sort=Val(false))
function _two_points_2d!(points)
p1, p2 = points[1], points[2]
if _isapprox(p1[1], p2[1]) && _isapprox(p1[2], p2[2]) # check for redundancy
pop!(points)
Expand Down
4 changes: 2 additions & 2 deletions test/unit_convex_hull.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ for N in [Float64, Rational{Int}]
# corner cases in dimension 1
@test convex_hull([Vector{N}(undef, 0)]) == [Vector{N}(undef, 0)]
@test convex_hull([[N(0)]]) == [[N(0)]]
@test convex_hull([[N(2)], [N(1)]]) == [[N(1)], [N(2)]]
@test convex_hull([[N(2)], [N(1)]]) == [[N(2)], [N(1)]]
@test convex_hull([[N(2)], [N(2)]]) == [[N(2)]]

# corner cases in dimension 2
Expand All @@ -19,7 +19,7 @@ for N in [Float64, Rational{Int}]
p2 = [1., 3.]
@test convex_hull([p1]) == [p1]
@test convex_hull([p1, p2]) == [p1, p2]
@test convex_hull([p2, p1]) == [p1, p2]
@test convex_hull([p2, p1]) == [p2, p1] # no sorting

# corner cases in higher dimension
@test convex_hull([[N(0), N(0), N(0)]]) == [[N(0), N(0), N(0)]]
Expand Down

0 comments on commit 6ed1ec5

Please sign in to comment.