# 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:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 4, 2020, 2:06am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/1 "2020-07-04T02:06:43Z")

</div>

I’m looking for geometry package, which has Euclidean point to point and point to line distances in 3D implemented.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [July 4, 2020, 11:17pm UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/2 "2020-07-04T23:17:56Z")

</div>

Distances.jl?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [July 4, 2020, 11:51pm UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/3 "2020-07-04T23:51:31Z")

</div>

Use `norm` in `LinearAlgebra` (in the standard library) for point to point distance.

Point to line needs a touch of geometry and then use `norm` again, e.g. [Distance from a point to a line - Wikipedia](https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line)

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 5, 2020, 12:01am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/4 "2020-07-05T00:01:35Z")

</div>

I know how to do it from scratch. I was hoping that a basic computational geometry package is lurking somewhere. The closest I found is a highly experimental [GitHub - rgcv/CGAL.jl: CGAL meets Julia](https://github.com/rgcv/CGAL.jl)

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [July 5, 2020, 12:13am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/5 "2020-07-05T00:13:56Z")

</div>

I agree that it seems to be missing.

Perhaps designing a good API is actually much more work than just reimplementing the algorithms each time!

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [July 5, 2020, 12:37am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/6 "2020-07-05T00:37:45Z")

</div>

Here’s some simple code:

```julia
using StaticArrays
using LinearAlgebra

"Point in N dimensions"
struct Point{N,T}
    x::SVector{N,T}
end

"Line in N dimensions. `p` is a point on the line and `u` is the direction vector
(not necessarily normalized). Parametrised as \$p + ut\$"
struct Line{N,T}
    p::SVector{N,T}
    u::SVector{N,T}
end

distance(p1::Point{N}, p2::Point{N}) where {N} = norm(p1.x - p2.x)

function distance(y::Point{N}, l::Line{N}) where {N}
    p, u = l.p, l.u
    
    t = (y.x - p) ⋅ u / (u ⋅ u) 
    x = Point(p + t*u)
    
    return distance(x, y)
end

l = Line(SA[1.0, 2, 3], SA[1.0, 1.0, 1.0])
p = Point(SA[4., 6., 6.])

distance(p, l)

```

Of course it would be better to use GeometryBasics.jl.  
Maybe this could / should be incorporated there.

---

<div class="post-metadata">

### Author: ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)
#### Post date: [July 6, 2020, 12:16pm UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/7 "2020-07-06T12:16:12Z")

</div>

Can it be easily calculated with Grassman?  
[https://github.com/chakravala/Grassmann.jl](https://github.com/chakravala/Grassmann.jl)

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 6, 2020, 12:29pm UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/8 "2020-07-06T12:29:29Z")

</div>

This thing is beautiful and scary at the same time. Definitely an overkill for simple Euclidean 3D spaces. I’m leaning towards implementing some basic functionality using SIMD.jl package and represent 3D points/vectors as `SIMD.Vec{4,Float32}` for performance.

---

<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 14, 2020, 4:40am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/9 "2020-07-14T04:40:13Z")

</div>

Well hi there, sorry to barge in.

I knew deeming it highly experimental would prove difficult for someone to use it confidently. I’ve removed that notice since. Despite it still being somewhat experimental, as testing is performed in a terribly confined environment, I’m confident it can perform well when necessary. Luckily that will soon change as I aim to register the package in the General registry, hopefully birthing an influx of issues and requests, maybe even contributions, to the fold. It is a relatively daunting undertaking!

As it stands, there are a couple of functions and types exposed from CGAL. 3D Point-Point and Point-Line distances are indeed present in the package. They come in the form of squared distances since CGAL tends to promote robustness over potentially unnecessary inexact/costly computation, but if the absolute distance is needed, `sqrt` will work just fine.

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 14, 2020, 4:47am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/10 "2020-07-14T04:47:44Z")

</div>

Thank you for working on this package. I think it may be of interest to many Julia users due to CGAL being a top-notch computational geometry library.

BTW, do you have any functions from 2D Polyline Simplification package wrapped up?

---

<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 14, 2020, 5:08am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/11 "2020-07-14T05:08:14Z")

</div>

**TL;DR** : Thanks! Unfortunately, I’ve overlooked 2D Polyline Simplification. There’s some foundation for it in place, but it’s not a near future priority. I’ll bear it in mind though.

I’m glad to see someone has found interest in it.

I’m sorry to say that I haven’t looked at that package yet, no. However, some of the building blocks such as 2D Triangulations (bit finicky, but usable) and 2D Polygons have already been mapped. I will keep this in mind moving forward.

Be that as it may, I wouldn’t hold my breath as it is not an immediate priority to wrap that package. A bit of background: this package was born out of necessity to obtain a series of types, functions, and algorithms as a foundation for another potential geometric constraint library, allowing for the description of constrained geometry in a textual fashion. Yes, there are constraint solvers out there, even geometric ones. Regardless, I set out to try constructive approaches instead. Fundamentally, they’re an abstraction over numerical and algebraic solvers, but we can always boil it down to numbers.

Enough rambling! Sorry to take up your time ^^

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 14, 2020, 5:19am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/12 "2020-07-14T05:19:44Z")

</div>

Thanks for sharing your plans. Not rambling at all. 😃

---

<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: [July 14, 2020, 5:58am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/13 "2020-07-14T05:58:42Z")

</div>

