| Type: | Package |
| Title: | A Finer Way to Render 3D Illustrated Objects in 'grid' UsingAffine Transformations |
| Version: | 0.1.3 |
| Description: | Dilate, permute, project, reflect, rotate, shear, and translate 2D and 3D points. Supports parallel projections including oblique projections such as the cabinet projection as well as axonometric projections such as the isometric projection. Use 'grid's "affine transformation" feature to render illustrated flat surfaces. |
| URL: | https://trevorldavis.com/R/affiner/ |
| BugReports: | https://github.com/trevorld/affiner/issues |
| License: | MIT + file LICENSE |
| Imports: | graphics, grDevices, grid, R6, utils |
| Suggests: | aRtsy, ggplot2, gridpattern, gtable, knitr, ragg (≥ 1.3.3),rgl, rlang, rmarkdown, stats, testthat (≥ 3.0.0), vdiffr,withr |
| VignetteBuilder: | knitr, ragg, rmarkdown |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.1 |
| Config/testthat/edition: | 3 |
| NeedsCompilation: | no |
| Packaged: | 2024-12-02 06:12:41 UTC; trevorld |
| Author: | Trevor L. Davis |
| Maintainer: | Trevor L. Davis <trevor.l.davis@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2024-12-02 06:40:02 UTC |
affiner: A Finer Way to Render 3D Illustrated Objects in 'grid' Using Affine Transformations
Description

