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

7
  • 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, 2012 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, 2012 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, 2012 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, 2012 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, 2012 at 19:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.