The [Chapter 6](https://www.geometrictools.com/Source/Mathematics.html) of [Geometric Tools Engine](https://www.geometrictools.com/) by Schneider, Eberly is to my knowledge the best source on the subject. CGAL can get nerdy so quickly.

Authors have worked at walt disney, dec, magic software  
The license is boost, eg. ~ mit.  
Code has been written during more than two decades and is still updated!

 ![mathematics - distance 2d](https://global.discourse-cdn.com/julialang/original/3X/1/a/1a800859d6735191824d0f8024ead50b6e0cfbfd.jpeg)

A good basis for an interface / API

Let’s make a port (: !

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 14, 2020, 7:55am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/14 "2020-07-14T07:55:50Z")

</div>

I used Geometric Tools for a prototype in C++ and I agree that it has fewer abstractions, which get in a way, compared with CGAL and especially with boost.geometry, which earns top nerdy honors. CGAL is much more comprehensive and has a real manual, though.

Eberly is close to releasing Geometric Tools Library (GTL), which will be a rewrite of Geometric Tools Engine.

---

<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: [July 15, 2020, 7:18am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/15 "2020-07-15T07:18:27Z")

</div>

> [@pauljurczak](#):
>
> Eberly is close to releasing Geometric Tools Library (GTL)

Book and code have been delayed in reason of covid. Book was finally published in june 2020. And we are now waiting for code.  
I bet it will be trapped in C++ land, and will certainly port some parts this year or next year since license will be boost too.

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [July 15, 2020, 7:37am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/16 "2020-07-15T07:37:19Z")

</div>

In general, I have interest in porting or developing high performance 2D and 3D basic computational geometry package. I don’t care about dimensions higher than 3 and accurate geometry. I will be fine with Float32 accuracy. Having an option of Float16 for faster compute on GPU would be nice.

I will know more in months to come. I have to learn more about Julia and specifics of my project, before I can contribute.

---

<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: [July 15, 2020, 4:39pm UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/17 "2020-07-15T16:39:05Z")

</div>

I intend to port a large bunch of work from Eberly, Ghali, Bostock starting in a couple of monthes.  
Recent work of Eberly address robustness issues in CG. I am not so pro about this.  
It remains to be determined how that will be handled.

I intend to contribute to [https://github.com/JuliaGeometry/](https://github.com/JuliaGeometry/)[https://github.com/JuliaGeo/](https://github.com/JuliaGeo/)  
[https://github.com/JuliaGraphics](https://github.com/JuliaGraphics) as much i can open source / we can pursue commons goal.

I work mainly in 2d/3d. Straight geometry, no bezier/spline curve, nurbs.

IMO, About the current state of

- CG in General : 2d does not belong to plot, 3d does not belong to mesh.
- GFX in Julia : Colors.jl is too tricky and GeometryBasis.jl too simple.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [July 17, 2020, 2:17am UTC](https://discourse.julialang.org/t/point-to-line-distance-in-geometrybasics-or-another-geometry-package/42501/18 "2020-07-17T02:17:27Z")

</div>

Indeed, `Grassmann` could be used for this also, I haven’t specifically built in functionality for the distance of a line and a point, although you could efficiently express it with geometric algebra. Currently, I am also working on supporting differential geometric calculations on a discrete manifold in any number of dimensions (1D, 2D, 3D, 4D, 5D, etc). I would recommend trying out the algebra in my package, I am using it as a foundation for geometry, similar to `GeometryBasics`, but different design.

---

<div class="post-metadata">

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

</div>

> [@pauljurczak](#):
>
> I’m looking for geometry package, which has Euclidean point to point and point to line distances in 3D implemented.

Here is a solution using [LazySets.jl](https://github.com/JuliaReach/LazySets.jl/):

```julia
using LazySets

# construct a line passing a point and the direction vector
l = Line([1.0, 2, 3], [1.0, 1.0, 1.0])

p = [4., 6., 6.]

distance(p, l)
0.816496580927726

# alternative constructor passing two points of the line
b = Line(from=[1.0, 2, 3], to=[2.0, 3, 4])

distance(p, b)
0.816496580927726

```

> implementing some basic functionality […] for performance.

For performance consider using [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) as already suggested by @dpsanders,

```julia
using StaticArrays, BenchmarkTools
                        
l = Line(SA[1.0, 2, 3], SA[1.0, 1.0, 1.0])
p = SA[4., 6., 6.]

@btime distance($p, $l)
  7.105 ns (0 allocations: 0 bytes)
0.816496580927726

```

---

<div class="post-metadata">

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

</div>

Using Grassmann.jl and a simple G(3,0,0) metric

```julia
julia> using Grassmann, BenchmarkTools

julia> basis"+++"
(⟨+++⟩, v, v₁, v₂, v₃, v₁₂, v₁₃, v₂₃, v₁₂₃)

julia> a,n = (1.0v1+2.0v2+3.0v3), (1.0v1+1.0v2+1.0v3)
(1.0v₁ + 2.0v₂ + 3.0v₃, 1.0v₁ + 1.0v₂ + 1.0v₃)

julia> p = 4.0v1+6.0v2+6.0v3
4.0v₁ + 6.0v₂ + 6.0v₃

julia> dist(a, n, p) = norm(((p-a)∧n)/n)
dist (generic function with 1 method)

julia> @btime dist($a,$n,$p)
  10.874 ns (0 allocations: 0 bytes)
0.816496580927726

```

Note with Geometric Algebra you don’t need to normalize the line direction. A more fair comparison with the example using LazySets.jl should include Line construction.

```julia
julia> @btime distance($p, Line($a, $n))
  39.027 ns (0 allocations: 0 bytes)
0.816496580927726

```

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