1% File src/library/grid/man/grid.xspline.Rd
2% Part of the R package, https://www.R-project.org
3% Copyright 1995-2020 R Core Team
4% Distributed under GPL 2 or later
5
6\name{grid.xspline}
7\alias{grid.xspline}
8\alias{xsplineGrob}
9\title{ Draw an Xspline }
10\description{
11  These functions create and draw an xspline, a curve drawn
12  relative to control points.
13}
14\usage{
15grid.xspline(...)
16xsplineGrob(x = c(0, 0.5, 1, 0.5), y = c(0.5, 1, 0.5, 0),
17            id = NULL, id.lengths = NULL,
18            default.units = "npc",
19            shape = 0, open = TRUE, arrow = NULL, repEnds = TRUE,
20            name = NULL, gp = gpar(), vp = NULL)
21}
22\arguments{
23  \item{x}{A numeric vector or unit object specifying x-locations of
24    spline control points.}
25  \item{y}{A numeric vector or unit object specifying y-locations of
26    spline control points.}
27  \item{id}{A numeric vector used to separate locations in \code{x} and
28    \code{y} into multiple xsplines.  All locations with the same
29    \code{id} belong to the same xspline.}
30  \item{id.lengths}{A numeric vector used to separate locations in \code{x} and
31    \code{y} into multiple xspline.  Specifies consecutive blocks of
32    locations which make up separate xsplines.}
33  \item{default.units}{A string indicating the default units to use
34    if \code{x} or \code{y}
35    are only given as numeric vectors.}
36  \item{shape}{A numeric vector of values between -1 and 1, which
37    control the shape of the spline relative to the control points.}
38  \item{open}{A logical value indicating whether the spline is
39    a line or a closed shape.}
40  \item{arrow}{A list describing arrow heads to place at either end
41    of the xspline, as produced by the \code{arrow} function.}
42  \item{repEnds}{A logical value indicating whether the first and
43    last control points should be replicated for drawing the curve
44    (see Details below).}
45  \item{name}{ A character identifier. }
46  \item{gp}{An object of class \code{"gpar"}, typically the output
47    from a call to the function \code{\link{gpar}}.  This is basically
48    a list of graphical parameter settings.}
49  \item{vp}{A Grid viewport object (or NULL).}
50  \item{\dots}{Arguments to be passed to \code{xsplineGrob}.}
51}
52\value{
53  A grob object.
54}
55\details{
56  Both functions create an xspline grob (a graphical object describing an
57  xspline), but only \code{grid.xspline}
58  draws the xspline.
59
60  An xspline is a line drawn relative to control points.  For each
61  control point, the line
62  may pass through (interpolate) the control point or it
63  may only approach (approximate) the control
64  point;  the behaviour is determined by a shape parameter for each
65  control point.
66
67  If the shape parameter is greater than zero, the spline
68  approximates the control points (and is
69  very similar to a cubic B-spline when the shape is
70  1).  If the shape parameter is less than zero, the spline interpolates
71  the control points (and is very similar to a Catmull-Rom spline when
72  the shape is -1).  If the shape parameter is 0, the spline forms a
73  sharp corner at that control point.
74
75  For open xsplines, the start and end control points must have a shape
76  of 0 (and non-zero values are silently converted to zero without warning).
77
78  For open xsplines, by default the start and end control points are
79  actually replicated before the curve is drawn.  A curve is drawn
80  between (interpolating or approximating)
81  the second and third of each set of four control points, so
82  this default behaviour ensures that
83  the resulting curve starts at the first control point you have
84  specified and ends at the last control point.
85  The default
86  behaviour can be turned off via the \code{repEnds} argument,
87  in which case the curve that is drawn starts (approximately) at
88  the second control point and ends (approximately) at the first and
89  second-to-last control point.
90
91  The \code{repEnds} argument is ignored for closed xsplines.
92
93  Missing values are not allowed for \code{x} and \code{y}
94  (i.e., it is not valid for a control point to be missing).
95
96  For closed xsplines, a curve is automatically drawn
97  between the final control point and the initial control point.
98}
99\references{
100  Blanc, C. and Schlick, C. (1995),
101  "X-splines : A Spline Model Designed for the End User",
102  in \emph{Proceedings of SIGGRAPH 95}, pp.\sspace{}377--386.
103  \url{https://dept-info.labri.fr/~schlick/DOC/sig1.html}
104}
105\seealso{
106  \link{Grid},
107  \code{\link{viewport}},
108  \code{\link{arrow}}.
109
110  \code{\link{xspline}}.
111}
112\examples{
113x <- c(0.25, 0.25, 0.75, 0.75)
114y <- c(0.25, 0.75, 0.75, 0.25)
115
116xsplineTest <- function(s, i, j, open) {
117  pushViewport(viewport(layout.pos.col=j, layout.pos.row=i))
118  grid.points(x, y, default.units="npc", pch=16, size=unit(2, "mm"))
119  grid.xspline(x, y, shape=s, open=open, gp=gpar(fill="grey"))
120  grid.text(s, gp=gpar(col="grey"),
121            x=unit(x, "npc") + unit(c(-1, -1, 1, 1), "mm"),
122            y=unit(y, "npc") + unit(c(-1, 1, 1, -1), "mm"),
123            hjust=c(1, 1, 0, 0),
124            vjust=c(1, 0, 0, 1))
125  popViewport()
126}
127
128pushViewport(viewport(width=.5, x=0, just="left",
129                      layout=grid.layout(3, 3, respect=TRUE)))
130pushViewport(viewport(layout.pos.row=1))
131grid.text("Open Splines", y=1, just="bottom")
132popViewport()
133xsplineTest(c(0, -1, -1, 0), 1, 1, TRUE)
134xsplineTest(c(0, -1,  0, 0), 1, 2, TRUE)
135xsplineTest(c(0, -1,  1, 0), 1, 3, TRUE)
136xsplineTest(c(0,  0, -1, 0), 2, 1, TRUE)
137xsplineTest(c(0,  0,  0, 0), 2, 2, TRUE)
138xsplineTest(c(0,  0,  1, 0), 2, 3, TRUE)
139xsplineTest(c(0,  1, -1, 0), 3, 1, TRUE)
140xsplineTest(c(0,  1,  0, 0), 3, 2, TRUE)
141xsplineTest(c(0,  1,  1, 0), 3, 3, TRUE)
142popViewport()
143pushViewport(viewport(width=.5, x=1, just="right",
144                      layout=grid.layout(3, 3, respect=TRUE)))
145pushViewport(viewport(layout.pos.row=1))
146grid.text("Closed Splines", y=1, just="bottom")
147popViewport()
148xsplineTest(c(-1, -1, -1, -1), 1, 1, FALSE)
149xsplineTest(c(-1, -1,  0, -1), 1, 2, FALSE)
150xsplineTest(c(-1, -1,  1, -1), 1, 3, FALSE)
151xsplineTest(c( 0,  0, -1,  0), 2, 1, FALSE)
152xsplineTest(c( 0,  0,  0,  0), 2, 2, FALSE)
153xsplineTest(c( 0,  0,  1,  0), 2, 3, FALSE)
154xsplineTest(c( 1,  1, -1,  1), 3, 1, FALSE)
155xsplineTest(c( 1,  1,  0,  1), 3, 2, FALSE)
156xsplineTest(c( 1,  1,  1,  1), 3, 3, FALSE)
157popViewport()
158}
159\keyword{dplot}
160