Skip to content

Commit 54adb46

Browse files
authored
Merge pull request #86 from ArneSpang/main
small update to inPolygon
2 parents 951d924 + bacd12f commit 54adb46

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/utils.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,19 +1650,16 @@ Checks if points given by matrices `X` and `Y` are in or on (both cases return t
16501650
16511651
"""
16521652
function inPolygon!(INSIDE::Matrix{Bool}, PolyX::Vector{T}, PolyY::Vector{T}, X::Matrix{T}, Y::Matrix{T}; fast=false) where T <: Real
1653-
iSteps = collect(eachindex(PolyX))
1654-
jSteps = [length(PolyX); collect(1:length(PolyX)-1)]
1655-
16561653
if fast
16571654
for j = 1 : size(X, 2)
16581655
for i = 1 : size(X, 1)
1659-
INSIDE[i,j] = inPolyPointF(PolyX, PolyY, X[i,j], Y[i,j], iSteps, jSteps)
1656+
INSIDE[i,j] = inPolyPointF(PolyX, PolyY, X[i,j], Y[i,j])
16601657
end
16611658
end
16621659
else
16631660
for j = 1 : size(X, 2)
16641661
for i = 1 : size(X, 1)
1665-
INSIDE[i,j] = (inPolyPoint(PolyX, PolyY, X[i,j], Y[i,j], iSteps, jSteps) || inPolyPoint(PolyY, PolyX, Y[i,j], X[i,j], iSteps, jSteps))
1662+
INSIDE[i,j] = (inPolyPoint(PolyX, PolyY, X[i,j], Y[i,j]) || inPolyPoint(PolyY, PolyX, Y[i,j], X[i,j]))
16661663
end
16671664
end
16681665
end
@@ -1675,16 +1672,13 @@ Same as above but `inside`, `X` and `Y` and are vectors.
16751672
16761673
"""
16771674
function inPolygon!(inside::Vector{Bool}, PolyX::Vector{T}, PolyY::Vector{T}, x::Vector{T}, y::Vector{T}; fast=false) where T <: Real
1678-
iSteps = collect(eachindex(PolyX))
1679-
jSteps = [length(PolyX); collect(1:length(PolyX)-1)]
1680-
16811675
if fast
16821676
for i = eachindex(x)
1683-
inside[i] = inPolyPointF(PolyX, PolyY, x[i], y[i], iSteps, jSteps)
1677+
inside[i] = inPolyPointF(PolyX, PolyY, x[i], y[i])
16841678
end
16851679
else
16861680
for i = eachindex(x)
1687-
inside[i] = (inPolyPoint(PolyX, PolyY, x[i], y[i], iSteps, jSteps) || inPolyPoint(PolyY, PolyX, y[i], x[i], iSteps, jSteps))
1681+
inside[i] = (inPolyPoint(PolyX, PolyY, x[i], y[i]) || inPolyPoint(PolyY, PolyX, y[i], x[i]))
16881682
end
16891683
end
16901684
end
@@ -1695,9 +1689,13 @@ end
16951689
Checks if a point given by x and y is in or on (both cases return true) a polygon given by PolyX and PolyY, iSteps and jSteps provide the connectivity between the polygon edges. This function should be used through inPolygon!().
16961690
16971691
"""
1698-
function inPolyPoint(PolyX::Vector{T}, PolyY::Vector{T}, x::T, y::T, iSteps::Vector{Int64}, jSteps::Vector{Int64}) where T <: Real
1692+
function inPolyPoint(PolyX::Vector{T}, PolyY::Vector{T}, x::T, y::T) where T <: Real
16991693
inside1, inside2, inside3, inside4 = false, false, false, false
1700-
for (i,j) in zip(iSteps, jSteps)
1694+
n = length(PolyX)
1695+
for i in eachindex(PolyX)
1696+
j = i-1
1697+
j += n * (j<1)
1698+
xi = PolyX[i]
17011699
xi = PolyX[i]
17021700
yi = PolyY[i]
17031701
xj = PolyX[j]
@@ -1730,9 +1728,12 @@ end
17301728
Faster version of inPolyPoint() but will miss some points that are on the edge of the polygon.
17311729
17321730
"""
1733-
function inPolyPointF(PolyX::Vector{T}, PolyY::Vector{T}, x::T, y::T, iSteps::Vector{Int64}, jSteps::Vector{Int64}) where T <: Real
1731+
function inPolyPointF(PolyX::Vector{T}, PolyY::Vector{T}, x::T, y::T) where T <: Real
17341732
inside = false
1735-
for (i,j) in zip(iSteps, jSteps)
1733+
n = length(PolyX)
1734+
for i in eachindex(PolyX)
1735+
j = i-1
1736+
j += n * (j<1)
17361737
xi = PolyX[i]
17371738
yi = PolyY[i]
17381739
xj = PolyX[j]

test/test_utils.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,9 @@ yN = zeros(Bool, size(X))
250250
inPolygon!(yN, PolyX, PolyY, X, Y, fast=true)
251251
@test sum(yN) == 194
252252
inPolygon!(yN, PolyX, PolyY, X, Y)
253+
@test sum(yN) == 217
254+
X, Y, yN = X[:], Y[:], yN[:]
255+
inPolygon!(yN, PolyX, PolyY, X, Y, fast=true)
256+
@test sum(yN) == 194
257+
inPolygon!(yN, PolyX, PolyY, X, Y)
253258
@test sum(yN) == 217

0 commit comments

Comments
 (0)