forked from OpenIntroStat/openintro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Braces.R
75 lines (73 loc) · 2.23 KB
/
Braces.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
68
69
70
71
72
73
74
75
#' Plot a Braces Symbol
#'
#' This function is not yet very flexible.
#'
#'
#' @param x x-coordinate of the center of the braces.
#' @param y y-coordinate of the center of the braces.
#' @param face.radians Radians of where the braces should face. For example,
#' the default with \code{face.radians = 0} has the braces facing right.
#' Setting to \code{pi / 2} would result in the braces facing up.
#' @param long The units for the long dimension of the braces.
#' @param short The units for the short dimension of the braces. This must be
#' less than or equal to half of the long dimension.
#' @param \dots Arguments passed to \code{\link[graphics]{lines}}.
#' @author David Diez
#' @seealso \code{\link{dlsegments}}
#' @export
#' @examples
#'
#' plot(0:1, 0:1, type = "n")
#' Braces(0.5, 0.5, face.radians = 3 * pi / 2)
Braces <- function(x,
y,
face.radians = 0, # left
long = 1,
short = 0.2,
...) {
stopifnot(short <= long / 2)
BuildQuarterCircle <- function(x, y,
radius,
radians = 0,
reverse = FALSE, ...) {
theta <- seq(radians, radians + pi / 2, length.out = 180)
if (reverse) {
theta <- rev(theta)
}
list(x + radius * cos(theta), y + radius * sin(theta))
}
# _____ Relative Coordinates _____ #
long. <- long / 2
short. <- short / 2
quarter <- list(
BuildQuarterCircle(short., long. - short., short., pi / 2),
BuildQuarterCircle(-short., short., short., 3 * pi / 2, TRUE),
BuildQuarterCircle(-short., -short., short., 0, TRUE),
BuildQuarterCircle(short., -long. + short., short., pi)
)
X <- c(
quarter[[1]][[1]],
0, 0,
quarter[[2]][[1]],
quarter[[3]][[1]],
0, 0,
quarter[[4]][[1]]
)
Y <- c(
quarter[[1]][[2]],
long. - short., short.,
quarter[[2]][[2]],
quarter[[3]][[2]],
-short., -long. + short.,
quarter[[4]][[2]]
)
tmp <- c(
cos(face.radians),
sin(face.radians),
-sin(face.radians),
cos(face.radians)
)
rotation.matrix <- matrix(tmp, 2, 2)
XY <- rotation.matrix <- rotation.matrix %*% rbind(X, Y)
lines(x + XY[1, ], y + XY[2, ], ...)
}