Intersection of two plots

I have two lines on a plot and I want Julia to find the intersection between the two lines but I can’t seem to find a package that can help with that.

see

or even:

The GMT fill_between function uses the gmtspatial module to do that.

https://www.generic-mapping-tools.org/GMTjl_doc/examples/plotting_functions/02_1filledlines/#fill_between

Idea

basically, if we don’t have explicit function expression, the curve is a set of data
simplify way is link two points to segment ,then check whether two segment intersect
this is naive method.
But I thought this is not a simple question, for numerical method, neighbors are problem

step

  • package Meshes.jl for Segment method Intersection
    using Meshes
    s1 = Segment((0.0,0.0), (1.0,0.0))
    s2 = Segment((0.5,0.0), (2.0,0.0))
    inst=s1 ∩ s2
    

typeof(inst)==Point2

if segment does’nt intersect, then return nothing

  • make data

  • iterate to find intersection point


code


using Meshes

step = 100
xspan = range(-2, 2, step)
arr = []
d1 = [(x, x^2) for x in xspan] .|> (d -> (round(d[1], digits=4), round(d[2], digits=4)))
d2 = [(x, 2 * x) for x in xspan] .|> (d -> (round(d[1], digits=4), round(d[2], digits=4)))

for i in 1:step-1

    local s1 = Segment(d1[i], d1[i+1])
    local s2 = Segment(d2[i], d2[i+1])
    local bool = s1 ∩ s2 |> d -> (typeof(d) == Point2)
    (bool == true) && push!(arr, res)
end

arr