transform_by_matrix: Transform 2D coordinates by a 3*3 matrix

View source: R/transfromation.R

transform_by_matrixR Documentation

Transform 2D coordinates by a 3*3 matrix

Description

Transform 2D coordinates by a 3*3 matrix

Usage

transform_by_matrix(x, y = NULL, matrix = transform_matrix_affine("none"))

Arguments

x

a vector of coordinates at x axis or a data.frame if y is NULL

y

a vector of coordinates at y axis or NULL

matrix

a 3*3 affine transformation matrix

Value

a matrix of transformed coordinates

Examples

# coordinate for a unit square
us <- expand.grid(c(0,1), c(0,1))[c(1,3,4,2),]
us_tf <- transform_by_matrix(x = us, matrix = transform_matrix_affine("rotate", x = 0, y =0, theta = pi/4))
library(ggplot2)
library(magrittr)
ggplot() +
  geom_polygon(mapping = aes(x = us[,1], y = us[,2]), fill = "blue", alpha = 0.3) +
  geom_point(mapping = aes(x = us[,1], y = us[,2]), color = "blue", size = 3) +
  geom_polygon(mapping = aes(x = us_tf[,1], y = us_tf[,2]), fill = "red", alpha = 0.3) +
  geom_point(mapping = aes(x = us_tf[,1], y = us_tf[,2]), color = "red", size = 3) +
  geom_segment(mapping = aes(x = us[,1], y = us[,2], xend = us_tf[,1], yend = us_tf[,2]),
               color = "black", linetype = "dotted", size = 1, arrow = arrow(type = "closed", length = unit(0.03, "native"))) +
  coord_fixed()


zzwch/crosslink documentation built on Aug. 27, 2023, 5:57 p.m.