Scale, rotate, and shift numeric coordinates

rescale_coordinates(
  x,
  scale = c(1, 1),
  rotate_degrees = 0,
  shift = c(0, 0),
  center = NULL,
  rotation_axes = c(1, 2),
  plot_debug = FALSE,
  ...
)

Arguments

x

matrix with one or more columns containing numeric coordinates.

scale

numeric vector whose values are expanded to length ncol(x). After subtracting the center, the coordinates in each column are multiplied by the scale.

rotate_degrees

numeric value in degrees indicating rotation around the center, where positive values are clockwise rotation. This rotation is only applied to two columns in x defined by rotation_axes.

shift

numeric vector whose values are expanded to length ncol(x). The coordinates in each column are added to the shift, after applying scale and rotate_degrees

center

numeric vector whose values are expanded to length ncol(x), indicating the center point used for scale and rotate_degrees transformations. When center=NULL it is derived from the bounding box, which is the mean of the range for each column in x.

rotation_axes

integer vector length 2, indicating which two columns in x are used for rotate_degrees.

plot_debug

logical indicating whether to plot starting, intermediate, and final polygons during the processing.

...

additional arguments are ignored.

Value

matrix with numeric values after processing.

Details

This function takes a numeric matrix with two or more numeric columns, and adjusts the coordinates in three ways:

  • scale: adjust coordinate range by a multiplier, relative to a central point

  • rotate: rotate coordinates around a central point in degrees

  • shift: adjust coordinate range by adding a numeric value

The operations are performed in that order: rotate, scale, shift.

When center is not defined, the default behavior is to use the mean of the range of each coordinate column. Using the mean range is equivalent to using the mean of the bounding box.

See also

Examples

pts <- matrix(ncol=2, c(1:4, 6, 8, 5, 7));
rownames(pts) <- letters[1:4];
plot(pts, pch=rownames(pts), asp=1)
points(pts, pch=21, cex=3)

pts2 <- rescale_coordinates(pts, rotate_degrees=25);
arrows(x0=pts[,1], x1=pts2[,1], y0=pts[,2], y1=pts2[,2], col="red")
points(pts2, pch=21, cex=3, col="red", bg="white")
points(pts2, pch=rownames(pts), col="red")

pts3 <- rescale_coordinates(pts2, scale=0.5);
arrows(x0=pts2[,1], x1=pts3[,1], y0=pts2[,2], y1=pts3[,2], col="blue")
points(pts3, pch=21, cex=3, col="blue", bg="white")
points(pts3, pch=rownames(pts), col="blue")

pts4 <- rescale_coordinates(pts3, shift=c(0.5, 0.5));
arrows(x0=pts3[,1], x1=pts4[,1], y0=pts3[,2], y1=pts4[,2], col="gold")
points(pts4, pch=21, cex=3, col="gold", bg="white")
points(pts4, pch=rownames(pts), col="gold")