Point-Line Distance¶
Write a function
point_line_distance(orpointLineDistance, according to your language's case convention) that takes six 64-bit floating point numbers:- $x_0$, $y_0$, $x_1$, $y_1$ --- coordinates of two distinct points $(x_0, y_0)$ and $(x_1, y_1)$ on a line
- $p$, $q$ --- the coordinates of a point $(p, q)$ and returns the distance from the point to the line through $(x_0, y_0)$ and $(x_1, y_1)$.
Consider introducing local variables to break the computation into several steps.
Hint 1: the distance between line $ax + by + c = 0$ and point $(p, q)$ is
$$ \frac{|ap + bq + c|}{ \sqrt{a^2 + b^2} }. $$
Hint 2: For line $ax + by + c = 0$, $(a, b)$ is perpendicular to the line. That is, it is perpendicular to $(x_1 - x_0, y_1 - y_0)$.
Use 64-bit floating point numbers for both inputs and the output.
Boilerplate source files
{go,jl,ml,rs}/point_line_distance.{go,jl,ml,rs}containing the test code is generated and shown below.Edit the source files either by opening them in a text editor (e.g., vscode), or editing the cells below and executing them.
import heytutor
1-2. Examples¶
1-2-1. A general question¶
%%hey
How to write a function in Go?
1-2-2. A hint on this specific problem¶
%%hey
Give me a hint on this problem for Rust
1-2-3. NEW: A few builtin variables¶
{file:FILENAME}is the content of FILE{bash[-1]}is the output of the last%%bash_cell,{bash[-2]}that of the second last%%bash_cell, etc.{problem}is the content of the file you specified by%%hey problem_file=foo.md{answer}is the content of the file you specified by%%hey answer_file=go/foo.go
1-2-4. Help when you struggle¶
%%hey answer_file=go/foo.go
I get this error when I compile it. What's wrong?"
My program:
{answer}
Error message:
{bash[-1]}
1-2-5. Ask feedback¶
- You are encouraged to ask a feedback once you think you are done with the problem, to know if there is a better answer. You can do so by something like:
%%hey problem_file=foo.md answer_file=go/foo.md
Give me a feedback to my answer.
Problem:
{problem}
My Answer:
{answer}
2. Go¶
2-1. Baseline code¶
import heytutor
%%writefile_ go/point_line_distance.go
package main
import "fmt"
import "math"
/** begin my answer */
func pointLineDistance(x0, y0, x1, y1, p, q float64) float64 {
dx := x1 - x0
dy := y1 - y0
a := dy
b := -dx
num := math.Abs(a * (p - x0) + b * (q - y0))
den := math.Sqrt(a * a + b * b)
return num / den
}
/** end my answer */
func main() {
if !(math.Abs(pointLineDistance(-3.0, 1.0, 1.0, -2.0, 4.0, -3.0) - 1.0) < 1.0e-5) { panic("wrong") }
if !(math.Abs(pointLineDistance( 2.0, 2.0, 4.0, 0.0, 1.0, 0.0) - 2.12132) < 1.0e-5) { panic("wrong") }
if !(math.Abs(pointLineDistance( 1.0, 1.0, 4.0, 3.0, 3.0, -2.0) - 3.60555) < 1.0e-5) { panic("wrong") }
fmt.Println("OK")
}
2-2. Compile¶
%%bash_
export PATH=${PATH}:~/.local/go/bin:~/go/bin
go build -o go/point_line_distance go/point_line_distance.go
- Note: when you run
goor other Go commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/go/bin) - You may consider adding that line in your
~/.bash_profile
2-3. Run¶
%%bash_
go/point_line_distance
2-4. Ask Questions or Get Feedback¶
%%hey problem_file=point_line_distance.md answer_file=go/point_line_distance.go
Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}
Give me a feedback to my answer.
3. Julia¶
3-1. Baseline code¶
import heytutor
%%writefile_ jl/point_line_distance.jl
### begin my answer
function point_line_distance(x0, y0, x1, y1, p, q)
dx, dy = x1 - x0, y1 - y0
a, b = dy, -dx
num = abs(a * (p - x0) + b * (q - y0))
den = sqrt(a * a + b * b)
num / den
end
### end my answer
function main()
@assert abs(point_line_distance(-3.0, 1.0, 1.0, -2.0, 4.0, -3.0) - 1.0) < 1.0e-5
@assert abs(point_line_distance( 2.0, 2.0, 4.0, 0.0, 1.0, 0.0) - 2.12132) < 1.0e-5
@assert abs(point_line_distance( 1.0, 1.0, 4.0, 3.0, 3.0, -2.0) - 3.60555) < 1.0e-5
println("OK")
end
main()
3-2. Compile¶
- Julia code is compiled "just in time" (compiled upon executed), so does not need a specific action for compilation before you run
3-3. Run¶
%%bash_
export PATH=${PATH}:~/.juliaup/bin
julia jl/point_line_distance.jl
- Note: when you run
juliaor other Julia commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/.juliaup/bin) - You may consider adding that line in your
~/.bash_profile
3-4. Interactive execution¶
juliacommand also serves is an interactive command for Julia programsYou can run a source code and continue interaction
$ julia -i jl/point_line_distance.jl
- For trial and error, you may also consider creating a Julia notebook
3-5. Ask Questions or Get Feedback¶
%%hey problem_file=point_line_distance.md answer_file=jl/point_line_distance.jl
Problem:
{problem}
My Answer (between ### begin my answer and ### end my answer):
{answer}
Give me a feedback to my answer.
4. OCaml¶
4-1. Baseline code¶
import heytutor
%%writefile_ ml/point_line_distance.ml
(** begin my answer *)
let point_line_distance x0 y0 x1 y1 p q =
let dx = x1 -. x0 in
let dy = y1 -. y0 in
let a = dy in
let b = -. dx in
let num = abs_float (a *. (p -. x0) +. b *. (q -. y0)) in
let den = sqrt (a *. a +. b *. b) in
num /. den;;
(** end my answer *)
let main () =
assert (abs_float (point_line_distance (-3.0) 1.0 1.0 (-2.0) 4.0 (-3.0) -. 1.0) < 1.0e-5);
assert (abs_float (point_line_distance 2.0 2.0 4.0 0.0 1.0 0.0 -. 2.12132) < 1.0e-5);
assert (abs_float (point_line_distance 1.0 1.0 4.0 3.0 3.0 (-2.0) -. 3.60555) < 1.0e-5);
Printf.printf "OK\n"
;;
main()
4-2. Compile¶
%%bash_
eval $(opam env)
ocamlc ml/point_line_distance.ml -o ml/point_line_distance
- Note: when you run
ocamlcor other OCaml commands (see below) in a terminal (SSH or Jupyter terminal), you need to execute the first line (eval $(opam env)) - You may consider adding that line in your
~/.bash_profile
4-3. Run¶
%%bash_
ml/point_line_distance
4-4. Interactive execution¶
ocamlcommand is an interactive command for OCaml programsIn terminal (Jupyter or SSH), you can directly run a source code
$ eval $(opam env) # once in your session or put it in ~/.bash_profile
$ ocaml ml/point_line_distance.ml
- You can run a source code and continue interaction
$ eval $(opam env) # once in your session or put it in ~/.bash_profile
$ ocaml -init ml/point_line_distance.ml
- For trial and error, you may also consider creating an OCaml notebook
4-5. Ask Questions or Get Feedback¶
%%hey problem_file=point_line_distance.md answer_file=ml/point_line_distance.ml
Problem:
{problem}
My Answer (between (** begin my answer *) and (** end my answer *)):
{answer}
Give me a feedback to my answer.
5. Rust¶
5-1. Baseline code¶
import heytutor
%%writefile_ rs/point_line_distance.rs
/** begin my answer */
fn point_line_distance(x0: f64, y0: f64, x1: f64, y1: f64, p: f64, q: f64) -> f64 {
let dx = x1 - x0;
let dy = y1 - y0;
let a = dy;
let b = -dx;
let num = (a * (p - x0) + b * (q - y0)).abs();
let den = (a * a + b * b).sqrt();
num / den
}
/** end my answer */
fn main() {
assert!((point_line_distance(-3.0, 1.0, 1.0, -2.0, 4.0, -3.0) - 1.0).abs() < 1.0e-5);
assert!((point_line_distance( 2.0, 2.0, 4.0, 0.0, 1.0, 0.0) - 2.12132).abs() < 1.0e-5);
assert!((point_line_distance( 1.0, 1.0, 4.0, 3.0, 3.0, -2.0) - 3.60555).abs() < 1.0e-5);
println!("OK")
}
5-2. Compile¶
%%bash_
. ~/.cargo/env
rustc rs/point_line_distance.rs -o rs/point_line_distance
- Note: when you run
rustcor other Rust commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (. ~/.cargo/env) - You may consider adding that line in your
~/.bash_profile
5-3. Run¶
%%bash_
rs/point_line_distance
5-4. Ask Questions or Get Feedback¶
%%hey problem_file=point_line_distance.md answer_file=rs/point_line_distance.rs
Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}
Give me a feedback to my answer.