### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Finding series intersection  (Read 499 times)

#### celso

• New member
• Posts: 8
##### Finding series intersection
« on: December 26, 2022, 04:12:22 pm »
In this chart attached, I have two series one is a TLineSeries (the blue one), and another is a TExpressionSeries (the red one). I need to know where the lines cross with each other, the intersection, there is two points where it cross.

There is any TChart tool I can use to detect it? Or any other way?

#### wp

• Hero Member
• Posts: 10875
##### Re: Finding series intersection
« Reply #1 on: December 26, 2022, 06:04:16 pm »
No, there is no ChartTool to calculate the intersection of two series. You must calculate this yourself:
• Iterate over all points of the LineSeries (make sure that it is sorted in x) and calculated the values of the ExpressionSeries at the x values of the LineSeries points
• Find the segment of the LineSeries (between x=x1 and x2) for which the y value of the ExpressionSeries at x=x1 is greater (or smaller) than the y value of the LineSeries at x1, and for which the y value of the ExpressionSeries at x=x2 is smaller (or larger) then the y value of the LineSeries at x2 -- now you know that the intersection occurs between the points at x1 and x2.
• Since each segment of a line series is a linear function you can calculated every point on this segment as y = (x-x1)(x2-x1)*(y2-y1) + y1
• This is needed when you apply a numerical algorithm such as Newton-Raphson to refine the intersection point: https://en.wikipedia.org/wiki/Newton%27s_method
• An alternative method is bisection in which the interval is recursively subdivided into halves so that the intersection point is always in that sub-interval for which the function series at one end is above (or below) and at the other end is below (or above) the lineseries. In the attachment there is an example how this could be coded. (Note that this code finds only a single intersection per segment).
« Last Edit: December 26, 2022, 06:57:16 pm by wp »

#### celso

• New member
• Posts: 8
##### Re: Finding series intersection
« Reply #2 on: December 28, 2022, 07:29:25 pm »
Hello friend, sorry for the late answer, thanks for your help, it worked just fine. For some reason the TChart inverted my values, but I could adjust it.