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

Given that I have a canvas that contains many shapes, now you can specify rectangles.

Each shape has an arrangement (inches), size (inches) and an angle of rotation (in degrees).

When a mouse event occurs inside the canvas for the location (x, y) in pixels.

I want to check if the clicked position of the mouse is inside / inside a certain shape, given the angle of rotation and unit conversion.

You can help?

+4
source share
2 answers

I found the answer (I need to convert all measurements to pixels to make sure that it will be calculated 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); } } } 
+3
source

Your question is terribly short in detail, I can give a general answer. Doing it mathematically is the fastest way. Spinning can make this difficult.

You can solve this slowly but easily using a bit test. Display the shapes in a bitmap using the same code that you are now using to display it on the screen. But now use a color that encodes the form number. Hit testing is now simple and fast with GetPixel (). Be careful to turn off image enhancement settings, such as anti-aliasing. First open it on the screen and take a good look at the pixels with ZoomIt.

+4
source

All Articles