dist: Distances and the Pythagorean Theorem

Exercise template for computing the distance (numeric answer) between two randomly-drawn points in a Cartesian coordinate system.

Name:
dist
Type:
Related:
Preview:

What is the distance between the two points \(p = (2, 4)\) and \(q = (5, 4)\) in a Cartesian coordinate system?

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{(2 - 5)^2 + (4 - 4)^2} = 3\).

What is the distance between the two points \(p = (3, 2)\) and \(q = (5, 5)\) in a Cartesian coordinate system?

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{(3 - 5)^2 + (2 - 5)^2} = 3.606\).

What is the distance between the two points \(p = (3, 2)\) and \(q = (4, 1)\) in a Cartesian coordinate system?

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{(3 - 4)^2 + (2 - 1)^2} = 1.414\).

Description:
Computing the (Euclidean) distance between two randomly-drawn points by using the Pythagorean Theorem.
Solution feedback:
Yes
Randomization:
Random numbers and graphics
Mathematical notation:
Yes
Verbatim R input/output:
No
Images:
Yes
Other supplements:
No
Template:
Raw: (1 random version)
PDF:
dist-Rmd-pdf
dist-Rnw-pdf
HTML:
dist-Rmd-html
dist-Rnw-html

(Note that the HTML output contains mathematical equations in MathML, rendered by MathJax using ‘mathjax = TRUE’. Instead it is also possible to use ‘converter = “pandoc-mathjax”’ so that LaTeX equations are rendered by MathJax directly.)

Demo code:

library("exams")

set.seed(403)
exams2html("dist.Rmd", mathjax = TRUE)
set.seed(403)
exams2pdf("dist.Rmd")

set.seed(403)
exams2html("dist.Rnw", mathjax = TRUE)
set.seed(403)
exams2pdf("dist.Rnw")