0

I write a program that draws strings over/on/under given path. The problem is that I need to check intersections between actually drawing letter and all the previous letters. I do it and it works fine. The only thing is about performance.

private void RemoveOverlapping(Graphics gr, float angle, float x, float y, GraphicsPath graphicsPath)
{
    Matrix matrix = new Matrix();
    for (;;)
    {
        matrix.Reset();
        matrix.RotateAt(angle, new PointF(x, y));
        graphicsPath.Transform(matrix);

        bool isOverlapping = false;
        Region letterRegion = new Region(graphicsPath);
        foreach (Region region in _previousRegions)
        {
            region.Intersect(letterRegion);

            if (!region.IsEmpty(gr))
            {
                matrix.Reset();
                matrix.RotateAt(360f - angle, new PointF(x, y));
                graphicsPath.Transform(matrix);

                matrix.Reset();
                matrix.Translate(1, 0);
                graphicsPath.Transform(matrix);

                isOverlapping = true;
                break;
            }
        }

        letterRegion.Dispose();

        if (!isOverlapping)
            return;
    }
}

I did Alt+F2 tests and it told me that region.IsEmpty() takes most of calculation time... Is there any another way (without implementing additional algorithms) to check two Regions/GraphicsPaths intersections?

It's how it looks like:
writings

Nickon
  • 9,652
  • 12
  • 64
  • 119
  • Is there a way to quickly discard previous regions which you are certain are not overlapping? I.e. just by looking at the bounding rectangle? – vgru Nov 20 '12 at 15:24
  • Not exactly, because the angle between two strings can be so small, that more than one of the previous letters can overlap a new one. – Nickon Nov 20 '12 at 15:46
  • How many letters are there? If you can discard 5 out of 10 letters, that's an improvement. OTOH, how fast is `Region.Intersect` compared to `IsEmpty`? Both of these methods P/Invoke native `gdiplus.dll` methods, but I would presume that `IsEmpty` is still a bit faster. Also, can you post some actual inputs to that method? – vgru Nov 20 '12 at 16:12
  • Also, if *more than one of the previous letters can overlap a new one*, doesn't that mean that previous letters can be overlapping among themselves? Sorry if I got something wrong about that part. – vgru Nov 20 '12 at 16:16
  • How many letters - it depends, even 20 when the font size and the angle are rly small. I used `Alt+F2` tests in `VS2012` and it told me that `IsEmpty` takes `98%` of computing time... I have tested it couple times always almost the same results. – Nickon Nov 20 '12 at 19:10
  • Tomorrow I will upload a photo with my problem, because the program is in my job and I don't want to pull GIT repo at home:) – Nickon Nov 20 '12 at 19:12
  • The performance will depend on the __number of rectangles needed to cover__ a region. See [here](https://stackoverflow.com/questions/43139118/region-isvisiblepointf-has-very-slow-performance-for-large-floating-point-valu/43180365?s=10|29.6415#43180365) for a discussion. If you can live with it, using only bounding rectangles will make it really fast compared to those highly complex letter paths.. – TaW Jul 08 '18 at 08:20

0 Answers0