evgeninikolov
Posts: 2
Joined: Wed Nov 24, 2021 10:50 am

Calculating an intersection between a line and a spline series

Hello,

I am trying to find a way to mark the points where a straight line and a spline are crossing.
Here is an example: https://jsfiddle.net/cpm9L5yk/

I've looked at similar solutions but they only work with chart type `line` and not with `spline`.
This demo for example, finds the intersection points using linear interpolation https://jsfiddle.net/sxrbmg69/2/ but if we switch the `line` type to `spline` then it doesn't work anymore.

From what I've read I found that I have to calculate the intersections using a the method that highcharts use for drawing these splines - cubic bezier curve.
I've tried to implement that separately but when I compare the calculated points to the highcharts intersections manually I am seeing a difference.

I've read further and I found out that highcharts is using a smoothing value to the control points and maybe that's the reason for the diff that I am seeing.
viewtopic.php?t=35013

So as a last resort, looking at this thread viewtopic.php?t=36066 I understood that I have to build the control points the same way the highcharts library does but the implementation there is too complex for me to wrap my head around and I am starting to feel a bit discouraged. :(

Is it really that hard to detect intersections between a line and a spline? It would be great if someone is able to help me with tracing this down to a working formula. :idea:

The worst case would be to just use a `line` for all series in my chart but that would make the whole chart uglier.
evgeninikolov
Posts: 2
Joined: Wed Nov 24, 2021 10:50 am

Re: Calculating an intersection between a line and a spline series

As an end result I would want https://jsfiddle.net/sxrbmg69/2/ this example that uses a spline to work correctly by calculating the correct crossing points with the horizontal line.
michal.f
Posts: 1114
Joined: Thu Aug 12, 2021 12:04 pm

Re: Calculating an intersection between a line and a spline series

Hi,

Welcome to our forum and thanks for contacting us with your question!

As you write here, it is on difficult.
Is it really that hard to detect intersections between a line and a spline?

In the demo you sent in, the function calculates the place based on the Pythagorean formula. In 'spline' you have to use a different one.
This is a mathematical problem and, unfortunately, it is beyond the scope of the forum, but I found a thread that will help you: viewtopic.php?t=42935

Let me know if you have any further questions!
Best regards!
Michał Filipiec
Highcharts Developer
https://blacklabel.pl/highcharts/

Return to “Highcharts Usage”