How to do Hit testing in GDI+ for rotated shapes with real shape measurements (inches)?

前端 未结 2 1891
时光取名叫无心
时光取名叫无心 2021-02-10 18:20

Given I have a canvas that contains many shapes, lets say rectangles for now.

Each shape has a location (inches), size(inches) and rotation angle(degrees).

When

2条回答
  •  庸人自扰
    2021-02-10 18:38

    I found the answer (I have to convert al measurements to pixels to make sure it will calculate correctly):

    public static bool HitTest(Rectangle bounds, float angle, Point location)
            {
                if (angle == 0) return bounds.Contains(location);
    
                using (Matrix matrix = new Matrix())
                {
                    matrix.RotateAt(angle, Center(bounds));
                    using (GraphicsPath path = new GraphicsPath())
                    {
                        path.AddRectangle(bounds);
                        path.Transform(matrix);
                        return path.IsVisible(location.X, location.Y);
                    }
                }
            }
    

提交回复
热议问题