<>= p <- c(sample(1:3, 1), sample(1:5, 1)) q <- c(sample(4:5, 1), sample(1:5, 1)) sol <- sqrt(sum((p - q)^2)) @ \begin{question} What is the distance between the two points $p = (\Sexpr{p[1]}, \Sexpr{p[2]})$ and $q = (\Sexpr{q[1]}, \Sexpr{q[2]})$ in a Cartesian coordinate system? \end{question} \begin{solution} The distance $d$ of $p$ and $q$ is given by $d^2 = (p_1 - q_1)^2 + (p_2 - q_2)^2$ (Pythagorean formula). Hence $d = \sqrt{(p_1 - q_1)^2 + (p_2 - q_2)^2} = \sqrt{(\Sexpr{p[1]} - \Sexpr{q[1]})^2 + (\Sexpr{p[2]} - \Sexpr{q[2]})^2} = \Sexpr{round(sol, digits = 3)}$. <>= par(mar = c(4, 4, 1, 1)) plot(0, type = "n", xlim = c(0, 6), ylim = c(0, 6), xlab = "x", ylab = "y") grid(col = "slategray") points(rbind(p, q), pch = 19) text(rbind(p, q), c("p", "q"), pos = c(2, 4)) lines(rbind(p, q)) lines(c(p[1], p[1], q[1]), c(p[2], q[2], q[2]), lty = 2) @ \end{solution} %% \extype{num} %% \exsolution{\Sexpr{round(sol, digits = 3)}} %% \exname{Euclidean distance} %% \extol{0.01}