Skip to content

Commit 1a1f341

Browse files
committed
change self_intersections to return tuple vector
1 parent 14f3ce4 commit 1a1f341

File tree

2 files changed

+6
-19
lines changed

2 files changed

+6
-19
lines changed

src/lines.jl

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,14 @@ end
6363
self_intersections(points::AbstractVector{<:Point})
6464
6565
Finds all self intersections of in a continuous line described by `points`.
66-
Returns a Vector of indices where each pair `v[2i], v[2i+1]` refers two
67-
intersecting line segments by their first point, and a Vector of intersection
68-
points.
66+
Returns a Vector of index tuples corresponding to the two intersecting line
67+
segments by their first point, and a Vector of intersection points.
6968
7069
Note that if two points are the same, they will generate a self intersection
7170
unless they are consecutive segments. (The first and last point are assumed to
7271
be shared between the first and last segment.)
7372
"""
7473
function self_intersections(points::AbstractVector{<:VecTypes{D, T}}) where {D, T}
75-
ti, sections = _self_intersections(points)
76-
# convert array of tuples to flat array
77-
return [x for t in ti for x in t], sections
78-
end
79-
80-
function _self_intersections(points::AbstractVector{<:VecTypes{D, T}}) where {D, T}
8174
sections = similar(points, 0)
8275
intersections = Tuple{Int, Int}[]
8376

@@ -108,7 +101,7 @@ Splits polygon `points` into it's self intersecting parts. Only 1 intersection
108101
is handled right now.
109102
"""
110103
function split_intersections(points::AbstractVector{<:VecTypes{N, T}}) where {N, T}
111-
intersections, sections = _self_intersections(points)
104+
intersections, sections = self_intersections(points)
112105
return if isempty(intersections)
113106
return [points]
114107
elseif length(intersections) == 1 && length(sections) == 1

test/runtests.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,24 +285,18 @@ end
285285
@test collect(GeometryBasics.consecutive_pairs(ps)) == collect(zip(ps[1:end-1], ps[2:end]))
286286

287287
ps = Point2f[(0,0), (1,0), (0,1), (1,2), (0,2), (1,1), (0,0)]
288-
idxs, ips = GeometryBasics._self_intersections(ps)
288+
idxs, ips = self_intersections(ps)
289289
@test idxs == [(2, 6), (3, 5)]
290290
@test ips == [Point2f(0.5), Point2f(0.5, 1.5)]
291-
idxs2, ips2 = self_intersections(ps)
292-
@test ips2 == ips
293-
@test idxs2 == [2, 6, 3, 5]
294291

295292
ps = [Point2f(cos(x), sin(x)) for x in 0:4pi/5:4pi+0.1]
296-
idxs, ips = GeometryBasics._self_intersections(ps)
293+
idxs, ips = self_intersections(ps)
297294
@test idxs == [(1, 3), (1, 4), (2, 4), (2, 5), (3, 5)]
298295
@test all(ips .≈ Point2f[(0.30901694, 0.2245140), (-0.118034005, 0.36327127), (-0.38196602, 0), (-0.118033946, -0.3632713), (0.309017, -0.22451389)])
299-
idxs2, ips2 = self_intersections(ps)
300-
@test ips2 == ips
301-
@test idxs2 == [1, 3, 1, 4, 2, 4, 2, 5, 3, 5]
302296

303297
@test_throws ErrorException split_intersections(ps)
304298
ps = Point2f[(0,0), (1,0), (0,1), (1,1), (0, 0)]
305-
idxs, ips = GeometryBasics._self_intersections(ps)
299+
idxs, ips = self_intersections(ps)
306300
sps = split_intersections(ps)
307301
@test sps[1] == [ps[3], ps[4], ips[1]]
308302
@test sps[2] == [ps[5], ps[1], ps[2], ips[1]]

0 commit comments

Comments
 (0)