affine_transform <- function(P, M, t_vec) {
linear <- t(M %*% t(P))
sweep(linear, 2, t_vec, "+")
}
plot_affine <- function(P, M, t_vec) {
P_lin <- t(M %*% t(P))
P_fin <- affine_transform(P, M, t_vec)
all_pts <- rbind(P, P_lin, P_fin, c(0, 0))
xlim <- range(all_pts[, 1]) + c(-1, 1)
ylim <- range(all_pts[, 2]) + c(-2.2, 1)
plot(NULL, xlim = xlim, ylim = ylim, asp = 1, xlab = "x", ylab = "y")
abline(h = 0, v = 0, lty = 2, col = "grey")
polygon(P, col = "#AAAAFF80", border = "blue")
polygon(P_lin, col = NA, border = "grey40", lty = 2)
polygon(P_fin, col = "#FFAAAA80", border = "red")
segments(P[, 1], P[, 2], P_fin[, 1], P_fin[, 2], lty = 3, col = "grey60")
text(P, labels = rownames(P), pos = c(1, 1, 3), col = "blue")
text(P_fin, labels = paste0(rownames(P), "'"), pos = c(2, 4, 3), col = "red")
legend("bottomright",
legend = c("v", "Mv", "Mv + t"),
fill = c("#AAAAFF80", "#FFFFFF", "#FFAAAA80"),
border = c("blue", "grey40", "red"), bty = "n"
)
}
coord_table <- function(P, M, t_vec) {
fmt <- function(X) sprintf("(%.2f,\u00a0%.2f)", X[, 1], X[, 2])
data.frame(
vertex = rownames(P),
before = fmt(P),
after = fmt(affine_transform(P, M, t_vec))
)
}