# Point to line distance in GeometryBasics or another geometry package

**URL:** https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501
**Category:** New to Julia
**Tags:** geometry
**Created:** [July 4, 2020, 2:06am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501 "2020-07-04T02:06:43Z")
**Posts on this page:** 4
**Page:** 2

<div class="post-metadata">

### Author: ![rgcv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgcv/32/11224_2.png) [@rgcv](https://discourse.julialang.org/u/rgcv)
#### Post date: [July 21, 2020, 8:37pm UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/21 "2020-07-21T20:37:54Z")

</div>

I’m seeing a lot of interesting examples here, I want to join the fun too! Here’s an example using CGAL.jl, akin to @Orbots’ Grassmann.jl example:

```julia
julia> using CGAL, BenchmarkTools

julia> a, n = Point3(1, 2, 3), Vector3(1, 1, 1)
(PointC3(1, 2, 3), VectorC3(1, 1, 1))

julia> p = Point3(4, 6, 6)
PointC3(4, 6, 6)

julia> l = Line3(a, n)
Line_3(PointC3(1, 2, 3), PointC3(2, 3, 4))

julia> @btime √squared_distance($p, $l)
  13.499 ns (0 allocations: 0 bytes)
0.816496580927726

```

As mentioned, CGAL doesn’t directly provide absolute distance computation. One could however just create a wrapping function around `squared_distance`:

```julia
julia> distance(a, b) = √squared_distance(a, b)
distance (generic function with 1 method)

julia> @btime distance($p, $l)
  13.526 ns (0 allocations: 0 bytes)
0.816496580927726

```

Here’s a benchmark that includes `Line3` creation too:

```julia
julia> @btime distance($p, Line3($a, $n))
  108.824 ns (1 allocation: 16 bytes)
0.816496580927726

```

---

<div class="post-metadata">

### Author: ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)
#### Post date: [September 17, 2020, 1:21am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/22 "2020-09-17T01:21:12Z")

</div>

Hi there all,

Just a word to say that Eberly has finally release his new “Geometric Tools” v5 those days @ [https://github.com/davideberly/GeometricTools](https://github.com/davideberly/GeometricTools) .

---

<div class="post-metadata">

### Author: ![liamh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamh/32/208541_2.png) [@liamh](https://discourse.julialang.org/u/liamh)
#### Post date: [April 8, 2025, 4:52pm UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/23 "2025-04-08T16:52:52Z")

</div>

If the point and line are represented by vectors `p` and `l` respectively

```julia
using LinearAlgebra
norm(cross(l, p))/norm(l)

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 8, 2025, 6:36pm UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/24 "2025-04-08T18:36:45Z")

</div>

> [@liamh](#):
>
> If the point and line are represented by vectors `p` and `l` respectively

In this formula, the vector `l` is parallel to the line and `p = p1 - p0`, where the point `p0` is on the line and `p1` is a point outside the line. Basically, we move the line to the origin. It measures how far `p1` is from the line.

> **Example**
>
> ```julia
> using StaticArrays, LinearAlgebra
> p0, v = SA[1.0, 2, 3], SA[1.0, 1, 1] # line: point + vector
> p1 = SA[4., 6., 6.] # external point
> norm(cross(v, p1 - p0))/norm(v) # Distance: 0.8165; 4.1 ns (0 allocs: 0 bytes)
> 
> ```

[Previous page](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501.md?page=1)
