a1 <- c(3, 1)
a2 <- c(1, 2)
b1 <- a1
p <- projection(a2, b1)
b2 <- a2 - p
c(dot_product(b1, b2), norm_vec(p))
plot(0, 0,
type = "n", xlim = c(-1.5, 3.5), ylim = c(-0.5, 2.5), asp = 1,
xlab = expression(x[1]), ylab = expression(x[2])
)
abline(h = 0, v = 0, col = "gray80")
arrows(0, 0, a1[1], a1[2], lwd = 2, col = "#17697e", length = 0.12, angle = 20)
arrows(0, 0, a2[1], a2[2], lwd = 2, col = "#1d4e6b", length = 0.12, angle = 20)
arrows(0, 0, b2[1], b2[2], lwd = 2, col = "#d9534f", length = 0.12, angle = 20)
segments(a2[1], a2[2], p[1], p[2], lty = 2)
segments(0, 0, p[1], p[2], lwd = 4, col = "#f2c14e")
s <- 0.12
d1 <- b1 / norm_vec(b1) * s
d2 <- b2 / norm_vec(b2) * s
lines(rbind(p - d1, p - d1 + d2, p + d2))
text(a1[1] - 0.3, a1[2] + 0.25, expression(v[1] == u[1]), col = "#17697e")
text(a2[1] + 0.1, a2[2] + 0.2, expression(v[2]), col = "#1d4e6b")
text(b2[1] - 0.2, b2[2] + 0.15, expression(u[2]), col = "#d9534f")
text(p[1] + 0.35, p[2] - 0.25, expression(proj[u[1]](v[2])))