Skip to content

Commit 99a3e20

Browse files
committed
make line intersection changes not breaking
1 parent 50c3abc commit 99a3e20

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/lines.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,21 @@ 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.
6669
6770
Note that if two points are the same, they will generate a self intersection
68-
unless they are the first and last point or part of consecutive segments.
71+
unless they are consecutive segments. (The first and last point are assumed to
72+
be shared between the first and last segment.)
6973
"""
7074
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}
7181
sections = similar(points, 0)
7282
intersections = Tuple{Int, Int}[]
7383

@@ -98,7 +108,7 @@ Splits polygon `points` into it's self intersecting parts. Only 1 intersection
98108
is handled right now.
99109
"""
100110
function split_intersections(points::AbstractVector{<:VecTypes{N, T}}) where {N, T}
101-
intersections, sections = self_intersections(points)
111+
intersections, sections = _self_intersections(points)
102112
return if isempty(intersections)
103113
return [points]
104114
elseif length(intersections) == 1 && length(sections) == 1

test/runtests.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,24 @@ 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 = 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]
291294

292295
ps = [Point2f(cos(x), sin(x)) for x in 0:4pi/5:4pi+0.1]
293-
idxs, ips = self_intersections(ps)
296+
idxs, ips = _self_intersections(ps)
294297
@test idxs == [(1, 3), (1, 4), (2, 4), (2, 5), (3, 5)]
295298
@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]
296302

297303
@test_throws ErrorException split_intersections(ps)
298304
ps = Point2f[(0,0), (1,0), (0,1), (1,1), (0, 0)]
299-
idxs, ips = self_intersections(ps)
305+
idxs, ips = _self_intersections(ps)
300306
sps = split_intersections(ps)
301307
@test sps[1] == [ps[3], ps[4], ips[1]]
302308
@test sps[2] == [ps[5], ps[1], ps[2], ips[1]]

0 commit comments

Comments
 (0)