-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoint_in_polygon.f90
83 lines (54 loc) · 2.15 KB
/
point_in_polygon.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
! copyright Evan. J. Gowan, 2013, 2016
!
! This file is part of countour_interpolation
!
! countour_interpolation is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, version 3 of the License
!
! countour_interpolation is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with countour_interpolation. If not, see <http://www.gnu.org/licenses/>.
!***********************************************************************************
module pip
contains
logical function point_in_polygon(x_boundary, y_boundary, x, y, number_points)
! this function determines whether a give point (x,y) is within a polygon defined by x_boundary and y_boundary
implicit none
integer, intent(in) :: number_points
double precision, intent(in) :: x, y
double precision, dimension(number_points), intent(in) :: x_boundary, y_boundary
integer :: current_point, next_point, last_point, crossover_counter
logical :: found_first, found_last, inside
found_first = .false.
found_last = .false.
inside = .false.
current_point = 1
search_boundary: do
next_point = current_point + 1
if (next_point > number_points) THEN
next_point = 1
found_last = .true.
endif
! even-odd rule algorithm to determine if the point is inside or outside
if (min(y_boundary(current_point), y_boundary(next_point)) < y .and.&
max(y_boundary(current_point), y_boundary(next_point)) >= y) THEN
if (x_boundary(current_point) + (y - y_boundary(current_point)) /&
(y_boundary(next_point) - y_boundary(current_point)) * &
(x_boundary(next_point) - x_boundary(current_point)) < x) THEN
inside = .not.(inside)
endif
endif
current_point = current_point + 1
if (found_last) THEN
exit search_boundary
endif
end do search_boundary
point_in_polygon = inside
return
end function point_in_polygon
end module pip