/
polyline_monotone.R
67 lines (66 loc) · 1.76 KB
/
polyline_monotone.R
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
#' Polyline monotonicity
#'
#' Monotonicity describes the property that values along a line is ever
#' increasing or decreasing, i.e. `f(x) > f(x + 1) || f(x) < f(x + 1)`. The weak
#' version allows equality as well, i.e. `f(x) >= f(x + 1) || f(x) <= f(x + 1)`.
#'
#' If a polyline is not at least weakly monotone in one dimension there is a
#' chance that it intersects with itself. This can be queried by
#' `is_selfintersecting()`.
#'
#' @param x A `polyclid_polyline` vector
#' @param dim Either `"x"` or `"y"` to indicate in which dimension to test
#' monotonicity in
#'
#' @return A logical vector with the same length as `x`
#'
#' @export
#'
#' @examples
#' sine <- polyline(
#' seq(0, 2*pi, length.out = 20),
#' sin(seq(0, 2*pi, length.out = 20))
#' )
#' is_monotone(sine, "x")
#' is_monotone(sine, "y")
#'
#' poly <- polyline(
#' c(0, 1, 1, 2),
#' c(0, 1, 2, 3)
#' )
#'
#' is_monotone(sine, "x")
#' is_weakly_monotone(sine, "x")
#'
is_monotone <- function(x, dim = "both") {
if (!is_polyline(x)) {
cli_abort("{.arg x} must be a polyline geometry")
}
switch(
dim,
x = polyline_is_x_monotone(get_ptr(x)),
y = polyline_is_y_monotone(get_ptr(x)),
cli_abort("{.arg dim} must be either {.val x} or {.val y}")
)
}
#' @rdname is_monotone
#' @export
is_weakly_monotone <- function(x, dim = "x") {
if (!is_polyline(x)) {
cli_abort("{.arg x} must be a polyline geometry")
}
switch(
dim,
x = polyline_is_x_weakly_monotone(get_ptr(x)),
y = polyline_is_y_weakly_monotone(get_ptr(x)),
cli_abort("{.arg dim} must be either {.val x} or {.val y}")
)
}
#' @rdname is_monotone
#' @export
is_selfintersecting <- function(x) {
if (!is_polyline(x)) {
cli_abort("{.arg x} must be a polyline geometry")
}
polyline_is_selfintersecting(get_ptr(x))
}