0

I have an app that tracks a users movements and plots the points on a custom map.

I have an issue where after a period of hours the device gets very slow and eventually runs out of memory.

My question is what would be the best way to draw only the points / lines that are visible?

My drawing code is

        using (var linePaint = new SKPaint())
        {
            linePaint.Style = SKPaintStyle.Stroke;
            linePaint.Color = SKColors.White;
            linePaint.StrokeJoin = SKStrokeJoin.Round;
            linePaint.StrokeCap = SKStrokeCap.Round;
            linePaint.StrokeWidth = lineWidth * 2f;

            Point previousPoint = null;
            foreach (var point in localPoints)
            {
                if (previousPoint == null)
                {
                    previousPoint = point;
                    continue;
                }

                canvas.DrawLine(previousPoint.X, previousPoint.Y, point.X, point.Y,
                    linePaint);

                previousPoint = point;
            }

How can this be reduced to only drawing points in view?

Dan
  • 115
  • 1
  • 8
  • You need to work out whether the line you're about to draw intersects with the visible region, and skip drawing it if so. Use [`Graphics.VisibleClipBounds`](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics.visibleclipbounds?view=netframework-4.8) for the visible rectangle, and [see here for an algorithm to determine if a line intersects a rectangle](https://stackoverflow.com/a/5514619/106159). – Matthew Watson Oct 01 '21 at 11:27
  • Actually, I'm not sure Graphics.VisibleClipBounds is available for xamarin - hopefully there's some other way of determining the coords of the visible rectangle. – Matthew Watson Oct 01 '21 at 11:30
  • 1
    have you actually determined that this is the root cause of your memory problem, or is it just a stab in the dark? – Jason Oct 01 '21 at 12:04
  • 1
    Profiling the App would be a good start to figure out what is taking up the memory. – Cheesebaron Oct 01 '21 at 12:05

0 Answers0