### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Fastline series with simplification  (Read 2890 times)

#### hedgehog

• New Member
• Posts: 47
##### Re: Fastline series with simplification
« Reply #15 on: January 10, 2024, 12:15:54 pm »
My first idea: we could easily modify the Douglas-Peuker algorithm so that some points always end up in the final array.

My second idea: don't draw polygons with a hole. You can sort all the polygons (by area value) and draw them starting with the largest.

I know, it should probably have been named "PointDist2" or similar, but it is difficult to change because it will break the code of somebody who used it.
function PointDist(...); deprecated; // use PointDist2 ???

#### wp

• Hero Member
• Posts: 12027
##### Re: Fastline series with simplification
« Reply #16 on: January 10, 2024, 02:09:10 pm »
My second idea: don't draw polygons with a hole. You can sort all the polygons (by area value) and draw them starting with the largest.
This does not work when the background is non-uniform, a bitmap for example, because the bitmap should be visible through the hole.

I know, it should probably have been named "PointDist2" or similar, but it is difficult to change because it will break the code of somebody who used it.
Code: [Select]
`function PointDist(...); deprecated; // use PointDist2 ???`
Done, but named it PointDistSq rather than Point2. Same with PointLineDist. (https://wiki.freepascal.org/Lazarus_4.0_release_notes#TAChart_2)

#### hedgehog

• New Member
• Posts: 47
##### Re: Fastline series with simplification
« Reply #17 on: January 14, 2024, 12:28:32 pm »
Quote
wp
I made a small demo (in attach).
That is, it was surprising to me, but this method (Canvas.DrawPolygon) really works.

As you can see in the screenshots, there is a hole! Even if polyline simplification works.

You can see how the number of dots is dramatically reduced (by a factor of 100..1000), and yet the map remains recognizable.
« Last Edit: January 14, 2024, 12:30:07 pm by hedgehog »

#### wp

• Hero Member
• Posts: 12027
##### Re: Fastline series with simplification
« Reply #18 on: January 14, 2024, 02:56:50 pm »
Nice.

And now I know why it's working, or better: I know the conditions when it's working:

- The map provider must provide the outer and inner (and maybe more inner) polygons separately.
- Each polygon must be closed.
- The point order must flipped in each level of polygons, i.e. if the outer-most polygon is clockwise, the next inner polygon must be counter-clockwise, etc.

The attachment contains a small project demonstrating the principle.

The polygon order is not changed by the polygon simplification, and the fact that the polygon is closed is not changed either. Therefore, the hole remains after simplification.

There is one issue left which prevents me from including this in TAChart. As I already said, it is not only the line series or polygon series which could benefit, an area series, a spline series and maybe some others might need it too and thus would have to duplicate the algorithm. This is not good. It would be better if the algorithm were implemented in the ChartSource which provides the data for the series. One idea how to do this:
• Create a new TFilteredChartSource (or a new option in TCalculatedChartsource, but this looks more complicated to me)
• This source gets a property Origin: TCustomChartSource to which the user must "hook" the original source containing the data
• This source gets in integer list in which all the data point indices from the original source will be stored which are included after the filtering process.
• The FilterChartSource must implement the filtering algorithm. It gets the data from the Origin source as pointers to TChartDataItem records (type TChartDataItem = record X, Y: Double; ...; end) and stores the accepted index in the mentioned list.
• As any other chartsoure, TFilteredChartSource must override the function GetItem(AIndex: Integer): PChartDataItem. AIndex is the data point index that the series wants to plot. The filtered source must lookup the original point index in its integer list and then finds the PChartDataItem under this index in the original source.
One problem, maybe, of this approach: Since filtering is done on the original data the effect might be not be suitable for visual presentation. Suppose a time series in which the y data are plotted logarithmically...

Maybe it's better to provide a filter directly in the series as "plug-in component" filter which operates on the points obtained after world-to-screen transformation. But something like this has not been provided in the TAChart infrastructure...