Dilate, permute, project, reflect, rotate, shear, and translate 2D and 3D points. Supports parallel projections including oblique projections such as the cabinet projection as well as axonometric projections such as the isometric projection. Use 'grid's "affine transformation" feature to render illustrated flat surfaces.
Package options
The following affiner function arguments may be set globally viabase::options():
- affiner_angular_unit
The default for the
unitargument used byangle()andas_angle().The default for this option is "degrees".- affiner_grid_unit
The default for the
unitargument used byaffine_settings().The default for this option is "inches".
The followingcli options may also be of interest:
- cli.unicode
Whether UTF-8 character support should be assumed.Along with
l10n_info()used to determine the default of theuse_unicodeargument offormat.angle()andprint.angle().
Author(s)
Maintainer: Trevor L. Davistrevor.l.davis@gmail.com (ORCID)
See Also
Useful links:
1D coordinate vector R6 Class
Description
Coord1D is anR6::R6Class() object representing two-dimensional pointsrepresented by Cartesian Coordinates.
Active bindings
xwA two-column matrix representing the homogeneous coordinates.The first column is the "x" coordinatesand the second column is all ones.
xA numeric vector of x-coordinates.
Methods
Public methods
Methodnew()
Usage
Coord1D$new(xw)
Arguments
xwA matrix with three columns representing (homogeneous) coordinates.The first column represents x coordinates andthe last column is all ones.Column names should be "x" and "w".
Methodprint()
Usage
Coord1D$print(n = NULL, ...)
Arguments
nNumber of coordinates to print. If
NULLprint all of them....Passed to
format.default().
Methodproject()
Usage
Coord1D$project(point = as_point1d("origin"), ...)Arguments
pointAPoint1D object of length one representing the pointyou with to reflect across or project to or an object coercible to one by
as_point1d(point, ...)such as "origin"....Passed to
project1d().
Methodreflect()
Usage
Coord1D$reflect(point = as_point1d("origin"), ...)Arguments
pointAPoint1D object of length one representing the pointyou with to reflect across or project to or an object coercible to one by
as_point1d(point, ...)such as "origin"....Passed to
reflect1d().
Methodscale()
Usage
Coord1D$scale(x_scale = 1)
Arguments
x_scaleScaling factor to apply to x coordinates
Methodtranslate()
Usage
Coord1D$translate(x = as_coord1d(0), ...)
Arguments
Methodtransform()
Usage
Coord1D$transform(mat = transform1d())
Arguments
matA 2x2 matrix representing a post-multiplied affine transformation matrix.The lastcolumn must be equal to
c(0, 1).If the lastrow isc(0, 1)you may need to transpose itto convert it from a pre-multiplied affine transformation matrix to a post-multiplied one.If a 1x1 matrix we'll quietly add a final column/row equal toc(0, 1).
Methodclone()
The objects of this class are cloneable with this method.
Usage
Coord1D$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
p <- as_coord1d(x = rnorm(100, 2))print(p, n = 10L)pc <- mean(p) # Centroid# method chained affine transformation matrices are auto-pre-multipliedp$ translate(-pc)$ reflect("origin")$ print(n = 10L)2D coordinate vector R6 Class
Description
Coord2D is anR6::R6Class() object representing two-dimensional pointsrepresented by Cartesian Coordinates.
Active bindings
xywA three-column matrix representing the homogeneous coordinates.The first two columns are "x" and "y" coordinatesand the third column is all ones.
xA numeric vector of x-coordinates.
yA numeric vector of y-coordinates.
Methods
Public methods
Methodnew()
Usage
Coord2D$new(xyw)
Arguments
xywA matrix with three columns representing (homogeneous) coordinates.The first two columns represent x and y coordinates andthe last column is all ones.Column names should be "x", "y", and "w".
Methodpermute()
Usage
Coord2D$permute(permutation = c("xy", "yx"))Arguments
permutationEither "xy" (no permutation) or "yx" (permute x and y axes)
Methodprint()
Usage
Coord2D$print(n = NULL, ...)
Arguments
nNumber of coordinates to print. If
NULLprint all of them....Passed to
format.default().
Methodproject()
Usage
Coord2D$project(line = as_line2d("x-axis"), ..., scale = 0)Arguments
lineALine2D object of length one representing the lineyou with to reflect across or project to or an object coercible to one by
as_line2d(line, ...)such as "x-axis" or "y-axis"....Passed to
project2d()scaleOblique projection scale factor.A degenerate
0value indicates an orthogonal projection.
Methodreflect()
Usage
Coord2D$reflect(line = as_line2d("x-axis"), ...)Arguments
lineALine2D object of length one representing the lineyou with to reflect across or project to or an object coercible to one by
as_line2d(line, ...)such as "x-axis" or "y-axis"....Passed to
reflect2d().
Methodrotate()
Usage
Coord2D$rotate(theta = angle(0), ...)
Arguments
thetaAn
angle()object of length one or an object coercible to one byas_angle(theta, ...)....Passed to
as_angle().
Methodscale()
Usage
Coord2D$scale(x_scale = 1, y_scale = x_scale)
Arguments
x_scaleScaling factor to apply to x coordinates
y_scaleScaling factor to apply to y coordinates
Methodshear()
Usage
Coord2D$shear(xy_shear = 0, yx_shear = 0)
Arguments
xy_shearHorizontal shear factor:
x = x + xy_shear * yyx_shearVertical shear factor:
y = yx_shear * x + y
Methodtranslate()
Usage
Coord2D$translate(x = as_coord2d(0, 0), ...)
Arguments
Methodtransform()
Usage
Coord2D$transform(mat = transform2d())
Arguments
matA 3x3 matrix representing a post-multiplied affine transformation matrix.The lastcolumn must be equal to
c(0, 0, 1).If the lastrow isc(0, 0, 1)you may need to transpose itto convert it from a pre-multiplied affine transformation matrix to a post-multiplied one.If a 2x2 matrix (such as a 2x2 post-multiplied 2D rotation matrix)we'll quietly add a final column/row equal toc(0, 0, 1).
Methodclone()
The objects of this class are cloneable with this method.
Usage
Coord2D$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
p <- as_coord2d(x = rnorm(100, 2), y = rnorm(100, 2))print(p, n = 10)pc <- mean(p) # Centroid# method chained affine transformation matrices are auto-pre-multipliedp$ translate(-pc)$ shear(x = 1, y = 0)$ reflect("x-axis")$ rotate(90, "degrees")$ print(n = 10)3D coordinate vector R6 Class
Description
Coord3D is anR6::R6Class() object representing three-dimensional pointsrepresented by Cartesian Coordinates.
Active bindings
xyzwA four-column matrix representing the homogeneous coordinates.The first three columns are "x", "y", and "z" coordinatesand the fourth column is all ones.
xA numeric vector of x-coordinates.
yA numeric vector of y-coordinates.
zA numeric vector of z-coordinates.
Methods
Public methods
Methodnew()
Usage
Coord3D$new(xyzw)
Arguments
xyzwA matrix with four columns representing (homogeneous) coordinates.The first three columns represent x, y, and z coordinates andthe last column is all ones.Column names should be "x", "y", "z", and "w".
Methodpermute()
Usage
Coord3D$permute(permutation = c("xyz", "xzy", "yxz", "yzx", "zyx", "zxy"))Arguments
permutationEither "xyz" (no permutation), "xzy" (permute y and z axes),"yxz" (permute x and y axes), "yzx" (x becomes z, y becomes x, z becomes y),"zxy" (x becomes y, y becomes z, z becomes x), "zyx" (permute x and z axes)
Methodprint()
Usage
Coord3D$print(n = NULL, ...)
Arguments
nNumber of coordinates to print. If
NULLprint all of them....Passed to
format.default().
Methodproject()
Usage
Coord3D$project( plane = as_plane3d("xy-plane"), ..., scale = 0, alpha = angle(45, "degrees"))Arguments
planeAPlane3D object of length one representing the planeyou wish to reflect across or project to or an object coercible to one using
as_plane3d(plane, ...)such as "xy-plane", "xz-plane", or "yz-plane"....Passed to
project3d().scaleOblique projection foreshortening scale factor.A (degenerate)
0value indicates an orthographic projection.A value of0.5is used by a “cabinet projection”while a value of1.0is used by a “cavalier projection”.alphaOblique projection angle (the angle the third axis is projected going off at).An
angle()object or one coercible to one withas_angle(alpha, ...).Popular angles are 45 degrees, 60 degrees, andarctangent(2)degrees.
Methodreflect()
Usage
Coord3D$reflect(plane = as_plane3d("xy-plane"), ...)Arguments
planeAPlane3D object of length one representing the planeyou wish to reflect across or project to or an object coercible to one using
as_plane3d(plane, ...)such as "xy-plane", "xz-plane", or "yz-plane"....Passed to
reflect3d().
Methodrotate()
Usage
Coord3D$rotate(axis = as_coord3d("z-axis"), theta = angle(0), ...)Arguments
axisACoord3D class object or one that can coerced to one by
as_coord3d(axis, ...).Theaxisrepresents the axis to be rotated around.thetaAn
angle()object of length one or an object coercible to one byas_angle(theta, ...)....Passed to
rotate3d().
Methodscale()
Usage
Coord3D$scale(x_scale = 1, y_scale = x_scale, z_scale = x_scale)
Arguments
x_scaleScaling factor to apply to x coordinates
y_scaleScaling factor to apply to y coordinates
z_scaleScaling factor to apply to z coordinates
Methodshear()
Usage
Coord3D$shear( xy_shear = 0, xz_shear = 0, yx_shear = 0, yz_shear = 0, zx_shear = 0, zy_shear = 0)
Arguments
xy_shearShear factor:
x = x + xy_shear * y + xz_shear * zxz_shearShear factor:
x = x + xy_shear * y + xz_shear * zyx_shearShear factor:
y = yx_shear * x + y + yz_shear * zyz_shearShear factor:
y = yx_shear * x + y + yz_shear * zzx_shearShear factor:
z = zx_shear * x + zy_shear * y + zzy_shearShear factor:
z = zx_shear * x + zy_shear * y + z
Methodtranslate()
Usage
Coord3D$translate(x = as_coord3d(0, 0, 0), ...)
Arguments
Methodtransform()
Usage
Coord3D$transform(mat = transform3d())
Arguments
matA 4x4 matrix representing a post-multiplied affine transformation matrix.The lastcolumn must be equal to
c(0, 0, 0, 1).If the lastrow isc(0, 0, 0, 1)you may need to transpose itto convert it from a pre-multiplied affine transformation matrix to a post-multiplied one.If a 3x3 matrix (such as a 3x3 post-multiplied 3D rotation matrix)we'll quietly add a final column/row equal toc(0, 0, 0, 1).
Methodclone()
The objects of this class are cloneable with this method.
Usage
Coord3D$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
p <- as_coord3d(x = rnorm(100, 2), y = rnorm(100, 2), z = rnorm(100, 2))print(p, n = 10)pc <- mean(p) # Centroid# method chained affine transformation matrices are auto-pre-multipliedp$ translate(-pc)$ reflect("xy-plane")$ rotate("z-axis", degrees(90))$ print(n = 10)2D lines R6 Class
Description
Line2D is anR6::R6Class() object representing two-dimensional lines.
Public fields
aNumeric vector that parameterizes the line via the equation
a * x + b * y + c = 0.bNumeric vector that parameterizes the line via the equation
a * x + b * y + c = 0.cNumeric vector that parameterizes the line via the equation
a * x + b * y + c = 0.
Methods
Public methods
Methodnew()
Usage
Line2D$new(a, b, c)
Arguments
aNumeric vector that parameterizes the line via the equation
a * x + b * y + c = 0.bNumeric vector that parameterizes the line via the equation
a * x + b * y + c = 0.cNumeric vector that parameterizes the line via the equation
a * x + b * y + c = 0.
Methodprint()
Usage
Line2D$print(n = NULL, ...)
Arguments
nNumber of lines to print. If
NULLprint all of them....Passed to
format.default().
Methodclone()
The objects of this class are cloneable with this method.
Usage
Line2D$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
p1 <- as_coord2d(x = 5, y = 10)p2 <- as_coord2d(x = 7, y = 12)theta <- degrees(45)as_line2d(theta, p1)as_line2d(p1, p2)3D planes R6 Class
Description
Plane3D is anR6::R6Class() object representing three-dimensional planes.
Public fields
aNumeric vector that parameterizes the plane via the equation
a * x + b * y + c * z + d = 0.bNumeric vector that parameterizes the plane via the equation
a * x + b * y + c * z + d = 0.cNumeric vector that parameterizes the plane via the equation
a * x + b * y + c * z + d = 0.dNumeric vector that parameterizes the plane via the equation
a * x + b * y + c * z + d = 0.
Methods
Public methods
Methodnew()
Usage
Plane3D$new(a, b, c, d)
Arguments
aNumeric vector that parameterizes the plane via the equation
a * x + b * y + c * z + d = 0.bNumeric vector that parameterizes the plane via the equation
a * x + b * y + c * z + d = 0.cNumeric vector that parameterizes the plane via the equation
a * x + b * y + c * z + d = 0.dNumeric vector that parameterizes the plane via the equation
a * x + b * y + c * z + d = 0.
Methodprint()
Usage
Plane3D$print(n = NULL, ...)
Arguments
nNumber of lines to print. If
NULLprint all of them....Passed to
format.default().
Methodclone()
The objects of this class are cloneable with this method.
Usage
Plane3D$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
1D points R6 Class
Description
Point1D is anR6::R6Class() object representing one-dimensional points.
Public fields
aNumeric vector that parameterizes the point via the equation
a * x + b = 0.bNumeric vector that parameterizes the point via the equation
a * x + b = 0.
Methods
Public methods
Methodnew()
Usage
Point1D$new(a, b)
Arguments
aNumeric vector that parameterizes the line via the equation
a * x + b = 0.bNumeric vector that parameterizes the line via the equation
a * x + b = 0.
Methodprint()
Usage
Point1D$print(n = NULL, ...)
Arguments
nNumber of lines to print. If
NULLprint all of them....Passed to
format.default().
Methodclone()
The objects of this class are cloneable with this method.
Usage
Point1D$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
p1 <- as_point1d(a = 1, b = 5)Compute Euclidean norm
Description
abs() computes the Euclidean norm forCoord2D class objects andCoord3D class objects.
Usage
## S3 method for class 'Coord1D'abs(x)## S3 method for class 'Coord2D'abs(x)## S3 method for class 'Coord3D'abs(x)Arguments
x |
Value
A numeric vector
Examples
z <- complex(real = 1:4, imaginary = 1:4) p <- as_coord2d(z) abs(p) # Euclidean norm # Less efficient ways to calculate same Euclidean norms sqrt(p * p) # `*` dot product distance2d(p, as_coord2d(0, 0, 0)) # In {base} R `abs()` calculates Euclidean norm of complex numbers all.equal(abs(p), abs(z)) all.equal(Mod(p), Mod(z)) p3 <- as_coord3d(x = 1:4, y = 1:4, z = 1:4) abs(p3)Affine transformation grob
Description
affineGrob() is a grid grob function to facilitateusing the group affine transformation features introduced in R 4.2.
Usage
affineGrob( grob, vp_define = NULL, transform = NULL, vp_use = NULL, name = NULL, gp = grid::gpar(), vp = NULL)grid.affine(...)Arguments
grob | A grid grob to perform affine transformations on. Passed to |
vp_define |
|
transform | An affine transformation function.If |
vp_use |
|
name | A character identifier (for grid). |
gp | A |
vp | A |
... | Passed to |
Details
Not all graphics devices provided bygrDevices or other R packages support theaffine transformation feature introduced in R 4.2.IfisTRUE(getRversion() >= '4.2.0') then the active graphics device should support this feature ifisTRUE(grDevices::dev.capabilities()$transformations).In particular the following graphics devices should support the affine transformation feature:
R's
grDevices::pdf()deviceR's 'cairo' devices e.g.
grDevices::cairo_pdf(),grDevices::png(type = 'cairo'),grDevices::svg(),grDevices::x11(type = 'cairo'), etc. IfisTRUE(capabilities('cairo'))then R was compiled with support for the 'cairo' devices .R's 'quartz' devices (since R 4.3.0) e.g.
grDevices::quartz(),grDevices::png(type = 'quartz'), etc. IfisTRUE(capabilities('aqua'))then R was compiled with support for the 'quartz' devices (generally onlyTRUEon macOS systems).ragg's devices (since v1.3.0) e.g.ragg::agg_png(),ragg::agg_capture(), etc.
Value
Agrid::gTree() (grob) object of class "affine".As a side effectgrid.affine() draws to the active graphics device.
See Also
Seeaffine_settings() for computing goodtransform andvp_use settings.Seehttps://www.stat.auckland.ac.nz/~paul/Reports/GraphicsEngine/groups/groups.htmlfor more information about the group affine transformation feature.SeeisocubeGrob() which wraps this function to render isometric cubes.
Examples
if (require("grid")) { grob <- grobTree(rectGrob(gp = gpar(fill = "blue", col = NA)), circleGrob(gp=gpar(fill="yellow", col = NA)), textGrob("RSTATS", gp=gpar(fontsize=32))) grid.newpage() pushViewport(viewport(width=unit(4, "in"), height=unit(2, "in"))) grid.draw(grob) popViewport()}if (require("grid") && getRversion() >= "4.2.0" && isTRUE(dev.capabilities()$transformations)) { # Only works if active graphics device supports affine transformations # such as `png(type="cairo")` on R 4.2+ vp_define <- viewport(width=unit(2, "in"), height=unit(2, "in")) affine <- affineGrob(grob, vp_define=vp_define) grid.newpage() pushViewport(viewport(width=unit(4, "in"), height=unit(2, "in"))) grid.draw(affine) popViewport()}if (require("grid") && getRversion() >= "4.2.0" && isTRUE(dev.capabilities()$transformations)) { # Only works if active graphics device supports affine transformations # such as `png(type="cairo")` on R 4.2+ settings <- affine_settings(xy = list(x = c(3/3, 2/3, 0/3, 1/3), y = c(2/3, 1/3, 1/3, 2/3)), unit = "snpc") affine <- affineGrob(grob, vp_define = vp_define, transform = settings$transform, vp_use = settings$vp) grid.newpage() grid.draw(affine)}Computegrid affine transformation feature viewports and transformation functions
Description
affine_settings() computesgrid group affine transformation feature viewport and transformationfunction settings given the (x,y) coordinates of the corners of theaffine transformed "viewport" one wishes to draw in.
Usage
affine_settings( xy = data.frame(x = c(0, 0, 1, 1), y = c(1, 0, 0, 1)), unit = getOption("affiner_grid_unit", "inches"))Arguments
xy | An R object with named elements |
unit | Which |
Value
A named list with the following group affine transformation feature viewportand functions settings:
- transform
An affine transformation function to pass to
affineGrob()oruseGrob().IfgetRversion()is less than"4.2.0"will instead beNULL.- vp
A
grid::viewport()object to pass toaffineGrob()oruseGrob().- sx
x-axis sx factor
- flipX
whether the affine transformed "viewport" is "flipped" horizontally
- x
x-coordinate for viewport
- y
y-coordinate for viewport
- width
Width of viewport
- height
Height of viewport
- default.units
Default
grid::unit()for viewport- angle
angle for viewport
Usage in other packages
To avoid taking a dependency onaffiner you may copy the source ofaffine_settings()into your own package under the permissive Unlicense. Either useusethis::use_standalone("trevorld/affiner", "standalone-affine-settings.r") orcopy the filestandalone-affine-settings.r into yourR directory and addgridto theImports of yourDESCRIPTION file.
See Also
Intended for use withaffineGrob() andgrid::useGrob().Seehttps://www.stat.auckland.ac.nz/~paul/Reports/GraphicsEngine/groups/groups.htmlfor more information about the group affine transformation feature.
Examples
if (require("grid")) { grob <- grobTree(rectGrob(gp = gpar(fill = "blue", col = NA)), circleGrob(gp=gpar(fill="yellow", col = NA)), textGrob("RSTATS", gp=gpar(fontsize=32))) grid.newpage() pushViewport(viewport(width=unit(4, "in"), height=unit(2, "in"))) grid.draw(grob) popViewport()}if (require("grid") && getRversion() >= "4.2.0" && isTRUE(dev.capabilities()$transformations)) { # Only works if active graphics device supports affine transformations # such as `png(type="cairo")` on R 4.2+ vp_define <- viewport(width=unit(2, "in"), height=unit(2, "in")) settings <- affine_settings(xy = list(x = c(1/3, 0/3, 2/3, 3/3), y = c(2/3, 1/3, 1/3, 2/3)), unit = "snpc") affine <- affineGrob(grob, vp_define=vp_define, transform = settings$transform, vp_use = settings$vp) grid.newpage() grid.draw(affine)}if (require("grid") && getRversion() >= "4.2.0" && isTRUE(dev.capabilities()$transformations)) { # Only works if active graphics device supports affine transformations # such as `png(type="cairo")` on R 4.2+ settings <- affine_settings(xy = list(x = c(3/3, 2/3, 0/3, 1/3), y = c(2/3, 1/3, 1/3, 2/3)), unit = "snpc") affine <- affineGrob(grob, vp_define=vp_define, transform = settings$transform, vp_use = settings$vp) grid.newpage() grid.draw(affine)}Get affiner options
Description
affiner_options() returns theaffiner package's global options.
Usage
affiner_options(..., default = FALSE)Arguments
... |
|
default | If |
Value
A list of option values.Note this functiondoes not set option values itself butthis list can be passed tooptions(),withr::local_options(), orwithr::with_options().
See Also
affiner for a high-level description of relevant global options.
Examples
affiner_options() affiner_options(default = TRUE) affiner_options(affiner_angular_unit = "pi-radians")Angle vectors
Description
angle() creates angle vectors with user specified angular unit.aroundas_angle() for those angular units.
Usage
angle(x = numeric(), unit = getOption("affiner_angular_unit", "degrees"))degrees(x)gradians(x)pi_radians(x)radians(x)turns(x)Arguments
x | An angle vector or an object to convert to it (such as a numeric vector) |
unit | A string of the desired angular unit. Supports the following strings(note we ignore any punctuation and space characters as well as any trailing
|
Value
A numeric vector of class "angle".Its "unit" attribute is a standardized string of the specified angular unit.
See Also
as_angle(),angular_unit(), andangle-methods.https://en.wikipedia.org/wiki/Angle#Units for more information about angular units.
Examples
# Different representations of the "same" angle angle(180, "degrees") angle(pi, "radians") angle(0.5, "turns") angle(200, "gradians") pi_radians(1) a1 <- angle(180, "degrees") angular_unit(a1) is_angle(a1) as.numeric(a1, "radians") cos(a1) a2 <- as_angle(a1, "radians") angular_unit(a2) is_congruent(a1, a2)Implemented base methods for angle vectors
Description
We implemented methods for several base generics for theangle() vectors.
Usage
## S3 method for class 'angle'as.double(x, unit = angular_unit(x), ...)## S3 method for class 'angle'as.complex(x, modulus = 1, ...)## S3 method for class 'angle'format(x, unit = angular_unit(x), ..., use_unicode = is_utf8_output())## S3 method for class 'angle'print(x, unit = angular_unit(x), ..., use_unicode = is_utf8_output())## S3 method for class 'angle'abs(x)Arguments
x |
|
unit | A string of the desired angular unit. Supports the following strings(note we ignore any punctuation and space characters as well as any trailing
|
... | Passed to |
modulus | Numeric vector representing the complex numbers' modulus |
use_unicode | If |
Details
MathematicalOps (in particular
+and-)for two angle vectors will (if necessary)set the second vector'sangular_unit()to match the first.as.numeric()takes aunitargument which can be used to convert angles into other angular unitse.g.angle(x, "degrees") |> as.numeric("radians")to cast a numeric vectorxfrom degrees to radians.abs()will calculate the angle modulo full turns.Use
is_congruent()to test if two angles are congruent instead of==orall.equal().Not all implemented methods are documented here and since
angle()is anumeric()class many other S3 genericsbesides the explicitly implemented ones should also work with it.
Value
Typical values as usually returned by these base generics.
Examples
# Two "congruent" angles a1 <- angle(180, "degrees") a2 <- angle(pi, "radians") print(a1) print(a1, unit = "radians") print(a1, unit = "pi-radians") cos(a1) sin(a1) tan(a1) # mathematical operations will coerce second `angle()` object to # same `angular_unit()` as the first one a1 + a2 a1 - a2 as.numeric(a1) as.numeric(a1, "radians") as.numeric(a1, "turns") # Use `is_congruent()` to check if two angles are "congruent" a1 == a2 isTRUE(all.equal(a1, a2)) is_congruent(a1, a2) is_congruent(a1, a2, mod_turns = FALSE) a3 <- angle(-180, "degrees") # Only congruent modulus full turns a1 == a3 isTRUE(all.equal(a1, a2)) is_congruent(a1, a3) is_congruent(a1, a3, mod_turns = FALSE)Get/set angular unit of angle vectors
Description
angular_unit() gets/sets the angular unit ofangle() vectors.
Usage
angular_unit(x)angular_unit(x) <- valueArguments
x | An |
value | A string of the desired angular unit. See |
Value
angular_unit() returns a string ofx's angular unit.
Examples
a <- angle(seq(0, 360, by = 90), "degrees")angular_unit(a)print(a)angular_unit(a) <- "turns"angular_unit(a)print(a)Cast to angle vector
Description
as_angle() casts to anangle() vector
Usage
as_angle(x, unit = getOption("affiner_angular_unit", "degrees"), ...)## S3 method for class 'angle'as_angle(x, unit = getOption("affiner_angular_unit", "degrees"), ...)## S3 method for class 'character'as_angle(x, unit = getOption("affiner_angular_unit", "degrees"), ...)## S3 method for class 'complex'as_angle(x, unit = getOption("affiner_angular_unit", "degrees"), ...)## S3 method for class 'Coord2D'as_angle(x, unit = getOption("affiner_angular_unit", "degrees"), ...)## S3 method for class 'Coord3D'as_angle( x, unit = getOption("affiner_angular_unit", "degrees"), type = c("azimuth", "inclination"), ...)## S3 method for class 'Line2D'as_angle(x, unit = getOption("affiner_angular_unit", "degrees"), ...)## S3 method for class 'Plane3D'as_angle( x, unit = getOption("affiner_angular_unit", "degrees"), type = c("azimuth", "inclination"), ...)## S3 method for class 'numeric'as_angle(x, unit = getOption("affiner_angular_unit", "degrees"), ...)Arguments
x | An R object to convert to a |
unit | A string of the desired angular unit. Supports the following strings(note we ignore any punctuation and space characters as well as any trailing
|
... | Further arguments passed to or from other methods |
type | Use "azimuth" to calculate the azimuthal angle and "inclination" to calculate the inclination angle aka polar angle. |
Value
Anangle() vector
Examples
as_angle(angle(pi, "radians"), "pi-radians")as_angle(complex(real = 0, imaginary = 1), "degrees")as_angle(as_coord2d(x = 0, y = 1), "turns")as_angle(200, "gradians")Cast to coord1d object
Description
as_coord1d() casts to aCoord1D class object
Usage
as_coord1d(x, ...)## S3 method for class 'character'as_coord1d(x, ...)## S3 method for class 'Coord2D'as_coord1d( x, permutation = c("xy", "yx"), ..., line = as_line2d("x-axis"), scale = 0)## S3 method for class 'data.frame'as_coord1d(x, ...)## S3 method for class 'list'as_coord1d(x, ...)## S3 method for class 'matrix'as_coord1d(x, ...)## S3 method for class 'numeric'as_coord1d(x, ...)## S3 method for class 'Coord1D'as_coord1d(x, ...)## S3 method for class 'Point1D'as_coord1d(x, ...)Arguments
x | An object that can be cast to aCoord1D class objectsuch as a numeric vector of x-coordinates. |
... | Further arguments passed to or from other methods |
permutation | Either "xy" (no permutation) or "yx" (permute x and y axes) |
line | ALine2D object of length one representing the lineyou with to reflect across or project to or an object coercible to one by |
scale | Oblique projection scale factor.A degenerate |
Value
ACoord1D class object
Examples
as_coord1d(x = rnorm(10))Cast to coord2d object
Description
as_coord2d() casts to aCoord2D class object
Usage
as_coord2d(x, ...)## S3 method for class 'angle'as_coord2d(x, radius = 1, ...)## S3 method for class 'character'as_coord2d(x, ...)## S3 method for class 'complex'as_coord2d(x, ...)## S3 method for class 'Coord3D'as_coord2d( x, permutation = c("xyz", "xzy", "yxz", "yzx", "zyx", "zxy"), ..., plane = as_plane3d("xy-plane"), scale = 0, alpha = angle(45, "degrees"))## S3 method for class 'data.frame'as_coord2d(x, ...)## S3 method for class 'list'as_coord2d(x, ...)## S3 method for class 'matrix'as_coord2d(x, ...)## S3 method for class 'numeric'as_coord2d(x, y = rep_len(0, length(x)), ...)## S3 method for class 'Coord2D'as_coord2d(x, ...)Arguments
x | An object that can be cast to aCoord2D class objectsuch as a matrix or data frame of coordinates. |
... | Further arguments passed to or from other methods |
radius | A numeric vector of radial distances. |
permutation | Either "xyz" (no permutation), "xzy" (permute y and z axes),"yxz" (permute x and y axes), "yzx" (x becomes z, y becomes x, z becomes y),"zxy" (x becomes y, y becomes z, z becomes x), "zyx" (permute x and z axes).This permutation is applied before the (oblique) projection. |
plane | APlane3D class object representing the planeyou wish to project to or an object coercible to one using |
scale | Oblique projection foreshortening scale factor.A (degenerate) |
alpha | Oblique projection angle (the angle the third axis is projected going off at).An |
y | Numeric vector of y-coordinates to be used. |
Value
ACoord2D class object
Examples
df <- data.frame(x = sample.int(10, 3), y = sample.int(10, 3))as_coord2d(df)as_coord2d(complex(real = 3, imaginary = 2))as_coord2d(angle(90, "degrees"), radius = 2)as_coord2d(as_coord3d(1, 2, 2), alpha = degrees(90), scale = 0.5)Cast to coord3d object
Description
as_coord3d() casts to aCoord3D class object
Usage
as_coord3d(x, ...)## S3 method for class 'angle'as_coord3d(x, radius = 1, inclination = NULL, z = NULL, ...)## S3 method for class 'character'as_coord3d(x, ...)## S3 method for class 'data.frame'as_coord3d(x, ..., z = NULL)## S3 method for class 'list'as_coord3d(x, ..., z = NULL)## S3 method for class 'matrix'as_coord3d(x, ...)## S3 method for class 'numeric'as_coord3d(x, y = rep_len(0, length(x)), z = rep_len(0, length(x)), ...)## S3 method for class 'Coord3D'as_coord3d(x, ...)## S3 method for class 'Coord2D'as_coord3d(x, z = rep_len(0, length(x)), ...)Arguments
x | An object that can be cast to aCoord3D class objectsuch as a matrix or data frame of coordinates. |
... | Further arguments passed to or from other methods |
radius | A numeric vector. If |
inclination | Spherical coordinates inclination angle aka polar angle. |
z | Numeric vector of z-coordinates to be used |
y | Numeric vector of y-coordinates to be usedif |
Value
ACoord3D class object
Examples
as_coord3d(x = 1, y = 2, z = 3)df <- data.frame(x = sample.int(10, 3), y = sample.int(10, 3), z = sample.int(10, 3))as_coord3d(df)# Cylindrical coordinatesas_coord3d(degrees(90), z = 1, radius = 1)# Spherical coordinatesas_coord3d(degrees(90), inclination = degrees(90), radius = 1)Cast to Line2D object
Description
as_line2d() casts to aLine2D object.
Usage
as_line2d(...)## S3 method for class 'numeric'as_line2d(a, b, c, ...)## S3 method for class 'angle'as_line2d(theta, p1 = as_coord2d("origin"), ...)## S3 method for class 'character'as_line2d(x, ...)## S3 method for class 'Coord2D'as_line2d(normal, p1 = as_coord3d("origin"), p2, ...)## S3 method for class 'Line2D'as_line2d(line, ...)## S3 method for class 'Point1D'as_line2d(point, b = 0, ...)Arguments
... | Passed to other function such as |
a,b,c | Numeric vectors that parameterize the line via the equation |
theta | Angle of the line represented by an |
p1 | Point on the line represented by aCoord2D class object. |
x | A (character) vector to be cast to aLine2D object |
normal | Normal vector to the line represented by aCoord2D class object. |
p2 | Another point on the line represented by aCoord2D class object. |
line | ALine2D object |
point | APoint1D object |
Examples
p1 <- as_coord2d(x = 5, y = 10)p2 <- as_coord2d(x = 7, y = 12)theta <- degrees(45)as_line2d(theta, p1)as_line2d(p1, p2)Cast to Plane3D object
Description
as_plane3d() casts to aPlane3D object.
Usage
as_plane3d(...)## S3 method for class 'numeric'as_plane3d(a, b, c, d, ...)## S3 method for class 'character'as_plane3d(x, ...)## S3 method for class 'Coord3D'as_plane3d(normal, p1 = as_coord3d("origin"), p2, p3, ...)## S3 method for class 'Plane3D'as_plane3d(plane, ...)## S3 method for class 'Point1D'as_plane3d(point, b = 0, c = 0, ...)## S3 method for class 'Line2D'as_plane3d(line, c = 0, ...)Arguments
... | Passed to other function such as |
a,b,c,d | Numeric vectors that parameterize the plane via the equation |
x | A (character) vector to be cast to aPlane3D object |
normal | Normal vector to the plane represented by aCoord3D class object. |
p1 | Point on the plane represented by aCoord3D class object. |
p2,p3 | Points on the plane represented byCoord3D class objects. |
plane | APlane3D object |
point | APoint1D object |
line | ALine2D object |
Cast to Point1D object
Description
as_point1d() casts to aPoint1D object.
Usage
as_point1d(...)## S3 method for class 'numeric'as_point1d(a, b, ...)## S3 method for class 'character'as_point1d(x, ...)## S3 method for class 'Coord1D'as_point1d(normal, ...)## S3 method for class 'Point1D'as_point1d(point, ...)Arguments
... | Passed to other function such as |
a,b | Numeric vectors that parameterize the point via the equation |
x | A (character) vector to be cast to aPoint1D object |
normal | Coord1D class object. |
point | APoint1D object |
Examples
p1 <- as_point1d(a = 1, b = 0)Cast to 1D affine transformation matrix
Description
as_transform1d() casts to atransform1d() affine transformation matrix
Usage
as_transform1d(x, ...)## S3 method for class 'transform1d'as_transform1d(x, ...)## Default S3 method:as_transform1d(x, ...)Arguments
x | An object that can be cast to a |
... | Further arguments passed to or from other methods |
Value
Atransform1d() object
Examples
m <- diag(2L)as_transform1d(m)Cast to 2D affine transformation matrix
Description
as_transform2d() casts to atransform2d() affine transformation matrix
Usage
as_transform2d(x, ...)## S3 method for class 'transform2d'as_transform2d(x, ...)## Default S3 method:as_transform2d(x, ...)Arguments
x | An object that can be cast to a |
... | Further arguments passed to or from other methods |
Value
Atransform2d() object
Examples
m <- diag(3L)as_transform2d(m)Cast to 3D affine transformation matrix
Description
as_transform3d() casts to atransform3d() affine transformation matrix
Usage
as_transform3d(x, ...)## S3 method for class 'transform3d'as_transform3d(x, ...)## Default S3 method:as_transform3d(x, ...)Arguments
x | An object that can be cast to a |
... | Further arguments passed to or from other methods |
Value
Atransform3d() object
Examples
m <- diag(4L)as_transform3d(m)Compute axis-aligned ranges
Description
range() computes axis-aligned ranges forCoord1D,Coord2D, andCoord3D class objects.
Usage
## S3 method for class 'Coord1D'range(..., na.rm = FALSE)## S3 method for class 'Coord2D'range(..., na.rm = FALSE)## S3 method for class 'Coord3D'range(..., na.rm = FALSE)Arguments
... | |
na.rm | logical, indicating if |
Value
Either aCoord1D,Coord2D, orCoord3D object of length two.The first element will have the minimum x/y(/z) coordinatesand the second element will have the maximum x/y(/z) coordinatesof the axis-aligned ranges.
Examples
range(as_coord2d(rnorm(5), rnorm(5)))range(as_coord3d(rnorm(5), rnorm(5), rnorm(5)))Compute centroids of coordinates
Description
mean()computes centroids forCoord1D,Coord2D, andCoord3D class objects
Usage
## S3 method for class 'Coord1D'mean(x, ...)## S3 method for class 'Coord2D'mean(x, ...)## S3 method for class 'Coord3D'mean(x, ...)Arguments
x | |
... | Passed to |
Value
ACoord1D,Coord2D, orCoord3D class object of length one
Examples
p <- as_coord2d(x = 1:4, y = 1:4)print(mean(p))print(sum(p) / length(p)) # less efficient alternativep <- as_coord3d(x = 1:4, y = 1:4, z = 1:4)print(mean(p))Compute 2D convex hulls
Description
convex_hull2d() is a S3 generic for computing the convex hull of an object.There is an implemented method supportingCoord2D class objectsusinggrDevices::chull() to compute the convex hull.
Usage
convex_hull2d(x, ...)## S3 method for class 'Coord2D'convex_hull2d(x, ...)Arguments
x | An object representing object to compute convex hull of such as aCoord2D class object. |
... | Further arguments passed to or from other methods. |
Value
An object of same class asx representing just the subset of points on the convex hull.The method forCoord2D class objects returns these points in counter-clockwise order.
Examples
p <- as_coord2d(x = rnorm(25), y = rnorm(25))print(convex_hull2d(p))# Equivalent to following caculation using `grDevices::chull()`all.equal(convex_hull2d(p), p[rev(grDevices::chull(as.list(p)))])Compute 3D vector cross product
Description
cross_product3d() computes the cross product of twoCoord3D class vectors.
Usage
cross_product3d(x, y)Arguments
x | ACoord3D class vector. |
y | ACoord3D class vector. |
Value
ACoord3D class vector
Examples
x <- as_coord3d(2, 3, 4)y <- as_coord3d(5, 6, 7)cross_product3d(x, y)1D Euclidean distances
Description
distance1d() computes 1D Euclidean distances.
Usage
distance1d(x, y)Arguments
x | |
y |
Examples
p <- as_coord1d(x = 1:4)distance1d(p, as_coord1d(0))2D Euclidean distances
Description
distance2d() computes 2D Euclidean distances.
Usage
distance2d(x, y)Arguments
x | |
y |
Examples
p <- as_coord2d(x = 1:4, y = 1:4)distance2d(p, as_coord2d(0, 0))3D Euclidean distances
Description
distance3d() computes 3D Euclidean distances.
Usage
distance3d(x, y)Arguments
x | |
y |
Examples
p <- as_coord3d(x = 1:4, y = 1:4, z = 1:4)distance3d(p, as_coord3d("origin"))Plot coordinates, points, lines, and planes
Description
plot() plotsCoord1D andCoord2D class objects whilepoints() drawsCoord1D andCoord2D class objectsandlines() drawsPoint1D andLine2D class objects to an existing plot.If the suggestedggplot2 andrgl packages are available we also registerggplot2::autolayer() methods forCoord1D,Coord2D,Point1D, andLine2D class objects and argl::plot3d() method forCoord3D class objects.
Usage
## S3 method for class 'Coord1D'plot(x, ...)## S3 method for class 'Coord1D'points(x, ...)## S3 method for class 'Point1D'lines(x, ...)## S3 method for class 'Coord2D'plot(x, ...)## S3 method for class 'Coord2D'points(x, ...)## S3 method for class 'Line2D'lines(x, ...)Arguments
x | A supported object to plot. |
... | Passed to the underlying plot method. |
Value
Used for its side effect of drawing to the graphics device.
Examples
c1 <- as_coord2d(x = 0, y = 1:10)l <- as_line2d(a = 1, b = -1, c = 0) # y = xc2 <- c1$clone()$reflect(l)plot(c1, xlim = c(-1, 11), ylim = c(-1, 11), main = "2D reflection across a line")lines(l)points(c2, col = "red")c1 <- as_coord2d(x = 1:10, y = 1:10)l <- as_line2d(a = -1, b = 0, c = 0) # x = 0c2 <- c1$clone()$project(l)if (require("ggplot2", quietly = TRUE, include.only = c("ggplot", "autolayer", "labs"))) { ggplot() + autolayer(c1) + autolayer(l) + autolayer(c2, color = "red") + labs(title = "2D projection onto a line")}c1 <- as_coord1d(x = seq.int(-4, -1))pt <- as_point1d(a = 1, b = 0) # x = 0c2 <- c1$clone()$reflect(pt)plot(c1, xlim = c(-5, 5), main = "1D reflection across a point")lines(pt)points(c2, col = "red")# 3D reflection across a planec1 <- as_coord3d(x = 1:10, y = 1:10, z = 1:10)pl <- as_plane3d(a = 0, b = 0, c = -1, d = 2) # z = 2c2 <- c1$clone()$reflect(pl)if (require("rgl", quietly = TRUE, include.only = c("plot3d", "planes3d", "points3d"))) { plot3d(c1, size = 8) planes3d(as.data.frame(pl), d = pl$d, color = "grey", alpha = 0.6) points3d(as.data.frame(c2), col = "red", size = 8)}Angle vector aware inverse trigonometric functions
Description
arcsine(),arccosine(),arctangent(),arcsecant(),arccosecant(), andarccotangent() areinverse trigonometric functions that returnangle() vectorswith a user chosen angular unit.
Usage
arcsine( x, unit = getOption("affiner_angular_unit", "degrees"), tolerance = sqrt(.Machine$double.eps))arccosine( x, unit = getOption("affiner_angular_unit", "degrees"), tolerance = sqrt(.Machine$double.eps))arctangent(x, unit = getOption("affiner_angular_unit", "degrees"), y = NULL)arcsecant(x, unit = getOption("affiner_angular_unit", "degrees"))arccosecant(x, unit = getOption("affiner_angular_unit", "degrees"))arccotangent(x, unit = getOption("affiner_angular_unit", "degrees"))Arguments
x | A numeric vector |
unit | A string of the desired angular unit. Supports the following strings(note we ignore any punctuation and space characters as well as any trailing
|
tolerance | If |
y | A numeric vector or |
Value
Anangle() vector
Examples
arccosine(-1, "degrees")arcsine(0, "turns")arctangent(0, "gradians")arccosecant(-1, "degrees")arcsecant(1, "degrees")arccotangent(1, "half-turns")# `base::atan2(y, x)` computes the angle of the vector from origin to (x, y)as_angle(as_coord2d(x = 1, y = 1), "degrees")Test whether an object is an angle vector
Description
is_angle() tests whether an object is an angle vector
Usage
is_angle(x)Arguments
x | An object |
Value
A logical value
Examples
a <- angle(180, "degrees")is_angle(a)is_angle(pi)Test whether two objects are congruent
Description
is_congruent() is a S3 generic that tests whether two different objects are “congruent”.Theis_congruent() method forangle() classes tests whether two angles are congruent.
Usage
is_congruent(x, y, ...)## S3 method for class 'numeric'is_congruent(x, y, ..., tolerance = sqrt(.Machine$double.eps))## S3 method for class 'angle'is_congruent( x, y, ..., mod_turns = TRUE, tolerance = sqrt(.Machine$double.eps))Arguments
x,y | Two objects to test whether they are “"congruent"”. |
... | Further arguments passed to or from other methods. |
tolerance | Angles (coerced to half-turns) or numerics with differences smallerthan |
mod_turns | If |
Value
A logical vector
Examples
# Use `is_congruent()` to check if two angles are "congruent" a1 <- angle(180, "degrees") a2 <- angle(pi, "radians") a3 <- angle(-180, "degrees") # Only congruent modulus full turns a1 == a2 isTRUE(all.equal(a1, a2)) is_congruent(a1, a2) is_congruent(a1, a2, mod_turns = FALSE) a1 == a3 isTRUE(all.equal(a1, a3)) is_congruent(a1, a3) is_congruent(a1, a3, mod_turns = FALSE)Test whether an object has a Coord1D class
Description
is_coord1d() tests whether an object has a "Coord1D" class
Usage
is_coord1d(x)Arguments
x | An object |
Value
A logical value
Examples
p <- as_coord1d(x = sample.int(10, 3))is_coord1d(p)Test whether an object has a Coord2D class
Description
is_coord2d() tests whether an object has a "Coord2D" class
Usage
is_coord2d(x)Arguments
x | An object |
Value
A logical value
Examples
p <- as_coord2d(x = sample.int(10, 3), y = sample.int(10, 3))is_coord2d(p)Test whether an object has a Coord3D class
Description
is_coord3d() tests whether an object has a "Coord3D" class
Usage
is_coord3d(x)Arguments
x | An object |
Value
A logical value
Examples
p <- as_coord3d(x = sample.int(10, 3), y = sample.int(10, 3), z = sample.int(10, 3))is_coord3d(p)Test whether an object has a Line2D class
Description
is_line2d() tests whether an object has a "Line2D" class
Usage
is_line2d(x)Arguments
x | An object |
Value
A logical value
Examples
l <- as_line2d(a = 1, b = 2, c = 3)is_line2d(l)Test whether an object has a Plane3D class
Description
is_plane3d() tests whether an object has a "Plane3D" class
Usage
is_plane3d(x)Arguments
x | An object |
Value
A logical value
Examples
p <- as_plane3d(a = 1, b = 2, c = 3, 4)is_plane3d(p)Test whether an object has a Point1D class
Description
is_point1d() tests whether an object has a "Point1D" class
Usage
is_point1d(x)Arguments
x | An object |
Value
A logical value
Examples
p <- as_point1d(a = 1, b = 5)is_point1d(p)Test if 1D affine transformation matrix
Description
is_transform1d() tests if object is atransform1d() affine transformation matrix
Usage
is_transform1d(x)Arguments
x | An object |
Value
A logical value
Examples
m <- transform1d(diag(2L))is_transform1d(m)is_transform1d(diag(2L))Test if 2D affine transformation matrix
Description
is_transform2d() tests if object is atransform2d() affine transformation matrix
Usage
is_transform2d(x)Arguments
x | An object |
Value
A logical value
Examples
m <- transform2d(diag(3L))is_transform2d(m)is_transform2d(diag(3L))Test if 3D affine transformation matrix
Description
is_transform3d() tests if object is atransform3d() affine transformation matrix
Usage
is_transform3d(x)Arguments
x | An object |
Value
A logical value
Examples
m <- transform3d(diag(4L))is_transform3d(m)is_transform3d(diag(4L))Isometric cube grob
Description
isometricCube() is a grid grob function to renderisometric cube faces by automatically wrapping aroundaffineGrob().
Usage
isocubeGrob( top, right, left, gp_border = grid::gpar(col = "black", lwd = 12), name = NULL, gp = grid::gpar(), vp = NULL)grid.isocube(...)Arguments
top | A grid grob object to use as the top side of the cube. ggplot2 objects will be coerced by |
right | A grid grob object to use as the right side of the cube. ggplot2 objects will be coerced by |
left | A grid grob object to use as the left side of the cube. ggplot2 objects will be coerced by |
gp_border | A |
name | A character identifier (for grid). |
gp | A |
vp | A |
... | Passed to |
Details
Anyggplot2 objects are coerced to grobs byggplot2::ggplotGrob(). Depending on what you'd liketo do you may want to instead manually convert a ggplot2 objectgg to a grob withgtable::gtable_filter(ggplot2::ggplotGrob(gg), "panel").
Not all graphics devices provided bygrDevices or other R packages support theaffine transformation feature introduced in R 4.2.IfisTRUE(getRversion() >= '4.2.0') then the active graphics device should support this feature ifisTRUE(grDevices::dev.capabilities()$transformations).In particular the following graphics devices should support the affine transformation feature:
R's
grDevices::pdf()deviceR's 'cairo' devices e.g.
grDevices::cairo_pdf(),grDevices::png(type = 'cairo'),grDevices::svg(),grDevices::x11(type = 'cairo'), etc. IfisTRUE(capabilities('cairo'))then R was compiled with support for the 'cairo' devices .R's 'quartz' devices (since R 4.3.0) e.g.
grDevices::quartz(),grDevices::png(type = 'quartz'), etc. IfisTRUE(capabilities('aqua'))then R was compiled with support for the 'quartz' devices (generally onlyTRUEon macOS systems).ragg's devices (since v1.3.0) e.g.ragg::agg_png(),ragg::agg_capture(), etc.
Value
Agrid::gTree() (grob) object of class "isocube".As a side effectgrid.isocube() draws to the active graphics device.
Examples
if (require("grid") && getRversion() >= "4.2.0" && isTRUE(dev.capabilities()$transformations)) { # Only works if active graphics device supports affine transformations # such as `png(type="cairo")` on R 4.2+ grid.newpage() gp_text <- gpar(fontsize = 72) grid.isocube(top = textGrob("top", gp = gp_text), right = textGrob("right", gp = gp_text), left = textGrob("left", gp = gp_text))}if (require("grid") && getRversion() >= "4.2.0" && isTRUE(dev.capabilities()$transformations)) { colors <- c("#D55E00", "#009E73", "#56B4E9") spacings <- c(0.25, 0.2, 0.25) texts <- c("pkgname", "left\nface", "right\nface") rots <- c(45, 0, 0) fontsizes <- c(52, 80, 80) sides <- c("top", "left", "right") types <- gridpattern::names_polygon_tiling[c(5, 7, 9)] l_grobs <- list() grid.newpage() for (i in 1:3) { if (requireNamespace("gridpattern", quietly = TRUE)) { bg <- gridpattern::grid.pattern_polygon_tiling( colour = "grey80", fill = c(colors[i], "white"), type = types[i], spacing = spacings[i], draw = FALSE) } else { bg <- rectGrob(gp = gpar(col = NA, fill = colors[i])) } text <- textGrob(texts[i], rot = rots[i], gp = gpar(fontsize = fontsizes[i])) l_grobs[[sides[i]]] <- grobTree(bg, text) } grid.newpage() grid.isocube(top = l_grobs$top, right = l_grobs$right, left = l_grobs$left)}# May take more than 5 seconds on CRAN machinesif (require("aRtsy") && require("grid") && require("ggplot2") && requireNamespace("gtable", quietly = TRUE) && getRversion() >= "4.2.0" && isTRUE(dev.capabilities()$transformations) ) { gg <- canvas_planet(colorPalette("lava"), threshold = 3) + scale_x_continuous(expand=c(0, 0)) + scale_y_continuous(expand=c(0, 0))grob <- ggplotGrob(gg)grob <- gtable::gtable_filter(grob, "panel") # grab just the panelgrid.newpage()grid.isocube(top = grob, left = grob, right = grob, gp_border = grid::gpar(col = "darkorange", lwd = 12))}2D normal vectors
Description
normal2d() is an S3 generic that computes a 2D normal vector.
Usage
normal2d(x, ...)## S3 method for class 'Coord2D'normal2d(x, ..., normalize = TRUE)## S3 method for class 'Line2D'normal2d(x, ..., normalize = TRUE)Arguments
x | Object to compute a 2D normal vector forsuch as aLine2D object. |
... | Passed to or from other methods. |
normalize | If |
Value
ACoord2D (normal) vector
Examples
p <- as_coord2d(x = 2, y = 3) normal2d(p) normal2d(p, normalize = FALSE)3D normal vectors
Description
normal3d() is an S3 generic that computes a 3D normal vector.
Usage
normal3d(x, ...)## S3 method for class 'Coord3D'normal3d(x, cross, ..., normalize = TRUE)## S3 method for class 'character'normal3d(x, ..., normalize = TRUE)## S3 method for class 'Plane3D'normal3d(x, ..., normalize = TRUE)Arguments
x | Object to compute a 3D normal vector forsuch as aPlane3D object |
... | Passed to other methods such as |
cross | ACoord3D vector.We'll compute the normal of |
normalize | If |
Value
ACoord3D (normal) vector
Examples
normal3d("xy-plane")normal3d(as_coord3d(2, 0, 0), cross = as_coord3d(0, 2, 0))Convert from 3D rotation matrix to axis-angle representation.
Description
rotate3d_to_AA() converts from (post-multiplied) rotation matrixto an axis-angle representation of 3D rotations.
Usage
rotate3d_to_AA( mat = diag(4), unit = getOption("affiner_angular_unit", "degrees"))Arguments
mat | 3D rotation matrix (post-multiplied).If you have a pre-multiplied rotation matrixsimply transpose it with |
unit | A string of the desired angular unit. Supports the following strings(note we ignore any punctuation and space characters as well as any trailing
|
See Also
https://en.wikipedia.org/wiki/Axis-angle_representation for more detailsabout the Axis-angle representation of 3D rotations.rotate3d() can be used to convert from an axis-angle representation to a rotation matrix.
Examples
# axis-angle representation of 90 degree rotation about the x-axis rotate3d_to_AA(rotate3d("x-axis", 90, unit = "degrees")) # find Axis-Angle representation of first rotating about x-axis 180 degrees # and then rotating about z-axis 45 degrees R <- rotate3d("x-axis", 180, unit = "degrees") %*% rotate3d("z-axis", 45, unit = "degrees") AA <- rotate3d_to_AA(R) # Can use `rotate3d()` to convert back to rotation matrix representation all.equal(R, do.call(rotate3d, AA))1D affine transformation matrices
Description
transform1d(),reflect1d(),scale2d(),andtranslate1d() create 1D affine transformation matrix objects.
Usage
transform1d(mat = diag(2L))project1d(point = as_point1d("origin"), ...)reflect1d(point = as_point1d("origin"), ...)scale1d(x_scale = 1)translate1d(x = as_coord1d(0), ...)Arguments
mat | A 2x2 matrix representing a post-multiplied affine transformation matrix.The lastcolumn must be equal to |
point | APoint1D object of length one representing the pointyou with to reflect across or project to or an object coercible to one by |
... | Passed to |
x_scale | Scaling factor to apply to x coordinates |
x | ACoord1D object of length one or an object coercible to one by |
Details
transform1d()User supplied (post-multiplied) affine transformation matrix
.
reflect1d()Reflections across a point.
scale1d()Scale the x-coordinates by multiplicative scale factors.
translate1d()Translate the coordinates by aCoord1D class object parameter.
transform1d() 1D affine transformation matrix objects are meant to bepost-multiplied and therefore shouldnot be multiplied in reverse order.Note theCoord1D class object methods auto-pre-multiply affine transformationswhen "method chaining" so pre-multiplying affine transformation matricesto do a single cumulative transformation instead of a method chain of multiple transformationswill not improve performance as much as as it does in other R packages.
To convert a pre-multiplied 1D affine transformation matrix to a post-multiplied onesimply compute its transpose usingt(). To get an inverse transformation matrixfrom an existing transformation matrix that does the opposite transformationssimply compute its inverse usingsolve().
Value
A 2x2 post-multiplied affine transformation matrix with classes "transform1d" and "at_matrix"
Examples
p <- as_coord1d(x = sample(1:10, 3))# {affiner} affine transformation matrices are post-multiplied# and therefore should **not** go in reverse ordermat <- transform1d(diag(2)) %*% scale1d(2) %*% translate1d(x = -1)p1 <- p$ clone()$ transform(mat)# The equivalent result appyling affine transformations via method chainingp2 <- p$ clone()$ transform(diag(2))$ scale(2)$ translate(x = -1)all.equal(p1, p2)2D affine transformation matrices
Description
transform2d(),project2d(),reflect2d(),rotate2d(),scale2d(),shear2d(),andtranslate2d() create 2D affine transformation matrix objects.
Usage
transform2d(mat = diag(3L))permute2d(permutation = c("xy", "yx"))project2d(line = as_line2d("x-axis"), ..., scale = 0)reflect2d(line = as_line2d("x-axis"), ...)rotate2d(theta = angle(0), ...)scale2d(x_scale = 1, y_scale = x_scale)shear2d(xy_shear = 0, yx_shear = 0)translate2d(x = as_coord2d(0, 0), ...)Arguments
mat | A 3x3 matrix representing a post-multiplied affine transformation matrix.The lastcolumn must be equal to |
permutation | Either "xy" (no permutation) or "yx" (permute x and y axes) |
line | ALine2D object of length one representing the lineyou with to reflect across or project to or an object coercible to one by |
... | Passed to |
scale | Oblique projection scale factor.A degenerate |
theta | An |
x_scale | Scaling factor to apply to x coordinates |
y_scale | Scaling factor to apply to y coordinates |
xy_shear | Horizontal shear factor: |
yx_shear | Vertical shear factor: |
x | ACoord2D object of length one or an object coercible to one by |
Details
transform2d()User supplied (post-multiplied) affine transformation matrix
.
project2d()Oblique vector projections onto a line parameterized byan oblique projection scale factor.A (degenerate) scale factor of zero results in an orthogonal projection.
reflect2d()Reflections across a line.To "flip" across both the x-axis and the y-axis use
scale2d(-1).rotate2d()Rotations around the origin parameterized by an
angle().scale2d()Scale the x-coordinates and/or the y-coordinates by multiplicative scale factors.
shear2d()Shear the x-coordinates and/or the y-coordinates using shear factors.
translate2d()Translate the coordinates by aCoord2D class object parameter.
transform2d() 2D affine transformation matrix objects are meant to bepost-multiplied and therefore shouldnot be multiplied in reverse order.Note theCoord2D class object methods auto-pre-multiply affine transformationswhen "method chaining" so pre-multiplying affine transformation matricesto do a single cumulative transformation instead of a method chain of multiple transformationswill not improve performance as much as as it does in other R packages.
To convert a pre-multiplied 2D affine transformation matrix to a post-multiplied onesimply compute its transpose usingt(). To get an inverse transformation matrixfrom an existing transformation matrix that does the opposite transformationssimply compute its inverse usingsolve().
Value
A 3x3 post-multiplied affine transformation matrix with classes "transform2d" and "at_matrix"
Examples
p <- as_coord2d(x = sample(1:10, 3), y = sample(1:10, 3))# {affiner} affine transformation matrices are post-multiplied# and therefore should **not** go in reverse ordermat <- transform2d(diag(3)) %*% reflect2d(as_coord2d(-1, 1)) %*% rotate2d(90, "degrees") %*% scale2d(1, 2) %*% shear2d(0.5, 0.5) %*% translate2d(x = -1, y = -1)p1 <- p$ clone()$ transform(mat)# The equivalent result appyling affine transformations via method chainingp2 <- p$ clone()$ transform(diag(3L))$ reflect(as_coord2d(-1, 1))$ rotate(90, "degrees")$ scale(1, 2)$ shear(0.5, 0.5)$ translate(x = -1, y = -1)all.equal(p1, p2)3D affine transformation matrices
Description
transform3d(),project3d(),reflect3d(),rotate3d(),scale3d(),shear3d(),andtranslate3d() create 3D affine transformation matrix objects.
Usage
transform3d(mat = diag(4L))permute3d(permutation = c("xyz", "xzy", "yxz", "yzx", "zyx", "zxy"))project3d( plane = as_plane3d("xy-plane"), ..., scale = 0, alpha = angle(45, "degrees"))reflect3d(plane = as_plane3d("xy-plane"), ...)rotate3d(axis = as_coord3d("z-axis"), theta = angle(0), ...)scale3d(x_scale = 1, y_scale = x_scale, z_scale = x_scale)shear3d( xy_shear = 0, xz_shear = 0, yx_shear = 0, yz_shear = 0, zx_shear = 0, zy_shear = 0)translate3d(x = as_coord3d(0, 0, 0), ...)Arguments
mat | A 4x4 matrix representing a post-multiplied affine transformation matrix.The lastcolumn must be equal to |
permutation | Either "xyz" (no permutation), "xzy" (permute y and z axes),"yxz" (permute x and y axes), "yzx" (x becomes z, y becomes x, z becomes y),"zxy" (x becomes y, y becomes z, z becomes x), "zyx" (permute x and z axes) |
plane | APlane3D object of length one representing the planeyou wish to reflect across or project to or an object coercible to one using |
... | Passed to |
scale | Oblique projection foreshortening scale factor.A (degenerate) |
alpha | Oblique projection angle (the angle the third axis is projected going off at).An |
axis | ACoord3D class object or one that can coerced to one by |
theta | An |
x_scale | Scaling factor to apply to x coordinates |
y_scale | Scaling factor to apply to y coordinates |
z_scale | Scaling factor to apply to z coordinates |
xy_shear | Shear factor: |
xz_shear | Shear factor: |
yx_shear | Shear factor: |
yz_shear | Shear factor: |
zx_shear | Shear factor: |
zy_shear | Shear factor: |
x | ACoord3D object of length one or an object coercible to one by |
Details
transform3d()User supplied (post-multiplied) affine transformation matrix
.
scale3d()Scale the x-coordinates and/or the y-coordinates and/or the z-coordinatesby multiplicative scale factors.
shear3d()Shear the x-coordinates and/or the y-coordinatesand/or the z-coordinates using shear factors.
translate3d()Translate the coordinates by aCoord3D class object parameter.
transform3d() 3D affine transformation matrix objects are meant to bepost-multiplied and therefore shouldnot be multiplied in reverse order.Note theCoord3D class object methods auto-pre-multiply affine transformationswhen "method chaining" so pre-multiplying affine transformation matricesto do a single cumulative transformation instead of a method chain of multiple transformationswill not improve performance as much as as it does in other R packages.
To convert a pre-multiplied 3D affine transformation matrix to a post-multiplied onesimply compute its transpose usingt(). To get an inverse transformation matrixfrom an existing transformation matrix that does the opposite transformationssimply compute its inverse usingsolve().
Value
A 4x4 post-multiplied affine transformation matrix with classes "transform3d" and "at_matrix"
Examples
p <- as_coord3d(x = sample(1:10, 3), y = sample(1:10, 3), z = sample(1:10, 3))# {affiner} affine transformation matrices are post-multiplied# and therefore should **not** go in reverse ordermat <- transform3d(diag(4L)) %*% rotate3d("z-axis", degrees(90)) %*% scale3d(1, 2, 1) %*% translate3d(x = -1, y = -1, z = -1)p1 <- p$ clone()$ transform(mat)# The equivalent result appyling affine transformations via method chainingp2 <- p$ clone()$ transform(diag(4L))$ rotate("z-axis", degrees(90))$ scale(1, 2, 1)$ translate(x = -1, y = -1, z = -1)all.equal(p1, p2)Angle vector aware trigonometric functions
Description
sine(),cosine(),tangent(),secant(),cosecant(), andcotangent() areangle() aware trigonometric functions that allow for a user chosen angular unit.
Usage
sine(x, unit = getOption("affiner_angular_unit", "degrees"))cosine(x, unit = getOption("affiner_angular_unit", "degrees"))tangent(x, unit = getOption("affiner_angular_unit", "degrees"))secant(x, unit = getOption("affiner_angular_unit", "degrees"))cosecant(x, unit = getOption("affiner_angular_unit", "degrees"))cotangent(x, unit = getOption("affiner_angular_unit", "degrees"))Arguments
x | An angle vector or an object to convert to it (such as a numeric vector) |
unit | A string of the desired angular unit. Supports the following strings(note we ignore any punctuation and space characters as well as any trailing
|
Value
A numeric vector
Examples
sine(pi, "radians")cosine(180, "degrees")tangent(0.5, "turns")a <- angle(0.5, "turns")secant(a)cosecant(a)cotangent(a)