# How to determine if a point given is within a line (two points)

**URL:** https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896
**Category:** Geo
**Tags:** math, geo, geometry, julia
**Created:** [November 7, 2023, 11:05am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896 "2023-11-07T11:05:25Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Chamika\_Kasun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chamika_kasun/32/202926_2.png) [@Chamika\_Kasun](https://discourse.julialang.org/u/Chamika_Kasun)
#### Post date: [November 7, 2023, 11:05am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/1 "2023-11-07T11:05:26Z")

</div>

I have got a line with two points and a single point. I need to check whether the single point is between the line (i.e. between the two points) given, including the two points given.

How do I do that using Julia.

 ![Screenshot 2023-11-07 at 9.47.26 pm](https://global.discourse-cdn.com/julialang/original/3X/3/b/3bc86f0a15552ec63005c356d6b2ac15d11ddf9c.png)  
 ![Screenshot 2023-11-07 at 9.46.31 pm](https://global.discourse-cdn.com/julialang/original/3X/b/c/bcf94845f2cf5c93814d50c834fe7a754d3d6b01.png)

```julia
line_string = [
                 [
                   145.06226370532733,
                   -37.90964817711629
                 ],
                 [
                   145.06220165065736,
                   -37.90997457594736
                 ]
               ]

point = [145.06220165065736, -37.90997457594736]

```

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [November 7, 2023, 11:32am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/2 "2023-11-07T11:32:22Z")

</div>

One solution is:

```julia
using GeoStats

Point(0.5, 0.5) ∈ Segment((0.0, 0.0), (1.0, 1.0))

```

Check chapter 4 of the book:

[https://juliaearth.github.io/geospatial-data-science-with-julia/](https://juliaearth.github.io/geospatial-data-science-with-julia/)

---

<div class="post-metadata">

### Author: ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)
#### Post date: [November 8, 2023, 8:10am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/3 "2023-11-08T08:10:54Z")

</div>

Check if the normalized dot product of the line with the vector from one end of the line to the point is between zero and one:

```julia-repl
julia> using LinearAlgebra
julia> between(a, b, p) = 0 <= dot((b - a) / norm(b - a), (p - a) / norm(b - a)) <= 1
between (generic function with 1 method)

julia> between(line_string[1], line_string[2], point)
true

```

---

<div class="post-metadata">

### Author: ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)
#### Post date: [November 8, 2023, 9:00am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/4 "2023-11-08T09:00:53Z")

</div>

> [@HanD](#):
>
> ```julia
> between(a, b, p) = 0 <= dot((b - a) / norm(b - a), (p - a) / norm(p - a)) <= 1
> between (generic function with 1 method)
> 
> ```

This does not seem 100% right.

```julia
a=[0.0,0.0]
2-element Vector{Float64}:
 0.00000e+00
 0.00000e+00

julia> b=[1.0,0.0]
2-element Vector{Float64}:
 1.00000e+00
 0.00000e+00

julia> p=[.5,.5]
2-element Vector{Float64}:
 5.00000e-01
 5.00000e-01

julia> between(a,b,p)
true

```

but (1/2, 1/2) is not between (0,0) and (1,0)

---

<div class="post-metadata">

### Author: ![gvdr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gvdr/32/6387_2.png) [@gvdr](https://discourse.julialang.org/u/gvdr)
#### Post date: [November 8, 2023, 9:46am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/5 "2023-11-08T09:46:30Z")

</div>

I think we are a bit confused by your question.

1. Do you mean whether the point is lying on the line segment between two points? (Then your initial question should be answered no, as that point is slightly off the line)
2. Do you mean the point is in the “slice” of plane between the two points (as defines by the normal to the line segment connecting the two points, at those two points)? In that case, the latter question should be yes.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [November 8, 2023, 9:51am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/6 "2023-11-08T09:51:37Z")

</div>

O also got confused with te quedtion. if question is 1. then compute distante to the line and see if it is smaller than an eps of your choice.

---

<div class="post-metadata">

### Author: ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)
#### Post date: [November 8, 2023, 9:59am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/7 "2023-11-08T09:59:28Z")

</div>

> [@ctkelley](#):
>
> but (1/2, 1/2) is not between (0,0) and (1,0)

Yupp, it doesn’t check if the point is actually on the line. For that, you need to check if the norm of the cross product is (near) zero. My snippet above checks if the point is between the two endpoints _assuming_ it is on the line.

Here’s an adjusted version that also checks for being on the segment:

```julia
function between(a, b, p; tol = 1e-10)::Bool
    v1 = b - a
    v2 = p - a
    return isapprox(dot(v1, v2), norm(v1) * norm(v2); rtol = tol) &&
        -tol <= dot(v1 / norm(v1), v2 / norm(v1)) <= 1 + tol
end

```

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [November 8, 2023, 11:04am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/8 "2023-11-08T11:04:39Z")

</div>

All this confusion is one of my main gripes with GIS standards. Back in the days, someone decided to give the name “line” to what actually is a line segment in mathematics.

@Chamika_Kasun you can always check the source code of our Meshes.jl submodule when you need to learn about these geometric algorithms, it is plain simple Julia code:

> <https://github.com/JuliaGeometry/Meshes.jl/blob/master/src/predicates/in.jl>

Don’t reinvent the wheel re-implementing these algorithms from scratch, because they can be quite tricky to get right. There are too many corner cases that we are still fixing with various industrial applications.

---

<div class="post-metadata">

### Author: ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)
#### Post date: [November 8, 2023, 11:28am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/9 "2023-11-08T11:28:36Z")

</div>

“line” and “between” have been defined for 1000s of years. I don’t see any serious opportunity for confusion or misunderstanding here.

Here’s an example “I’m a liberal somewhere between Trump and DeSantis and I’m not line.”

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [November 8, 2023, 11:53am UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/10 "2023-11-08T11:53:38Z")

</div>

The confusion exists because GIS Line and LineString should have been called LineSegment and polygonal Chain.

A line is infinite, a segment is not.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [November 8, 2023, 12:02pm UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/11 "2023-11-08T12:02:14Z")

</div>

I recal that data in OP’s example is geographical, so none e of the above solutions do really apply.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [November 8, 2023, 12:39pm UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/12 "2023-11-08T12:39:38Z")

</div>

At the scale of the screenshots, the solution will apply.

Algorithms for geographic coordinates will be added in the near future. It is an implementation detail that we also plan to hide from end users, who don’t care about all this GIS idiosyncrasy.

---

<div class="post-metadata">

### Author: ![gvdr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gvdr/32/6387_2.png) [@gvdr](https://discourse.julialang.org/u/gvdr)
#### Post date: [November 8, 2023, 5:41pm UTC](https://discourse.julialang.org/t/how-to-determine-if-a-point-given-is-within-a-line-two-points/105896/13 "2023-11-08T17:41:27Z")

</div>

Hence OP example in the question is wrong, as that point is NOT on the line. 🤷‍♂️

Or, maybe, OP is intending those terms in a slight different way from the geometrical one. As you would when you say that Florence is between Milan and Rome.

Thus, there is some confusion 😁
