1% File src/library/graphics/man/xspline.Rd
2% Part of the R package, https://www.R-project.org
3% Copyright 1995-2014 R Core Team
4% Distributed under GPL 2 or later
5
6\name{xspline}
7\alias{xspline}
8\title{Draw an X-spline}
9\description{
10  Draw an X-spline, a curve drawn relative to control points.
11}
12\usage{
13xspline(x, y = NULL, shape = 0, open = TRUE, repEnds = TRUE,
14        draw = TRUE, border = par("fg"), col = NA, \dots)
15}
16\arguments{
17  \item{x,y}{vectors containing the coordinates of the vertices
18    of the polygon. See \code{\link{xy.coords}} for alternatives.}
19  \item{shape}{A numeric vector of values between -1 and 1, which
20    control the shape of the spline relative to the control points.}
21  \item{open}{A logical value indicating whether the spline is
22    an open or a closed shape.}
23  \item{repEnds}{For open X-splines, a logical value indicating whether
24    the first and last control points should be replicated for drawing
25    the curve. Ignored for closed X-splines.}
26  \item{draw}{logical: should the X-spline be drawn?  If false, a set of
27    line segments to draw the curve is returned, and nothing is drawn.}
28  \item{border}{the color to draw the curve.   Use \code{border = NA} to
29    omit borders.}
30  \item{col}{the color for filling the shape. The default,
31    \code{NA}, is to leave unfilled.}
32  \item{\dots}{\link{graphical parameters} such as \code{lty}, \code{xpd},
33    \code{lend}, \code{ljoin} and \code{lmitre} can be given as arguments.}
34}
35\details{
36  An X-spline is a line drawn relative to control points.  For each
37  control point, the line may pass through (interpolate) the control
38  point or it may only approach (approximate) the control point;  the
39  behaviour is determined by a shape parameter for each control point.
40
41  If the shape parameter is greater than zero, the spline approximates
42  the control points (and is very similar to a cubic B-spline when the
43  shape is 1).  If the shape parameter is less than zero, the spline
44  interpolates the control points (and is very similar to a Catmull-Rom
45  spline when the shape is -1).  If the shape parameter is 0, the spline
46  forms a sharp corner at that control point.
47
48  For open X-splines, the start and end control points must have a shape
49  of 0 (and non-zero values are silently converted to zero).
50
51  For open X-splines, by default the start and end control points are
52  replicated before the curve is drawn.  A curve is drawn between
53  (interpolating or approximating) the second and third of each set of
54  four control points, so this default behaviour ensures that the
55  resulting curve starts at the first control point you have specified
56  and ends at the last control point.  The default behaviour can be
57  turned off via the \code{repEnds} argument.
58}
59\value{
60  If \code{draw = TRUE}, \code{NULL} otherwise a list with elements
61  \code{x} and \code{y} which could be passed to \code{\link{lines}},
62  \code{\link{polygon}} and so on.
63
64  Invisible in both cases.
65}
66\note{
67  Two-dimensional splines need to be created in an isotropic coordinate
68  system.  Device coordinates are used (with an anisotropy correction if
69  needed.)
70}
71\references{
72  Blanc, C. and Schlick, C. (1995),
73  \emph{X-splines : A Spline Model Designed for the End User},
74  in \emph{Proceedings of SIGGRAPH 95}, pp.\sspace{}377--386.
75  \url{https://dept-info.labri.fr/~schlick/DOC/sig1.html}
76}
77\seealso{
78  \code{\link{polygon}}.
79
80  \code{\link{par}} for how to specify colors.
81}
82\examples{
83## based on examples in ?grid.xspline
84
85xsplineTest <- function(s, open = TRUE,
86                        x = c(1,1,3,3)/4,
87                        y = c(1,3,3,1)/4, ...) {
88    plot(c(0,1), c(0,1), type = "n", axes = FALSE, xlab = "", ylab = "")
89    points(x, y, pch = 19)
90    xspline(x, y, s, open, ...)
91    text(x+0.05*c(-1,-1,1,1), y+0.05*c(-1,1,1,-1), s)
92}
93op <- par(mfrow = c(3,3), mar = rep(0,4), oma = c(0,0,2,0))
94xsplineTest(c(0, -1, -1, 0))
95xsplineTest(c(0, -1,  0, 0))
96xsplineTest(c(0, -1,  1, 0))
97xsplineTest(c(0,  0, -1, 0))
98xsplineTest(c(0,  0,  0, 0))
99xsplineTest(c(0,  0,  1, 0))
100xsplineTest(c(0,  1, -1, 0))
101xsplineTest(c(0,  1,  0, 0))
102xsplineTest(c(0,  1,  1, 0))
103title("Open X-splines", outer = TRUE)
104
105par(mfrow = c(3,3), mar = rep(0,4), oma = c(0,0,2,0))
106xsplineTest(c(0, -1, -1, 0), FALSE, col = "grey80")
107xsplineTest(c(0, -1,  0, 0), FALSE, col = "grey80")
108xsplineTest(c(0, -1,  1, 0), FALSE, col = "grey80")
109xsplineTest(c(0,  0, -1, 0), FALSE, col = "grey80")
110xsplineTest(c(0,  0,  0, 0), FALSE, col = "grey80")
111xsplineTest(c(0,  0,  1, 0), FALSE, col = "grey80")
112xsplineTest(c(0,  1, -1, 0), FALSE, col = "grey80")
113xsplineTest(c(0,  1,  0, 0), FALSE, col = "grey80")
114xsplineTest(c(0,  1,  1, 0), FALSE, col = "grey80")
115title("Closed X-splines", outer = TRUE)
116
117par(op)
118
119x <- sort(stats::rnorm(5))
120y <- sort(stats::rnorm(5))
121plot(x, y, pch = 19)
122res <- xspline(x, y, 1, draw = FALSE)
123lines(res)
124## the end points may be very close together,
125## so use last few for direction
126nr <- length(res$x)
127arrows(res$x[1], res$y[1], res$x[4], res$y[4], code = 1, length = 0.1)
128arrows(res$x[nr-3], res$y[nr-3], res$x[nr], res$y[nr], code = 2, length = 0.1)
129}
130\keyword{aplot}
131